1
resposta

Criando migrations: Error starting ApplicationContext.

Ao executar o ForumApplication.kt está retornando este erro:

Caused by: org.hibernate.AnnotationException: Association 'br.com.alura.forum.model.Topico.respostas' targets the type 'Resposta' which does not belong to the same persistence unit

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-02-24T17:00:37.307-03:00 ERROR 20652 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Association 'br.com.alura.forum.model.Topico.respostas' targets the type 'Resposta' which does not belong to the same persistence unit at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205) ~[spring-beans-6.1.8.jar:6.1.8] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:952) ~[spring-context-6.1.8.jar:6.1.8] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624) ~[spring-context-6.1.8.jar:6.1.8] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.0.jar:3.3.0] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.0.jar:3.3.0] at br.com.alura.forum.ForumApplicationKt.main(ForumApplication.kt:13) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.3.0.jar:3.3.0] Caused by: org.hibernate.AnnotationException: Association 'br.com.alura.forum.model.Topico.respostas' targets the type 'Resposta' which does not belong to the same persistence unit at org.hibernate.boot.model.internal.CollectionBinder.detectManyToManyProblems(CollectionBinder.java:2607) ~[hibernate-core-6.5.2.Final.jar:6.5.2.Final]

1 resposta

Olá, Rosimeire. Tudo bem?

O erro "Association 'br.com.alura.forum.model.Topico.respostas' targets the type 'Resposta' which does not belong to the same persistence unit" geralmente indica que as classes Topico e Resposta não estão sendo reconhecidas como parte da mesma unidade de persistência.

Como eu não sei em que contexto você está, seja de curso ou aula, vou deixar algumas sugestões para tentar resolver o problema, baseado no erro:

  1. Verifique o arquivo de configuração persistence.xml: Se você estiver usando um arquivo persistence.xml, verifique se ambas as entidades Topico e Resposta estão listadas dentro da mesma unidade de persistência. Algo como:

    <persistence-unit name="yourPersistenceUnitName">
        <class>br.com.alura.forum.model.Topico</class>
        <class>br.com.alura.forum.model.Resposta</class>
    </persistence-unit>
    
  2. Anotações nas Entidades: Verifique se ambas as classes Topico e Resposta estão anotadas corretamente com @Entity. Além disso, certifique-se de que as importações estão corretas e que não há duplicidade de classes.

  3. Configuração do Spring Boot: Se você não estiver usando um persistence.xml, verifique se as entidades estão sendo escaneadas corretamente pelo Spring Boot. Geralmente, isso é feito automaticamente, mas você pode especificar pacotes adicionais para escanear no arquivo application.properties ou application.yml:

    spring.jpa.properties.hibernate.default_schema=yourSchemaName
    
  4. Pacotes Diferentes: Verifique se as classes Topico e Resposta estão no mesmo pacote ou que o pacote onde estão localizadas está sendo escaneado pelo Spring Boot.

  5. Dependências no pom.xml ou build.gradle: Verifique se as dependências do Hibernate e Spring Data JPA estão corretamente configuradas. Às vezes, versões incompatíveis podem causar problemas.

Espero que essas dicas ajudem a resolver o problema. Bons estudos.

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.Bons Estudos!