Home > Java > AnnotationException : Entity X References An Unknown Entity Y

AnnotationException : Entity X References An Unknown Entity Y

Problem: I was getting an org.hibernate.AnnotationException when creating a bidirectional one-to-one relationship between 2 entities (EntityOne and EntityTwo).

Relevant Section Of StackTrace:
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.xyz.domain.EntityOne.anImage references an unknown entity: com.xyz.domain.EntityTwo
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103)
at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1449)

Relevant Section Of EntityOne’s Code:

@Entity
@Table(name = "entity1")
public class EntityOne implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "e1_id")
  private Long id;
 
  @OneToOne(fetch = FetchType.LAZY, optional = true)
  @JoinTable(name = "entity1_entity2_map",
    joinColumns = @JoinColumn(name = "e1_id"),
    inverseJoinColumns = @JoinColumn(name = "e2_id"))
  private EntityTwo anImage;
 
  //Other code
}

Relevant Section Of EntityTwo’s Code:

@Entity
@Table(name = "entity2")
public class EntityTwo implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "e2_id")
  private Long id;
 
  @OneToOne(mappedBy = "anImage", optional = true)
  private EntityOne e1;
 
  //Other code
}

Solution That Worked For Me:
Check the persistence.xml to ensure that the 2 entities (EntityOne and EntityTwo) are listed. Previously I had not included EntityTwo, which is why I kept getting the message com.xyz.domain.EntityOne.anImage references an unknown entity: com.xyz.domain.EntityTwo

    com.xyz.domain.EntityOne
    com.xyz.domain.EntityTwo
    <!-- other configurations -->
Categories: Java Tags: ,
  1. No comments yet.
  1. No trackbacks yet.