0

I have an Spring boot Api that uses jpa with postgresql, and is supossed to automatically create the the tables when i run the Application.

But the following error messages appears:

> 
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.globalsys.apicep.model.Cep.Cidade in com.globalsys.apicep.model.Cidade.ceps
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:844) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:795) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.1.jar:5.3.1]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.1.jar:5.3.1]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.1.jar:5.3.1]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

This is my application.properties

server.port=8080
spring.datasource.url=jdbc:postgresql://localhost:5432/dbcep
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=romulo
spring.jpa.open-in-view=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
Simulant
  • 19,190
  • 8
  • 63
  • 98
Rômulo Sorato
  • 1,570
  • 5
  • 18
  • 29
  • Does this answer your question? [PostgreSQL + Hibernate + Spring auto create database](https://stackoverflow.com/questions/38299685/postgresql-hibernate-spring-auto-create-database) – Simulant Nov 20 '20 at 16:15
  • In POJO you are mapping wrong target entity. correct that, then it will work. properties looks fine. i think you are using something like (ManyToOne or ManyToMany etc) so there you are making a mistake. – priyranjan Nov 20 '20 at 16:42
  • Souns correct to me, what is wrong? I will edit the question to add the pojo models – Rômulo Sorato Nov 20 '20 at 16:59

1 Answers1

0

So good news! The problem is a reference in the mapped by properties that i thought is nothing wrong at first.

In the OneToMany anootation i put in this way referencing the table itself!

@OneToMany(mappedBy = "Cidade", cascade = CascadeType.ALL)
    private List<Cep> ceps;

Turns out you can not refer to the table itself! Instead you need to refer the property in the other side of relationship that is the owner of relationship!

 @ManyToOne
    private Cidade cidade;

In the Cep class model!

Rômulo Sorato
  • 1,570
  • 5
  • 18
  • 29