Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on <entity> references an unknown entity

I am receiving the following Hibernate 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 the problem with various people, having read every possible blog or forum, I am really getting desperate here.

This is a mid-sized project. I should mention the database is Postgres 9.1 and we generate the DB using a modelling tool. Hibernate connects to the database but does not generate it.

I have created a new entity in the database, it's called "KonfiguracniTemplate" (configuration template). I have created the model, controller, form, validators, .jsp's, all basically copied 1:1 from an existing entity of a similar nature. I can now work with KonfiguracniTemplate, CRUD is fully working.

The problem comes when I reference this KonfiguracniTemplate from the entity called Switchport. In the DB there is a relation between the two:

  • Switchport 1:1 ... 0:N KonfiguracniTemplate (switchport always references a KonfiguracniTemplate; a KonfiguracniTemplate MAY BE referenced zero or more times)
  • Switchport has FK konfiguracniTemplateAccess_id for this relation.

In .../model/Switchport.java the relation is mapped just like all other relations that are working:

@ManyToOne
@JoinColumn(nullable = false)
private KonfiguracniTemplate konfiguracniTemplateAccess;

I have 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 have also checked:

  • both entities are in the same package
  • they are both annotated "@Entity" using "import javax.persistence.Entity;"
  • the build produces no error/warning messages
  • as long as the reference in Switchport is commented out, everything is fine

No matter what I try I cannot get rid of the "references an unknown entity" exception. Can somebody please share an idea what is happening or maybe how to debug the issue? The stacktrace at the top of the post is all I get in the logs.

All input is greatly appreciated!

like image 968
Martin Avatar asked Sep 08 '11 14:09

Martin


2 Answers

Just add the class Team to the "hibernate-cfg.xml" file, because Hibernate doesn't identify without adding into it.

like image 164
Chintan Panchal Avatar answered Sep 30 '22 06:09

Chintan Panchal


Possible Solutions:

1) Ensure that the entity has been appropriately referenced in hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    ... 
    <mapping class="com.project.entitytwo.model.EntityTwo"/>
    ...
</session-factory>

2) Ensure that @Entity has been specified at the class-level ( at the top of the class )

@Entity
@Table( name="ENTITY_TWO" )
public class EntityTwo extends AnyClass
{
    ...
like image 33
Shoaib Chikate Avatar answered Sep 30 '22 06:09

Shoaib Chikate