AnnaKlein AnnaKlein -4 years ago 160
Java Question

mappedBy reference an unknown target entity property with annotation

I try to store an Observable and its observers into a database. I receive during execution


org.hibernate.AnnotationException: mappedBy reference an unknown target entity property


Of course I used google and found some good answer.

So I added to the annotation mappedBy, targetEntity, fetch and cascade.

But I still got the same error. I tried so far actually everything I could find with google with no success. It is probably only a minor think and I hope you can help me. (I use Spring Boot)

ObservableImpl

@Entity
public class ObservableImpl extends Observable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(mappedBy = "oservableImpl", targetEntity = ObserverImpl.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
List<ObserverImpl> observerList = new LinkedList<>();


public ObservableImpl(String name) {
this.name = name;
}

protected ObservableImpl() {
}

@Override
public synchronized void addObserver(Observer o) {
super.addObserver(o);
observerList.add((ObserverImpl) o);
}

@Override
public synchronized void deleteObserver(Observer o) {
super.deleteObserver(o);
observerList.remove(o);
}

public void change() {
setChanged();
notifyObservers();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ObservableImpl that = (ObservableImpl) o;

return id == that.id;
}

@Override
public String toString() {
return "ObservableImpl{" +
"ID=" + id +
'}';
}
}


ObserverImpl

@Entity
public class ObserverImpl implements Observer {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ID;
private String name;

public Long getID() {
return ID;
}

public void setID(Long ID) {
this.ID = ID;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public ObserverImpl(String name) {
this.name = name;
}

protected ObserverImpl() {
}


@Override
public String toString() {
return "ObserverImpl{" +
"ID=" + ID +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ObserverImpl observer = (ObserverImpl) o;

return ID == observer.ID;
}


@Override
public void update(Observable o, Object arg) {
System.out.println(this);
}
}


Error Message:

2017-06-11 14:40:57.032 INFO 21156 --- [ main] c.e.hibernate.HibernateApplication : No active profile set, falling back to default profiles: default
2017-06-11 14:40:57.061 INFO 21156 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4516af24: startup date [Sun Jun 11 14:40:57 CEST 2017]; root of context hierarchy
2017-06-11 14:40:57.826 INFO 21156 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$cfcf04d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-11 14:40:58.032 INFO 21156 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-11 14:40:58.039 INFO 21156 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-06-11 14:40:58.039 INFO 21156 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-06-11 14:40:58.091 INFO 21156 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-11 14:40:58.091 INFO 21156 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1031 ms
2017-06-11 14:40:58.163 INFO 21156 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-11 14:40:58.165 INFO 21156 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-11 14:40:58.166 INFO 21156 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-11 14:40:58.166 INFO 21156 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-11 14:40:58.166 INFO 21156 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-11 14:40:58.387 INFO 21156 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-11 14:40:58.394 INFO 21156 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-06-11 14:40:58.428 INFO 21156 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2017-06-11 14:40:58.428 INFO 21156 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-06-11 14:40:58.429 INFO 21156 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-06-11 14:40:58.449 INFO 21156 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-06-11 14:40:58.501 INFO 21156 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2017-06-11 14:40:58.596 WARN 21156 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.hibernate.obs.ObserverImpl.oservableImpl in com.example.hibernate.obs.ObservableImpl.observerList
2017-06-11 14:40:58.598 INFO 21156 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2017-06-11 14:40:58.606 INFO 21156 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-11 14:40:58.612 ERROR 21156 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.hibernate.obs.ObserverImpl.oservableImpl in com.example.hibernate.obs.ObservableImpl.observerList
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.example.hibernate.HibernateApplication.main(HibernateApplication.java:19) [classes/:na]
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.hibernate.obs.ObserverImpl.oservableImpl in com.example.hibernate.obs.ObservableImpl.observerList
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:719) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1623) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 16 common frames omitted


Process finished with exit code 1


Thank you a lot.

HibernateApplication

@SpringBootApplication
public class HibernateApplication {


private static final Logger log = LoggerFactory.getLogger(HibernateApplication.class);

public static void main(String[] args) {
SpringApplication.run(HibernateApplication.class);
}


@Bean
public CommandLineRunner addObservables(ObserveableRepository repository) {
return (args) -> {
useObserver(repository);

// fetch all customers
log.info("Observables found with findAll():");
log.info("-------------------------------");
for (ObservableImpl observable : repository.findAll()) {
log.info(observable.toString());
observable.change();
}
log.info("");

};
}

private void useObserver(ObserveableRepository repository) {
ObservableImpl observable = new ObservableImpl("1");
observable.addObserver(new ObserverImpl("firstObserver"));
observable.addObserver(new ObserverImpl("secondObserver"));
observable.addObserver(new ObserverImpl("thirdObserver"));
repository.save(observable);
}
}


ObserveableRepository

public interface ObserveableRepository extends CrudRepository<ObservableImpl, Long> {

}


application.properties

spring.datasource.url=jdbc:h2:file:~/test3;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=admin
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect

Answer Source

I recommend you to rename your entities and give them "simple" and comprehended names. This will allow you not take care about the complex definitions of their fields and possible errors.

For example:

@Entity
public class Experiment extends Observable {
    //...

    @OneToMany(mappedBy = "experiment")
    private Set<Experimenter> experimenters = new HashSet<>();

    //...
} 

@Entity
public class Experimenter extends Observer {
    //...

    @ManyToOne
    private Experiment experiment;

    //...
} 

So we had here a classic bidirectional OneToMany association


To get access to H2 database from IDE you can use this answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download