Java SpringBoot

Alguém poderia me ajudar. Quando rodo o código dá esse erro:

Error creating bean with name 'topicosController': Unsatisfied dependency expressed through field 'topicoRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicoRepository' defined in br.com.projeto.forum.repository.TopicoRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class br.com.projeto.forum.model.Topico

Posta o código da interface TopicoRepository pra gente ver.

package br.com.projeto.forum.repository;

import java.util.List;

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

import br.com.projeto.forum.model.Topico;


public interface TopicoRepository extends JpaRepository<Topico, Long> {


	List<Topico> findByCursoNome(String nomeCurso);

}

A classe Topico é uma @Entity?

Tente também colocar a anotação @Repository na interface TopicoRepository.

Coloquei. Mas apareceu outro erro agora :
Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: br.com.projeto.forum.repository.TopicoRepository

Pelo erro, parece que sua @Entity não tem um @Id definido. Posta a entidade Topico pra gente ver.

package br.com.projeto.forum.model;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

public class Topico {
	
	@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	private String titulo;
	private String mensagem;
	private LocalDateTime dataCriacao = LocalDateTime.now();
	@Enumerated(EnumType.STRING)
	private StatusTopico status = StatusTopico.NAO_RESPONDIDO;
	@ManyToOne
	private Usuario autor;
	@ManyToOne
	private Curso curso;
	@OneToMany(mappedBy = "topico")
	private List<Resposta> respostas = new ArrayList<>();

	
	
	public Topico(String titulo, String mensagem, Curso curso) {
		this.titulo = titulo;
		this.mensagem = mensagem;
		this.curso = curso;
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Topico other = (Topico) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitulo() {
		return titulo;
	}

	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public String getMensagem() {
		return mensagem;
	}

	public void setMensagem(String mensagem) {
		this.mensagem = mensagem;
	}

	public LocalDateTime getDataCriacao() {
		return dataCriacao;
	}

	public void setDataCriacao(LocalDateTime dataCriacao) {
		this.dataCriacao = dataCriacao;
	}

	public StatusTopico getStatus() {
		return status;
	}

	public void setStatus(StatusTopico status) {
		this.status = status;
	}

	public Usuario getAutor() {
		return autor;
	}

	public void setAutor(Usuario autor) {
		this.autor = autor;
	}

	public Curso getCurso() {
		return curso;
	}

	public void setCurso(Curso curso) {
		this.curso = curso;
	}

	public List<Resposta> getRespostas() {
		return respostas;
	}

	public void setRespostas(List<Resposta> respostas) {
		this.respostas = respostas;
	}

}

Depois que vc adicionou o @Entity e o @Id, deu certo?

O id já tinha. Não está dando certo

Qual o nome da tabela? Vc deixou o @Repository na TopicoRepository?

Qual tabela vc fala?

A tabela no banco referente à entidade Topico.

Coloquei o @Repository na classe topico que sobe para o banco. Mas continua com o mesmo erro

Segui um passo a passo encontrado em um forum. Aparente esse erro resolveu agora apareceu outro. Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.

1 curtida

Hmmm, apareceu alguma exception (stacktrace) no console junto com essa mensagem?

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topico' defined in file [C:\Users\KerolenRodrigues\Documents\Estudos\forum\target\classes\br\com\projeto\forum\model\Topico.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Uai, parece que o spring está tentando carregar o Topico como um bean gerenciado. Estranho. Nessa classe Topico tem alguma anotação @Component ou @Service?

Ah entendi, vc colocou o @Repository na classe errada. Em vez de colocar no Topico, deveria estar na TopicoRepository.

Na classe Topico, deve ter apenas o @Entity (pois ela é uma entidade que mapeia uma tabela do banco na aplicação).

@Repository
public interface TopicoRepository extends JpaRepository<Topico, Long> {
@Entity
public class Topico {

Arrumei. Só que agora veio outro erro Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: br.com.projeto.forum.model.Topico.respostas[br.com.projeto.forum.model.Resposta]