Org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> refers to an unknown object
I get the following hibernation exception:
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on cz.rohan.dusps.model.Switchport.konfiguracniTemplateAccess references an unknown entity: cz.rohan.dusps.model.KonfiguracniTemplate org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103) org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541) org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523) org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380) org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377) org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) cz.rohan.dusps.helper.SessionFactoryHelper.initFactory(SessionFactoryHelper.java:122) cz.rohan.dusps.helper.SessionFactoryHelper.getSessionFactory(SessionFactoryHelper.java:134) cz.rohan.dusps.filter.HistorieZmenFilter.doFilter(HistorieZmenFilter.java:102) cz.rohan.dusps.filter.CharsetFilter.doFilter(CharsetFilter.java:41) after ~ 20 hours spent on a problem with different people, having read all the possible blogs or forums, I really despaired here.
This is a medium sized project. I must mention that the database is Postgres 9.1, and we create the database using a modeling tool. Hibernate connects to the database but does not generate it.
I created a new object in the database called "KonfiguracniTemplate" (configuration template). I created a model, controller, form, validators, .jsp, all basically copied 1: 1 from an existing entity of a similar nature. Now I can work with KonfiguracniTemplate, CRUD fully works.
The problem occurs when I refer to this KonfiguracniTemplate from an object called Switchport. There is a connection between the two in the database:
- Switchport 1: 1 ... 0: N KonfiguracniTemplate (switchport always refers to KonfiguracniTemplate, KonfiguracniTemplate MAY be a reference to zero or more)
- The switch has an FK konfiguracniTemplateAccess_id for this relationship.
In ... / model / Switchport.java, the relation is displayed in the same way as all other relations that work:
@ManyToOne @JoinColumn(nullable = false) private KonfiguracniTemplate konfiguracniTemplateAccess; I tried various forms:
@ManyToOne @JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false) private KonfiguracniTemplate konfiguracniTemplateAccess; or
@ManyToOne(targetEntity=KonfiguracniTemplate.class) @JoinColumn(name="konfiguracnitemplateaccess_id", nullable = false) private KonfiguracniTemplate konfiguracniTemplateAccess; I also checked:
- both objects are in the same package
- they are both annotated with "@Entity" using "import javax.persistence.Entity;"
- assembly does not generate error / warning messages
- as long as the link in Switchport is not commented out, everything is in order
No matter what I try, I can not get rid of the "reference to an unknown entity" exception. Can someone please share an idea of ββwhat is happening, or maybe how to debug the problem? The stop line at the top of the message is all that I get in the logs.
All input is welcome!
Possible solutions:
1) Make sure the object was appropriately specified in the hibernate.cfg.xml file
<hibernate-configuration> <session-factory> ... <mapping class="com.project.entitytwo.model.EntityTwo"/> ... </session-factory> 2) Make sure @Entity is indicated at the class level (at the top of the class)
@Entity @Table( name="ENTITY_TWO" ) public class EntityTwo extends AnyClass { ...