org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl

Hibernate JIRA | Adam T | 10 years ago
  1. 0

    I have found a bug in Hibernate when mixing annotations and mapping files. I have generated a minimal test case. Additionally, I have confirmed that the error does not appear when all classes use annotations. There are two mapped classes AImpl and BImpl. AImpl extends BImpl. A third annotated class ZImpl, refers to BImpl through a ManyToOne relationship. All method signatures refer to these classes by their interfaces A, B, and Z; however, Hibernate is instructed to use the *Impl classes when necessary. I am NOT mixing annotations and mappings in a single inheritance hierarchy. All classes implement Serializable by extending their Serializable interfaces. The minimal test case is attached as a zip and included below: 13:02:57,811 INFO [Version] Hibernate Annotations 3.2.1.GA 13:02:57,827 INFO [Environment] Hibernate 3.2.1 13:02:57,827 INFO [Environment] hibernate.properties not found 13:02:57,842 INFO [Environment] Bytecode provider name : cglib 13:02:57,842 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling 13:02:57,936 INFO [Configuration] configuring from resource: /hibernate.cfg.xml 13:02:57,936 INFO [Configuration] Configuration resource: /hibernate.cfg.xml 13:02:58,077 INFO [Configuration] Reading mappings from resource : B.hbm.xml 13:02:58,342 INFO [Configuration] Reading mappings from resource : A.hbm.xml 13:02:58,389 INFO [Configuration] Configured SessionFactory: null 13:02:58,655 INFO [HbmBinder] Mapping class: test.AImpl -> A 13:02:58,670 INFO [AnnotationBinder] Binding entity from annotated class: test.ZImpl 13:02:58,702 INFO [EntityBinder] Bind entity test.ZImpl on table Z 13:02:58,780 ERROR [HibernateUtil] Building SessionFactory failed. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56) at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283) at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85) at test.TestMixedMappings.main(TestMixedMappings.java:10) Exception in thread "main" java.lang.ExceptionInInitializerError at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:93) at test.TestMixedMappings.main(TestMixedMappings.java:10) Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56) at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283) at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85) ... 1 more hibernate.cfg.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.datasource">java:/comp/env/jdbc/newsoncongress</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <mapping resource="B.hbm.xml" /> <mapping resource="A.hbm.xml" /> <mapping class="test.ZImpl" /> </session-factory> </hibernate-configuration> A.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 --> <hibernate-mapping> <class name="test.AImpl" table="A" proxy="test.A"> <id name="aId" column="aID" type="java.lang.Integer"> <generator class="identity" /> </id> </class> </hibernate-mapping> B.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 --> <hibernate-mapping> <joined-subclass name="test.BImpl" table="B" proxy="test.B" extends="test.AImpl"> <key column="aID" /> <property name="bId" column="bID" type="java.lang.Integer" not-null="true" /> </joined-subclass> </hibernate-mapping> A.java: package test; public interface A extends java.io.Serializable { public Integer getAId(); public void setAId(Integer aId); } B.java: package test; public interface B extends A { public Integer getBId(); public void setBId(Integer bId); } Z.java: package test; public interface Z extends java.io.Serializable { public Integer getZId(); public void setZId(Integer zId); public B getB(); public void setB(B b); } AImpl.java: package test; public class AImpl implements A { private static final long serialVersionUID = 1L; private Integer aId = 0; public AImpl() { } public Integer getAId() { return this.aId; } public void setAId(Integer aId) { this.aId = aId; } } BImpl.java: package test; public class BImpl extends AImpl implements B { private static final long serialVersionUID = 1L; private Integer bId = 0; public BImpl() { super(); } public Integer getBId() { return bId; } public void setBId(Integer bId) { this.bId = bId; } } ZImpl.java: package test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Inheritance(strategy = InheritanceType.JOINED) @org.hibernate.annotations.Proxy(proxyClass = Z.class) @Table(name = "Z") public class ZImpl implements Z { private static final long serialVersionUID = 1L; private Integer zId = null; private B b = null; @Id @Column(name = "zID") public Integer getZId() { return zId; } public void setZId(Integer zId) { this.zId = zId; } @ManyToOne(optional = false, targetEntity = BImpl.class, fetch = FetchType.LAZY) @JoinColumn(name = "bID", referencedColumnName = "bID") public B getB() { return b; } public void setB(B b) { this.b = b; } } ... When the AImpl and BImpl classes are annotated, the error disappears. AImpl.java: package test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Inheritance(strategy = InheritanceType.JOINED) @org.hibernate.annotations.Proxy(proxyClass = A.class) @Table(name = "A") public class AImpl implements A { private static final long serialVersionUID = 1L; private Integer aId = 0; public AImpl() { } @Id @Column(name = "aID") public Integer getAId() { return this.aId; } public void setAId(Integer aId) { this.aId = aId; } } BImpl.java: package test; import javax.persistence.Entity; import javax.persistence.Table; @Entity @org.hibernate.annotations.Proxy(proxyClass = B.class) @Table(name = "B") public class BImpl extends AImpl implements B { private static final long serialVersionUID = 1L; private Integer bId = 0; public BImpl() { super(); } public Integer getBId() { return bId; } public void setBId(Integer bId) { this.bId = bId; } } That's all folks!

    Hibernate JIRA | 10 years ago | Adam T
    org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl
  2. 0

    I have found a bug in Hibernate when mixing annotations and mapping files. I have generated a minimal test case. Additionally, I have confirmed that the error does not appear when all classes use annotations. There are two mapped classes AImpl and BImpl. AImpl extends BImpl. A third annotated class ZImpl, refers to BImpl through a ManyToOne relationship. All method signatures refer to these classes by their interfaces A, B, and Z; however, Hibernate is instructed to use the *Impl classes when necessary. I am NOT mixing annotations and mappings in a single inheritance hierarchy. All classes implement Serializable by extending their Serializable interfaces. The minimal test case is attached as a zip and included below: 13:02:57,811 INFO [Version] Hibernate Annotations 3.2.1.GA 13:02:57,827 INFO [Environment] Hibernate 3.2.1 13:02:57,827 INFO [Environment] hibernate.properties not found 13:02:57,842 INFO [Environment] Bytecode provider name : cglib 13:02:57,842 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling 13:02:57,936 INFO [Configuration] configuring from resource: /hibernate.cfg.xml 13:02:57,936 INFO [Configuration] Configuration resource: /hibernate.cfg.xml 13:02:58,077 INFO [Configuration] Reading mappings from resource : B.hbm.xml 13:02:58,342 INFO [Configuration] Reading mappings from resource : A.hbm.xml 13:02:58,389 INFO [Configuration] Configured SessionFactory: null 13:02:58,655 INFO [HbmBinder] Mapping class: test.AImpl -> A 13:02:58,670 INFO [AnnotationBinder] Binding entity from annotated class: test.ZImpl 13:02:58,702 INFO [EntityBinder] Bind entity test.ZImpl on table Z 13:02:58,780 ERROR [HibernateUtil] Building SessionFactory failed. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56) at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283) at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85) at test.TestMixedMappings.main(TestMixedMappings.java:10) Exception in thread "main" java.lang.ExceptionInInitializerError at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:93) at test.TestMixedMappings.main(TestMixedMappings.java:10) Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl at org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56) at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283) at org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85) ... 1 more hibernate.cfg.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.datasource">java:/comp/env/jdbc/newsoncongress</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <mapping resource="B.hbm.xml" /> <mapping resource="A.hbm.xml" /> <mapping class="test.ZImpl" /> </session-factory> </hibernate-configuration> A.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 --> <hibernate-mapping> <class name="test.AImpl" table="A" proxy="test.A"> <id name="aId" column="aID" type="java.lang.Integer"> <generator class="identity" /> </id> </class> </hibernate-mapping> B.hbm.xml: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 9, 2006 6:27:53 PM by Hibernate Tools 3.2.0.beta7 --> <hibernate-mapping> <joined-subclass name="test.BImpl" table="B" proxy="test.B" extends="test.AImpl"> <key column="aID" /> <property name="bId" column="bID" type="java.lang.Integer" not-null="true" /> </joined-subclass> </hibernate-mapping> A.java: package test; public interface A extends java.io.Serializable { public Integer getAId(); public void setAId(Integer aId); } B.java: package test; public interface B extends A { public Integer getBId(); public void setBId(Integer bId); } Z.java: package test; public interface Z extends java.io.Serializable { public Integer getZId(); public void setZId(Integer zId); public B getB(); public void setB(B b); } AImpl.java: package test; public class AImpl implements A { private static final long serialVersionUID = 1L; private Integer aId = 0; public AImpl() { } public Integer getAId() { return this.aId; } public void setAId(Integer aId) { this.aId = aId; } } BImpl.java: package test; public class BImpl extends AImpl implements B { private static final long serialVersionUID = 1L; private Integer bId = 0; public BImpl() { super(); } public Integer getBId() { return bId; } public void setBId(Integer bId) { this.bId = bId; } } ZImpl.java: package test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Inheritance(strategy = InheritanceType.JOINED) @org.hibernate.annotations.Proxy(proxyClass = Z.class) @Table(name = "Z") public class ZImpl implements Z { private static final long serialVersionUID = 1L; private Integer zId = null; private B b = null; @Id @Column(name = "zID") public Integer getZId() { return zId; } public void setZId(Integer zId) { this.zId = zId; } @ManyToOne(optional = false, targetEntity = BImpl.class, fetch = FetchType.LAZY) @JoinColumn(name = "bID", referencedColumnName = "bID") public B getB() { return b; } public void setB(B b) { this.b = b; } } ... When the AImpl and BImpl classes are annotated, the error disappears. AImpl.java: package test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Inheritance(strategy = InheritanceType.JOINED) @org.hibernate.annotations.Proxy(proxyClass = A.class) @Table(name = "A") public class AImpl implements A { private static final long serialVersionUID = 1L; private Integer aId = 0; public AImpl() { } @Id @Column(name = "aID") public Integer getAId() { return this.aId; } public void setAId(Integer aId) { this.aId = aId; } } BImpl.java: package test; import javax.persistence.Entity; import javax.persistence.Table; @Entity @org.hibernate.annotations.Proxy(proxyClass = B.class) @Table(name = "B") public class BImpl extends AImpl implements B { private static final long serialVersionUID = 1L; private Integer bId = 0; public BImpl() { super(); } public Integer getBId() { return bId; } public void setBId(Integer bId) { this.bId = bId; } } That's all folks!

    Hibernate JIRA | 10 years ago | Adam T
    org.hibernate.AnnotationException: @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl
  3. 0

    Hibernate One To Many Annotation tutorial with example. Hibernate annotations

    viralpatel.net | 3 months ago
    java.lang.ExceptionInInitializerError
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring Boot JTA Error

    Stack Overflow | 2 years ago
    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 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
  6. 0

    query regarding one to many relationship in hibernate

    Stack Overflow | 2 years ago | arjun narahari
    org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.hibernate.arjun3.Author.book

  1. davidvanlaatum 2 times, last 2 months ago
  2. r1chjames 1 times, last 6 months ago
  3. Kawada 108 times, last 7 months ago
  4. tjago 4 times, last 8 months ago
43 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. org.hibernate.AnnotationException

    @OneToOne or @ManyToOne on test.ZImpl.b references an unknown entity: test.BImpl

    at org.hibernate.cfg.FkSecondPass.doSecondPass()
  2. Hibernate
    Configuration.buildSessionFactory
    1. org.hibernate.cfg.FkSecondPass.doSecondPass(FkSecondPass.java:56)
    2. org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:428)
    3. org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:286)
    4. org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
    4 frames
  3. org.illumen.hibernate
    HibernateUtil.<clinit>
    1. org.illumen.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:85)
    1 frame
  4. test
    TestMixedMappings.main
    1. test.TestMixedMappings.main(TestMixedMappings.java:10)
    1 frame