Spring data JPA error (Parameter 0 of constructor and entityManagerFactory)

Asked

Viewed 1,140 times

0

Guys, for hours I’ve been trying to solve a problem in my Spring Data JPA I execute the code and I get this error

Description:

Parameter 0 of constructor in br.com.aluguel.de.carros.alugador.AlugadorServiceImpl required a bean named ''entityManagerFactory'' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean named ''entityManagerFactory'' in your configuration.

As if the builder of my Rental was 0, but is not!! I’ve searched everywhere on the internet, tell me to add some dependencies to my pom.xml, I add and the error persists

My lease is like this:

package br.com.aluguel.de.carros.alugador;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class AlugadorServiceImpl implements AlugadorService {
    private AlugadorRepository repository;

    @Autowired
    public AlugadorServiceImpl(AlugadorRepository repository) {
        this.repository = repository;
    }

    @Override
    public List<Alugador> todos() {
        return repository.findAll();
    }

    @Override
    public Optional<Alugador> alugador(Long id) {
        return repository.findById(id);
    }

    @Override
    public Alugador novo(Alugador alugador) {
        alugador.setNome(alugador.getNome());
        return repository.save(alugador);
    }

    @Override
    public Alugador atualiza(Alugador alugador) {
        Alugador alugadorAtualizado = repository.save(alugador);
        //repository.reflesh(alugadorAtualizado);
        return alugadorAtualizado;
    }

    @Override
    public boolean deleta(Long id) {
        Optional<Alugador> alugadorOpt = alugador(id);
        if (alugadorOpt.isPresent()) {
            repository.delete(alugadorOpt.get());
            return true;
        }
        return false;
    }
}

My pom.xlm is like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.tmf</groupId>
    <artifactId>gp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>gp</name>
    <description>WebApp Para gerenciar projetos</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

My Rentidorservice is like this:

package br.com.aluguel.de.carros.alugador;

import java.util.List;
import java.util.Optional;

public interface AlugadorService {
    List<Alugador> todos();

    Optional<Alugador> alugador(Long id);

    Alugador novo(Alugador alugador);

    Alugador atualiza(Alugador alugador);

    boolean deleta(Long id);
}

And my Renter Repository is like this:

package br.com.aluguel.de.carros.alugador;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AlugadorRepository extends JpaRepository<Alugador, Long> {
}

Somebody help me please, I don’t know what else to do

1 answer

1

I needed to add the relationship in the Entity

@OneToOne
    @JoinColumn(name = "id_endereco")
    private Endereco endereco;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.