0

I have a mapping problem with JPA

@Entity
@Table(name = "TBL_A")
public class EntityA {

  // Other mappings

  @OneToMany(mappedBy = "entityA")
  private List<EntityBImpl> bEntities;

  @OneToMany(mappedBy = "entityA")
  private List<AnotherEntityBImpl> anotherBEntities;
}

@Entity
@DiscriminatorValue("X")
public class EntityBImpl extends AbstractEntityB {

  // Other mappings
}

@Entity
@DiscriminatorValue("Y")
public class AnotherEntityBImpl extends AbstractEntityB {

  // Other mappings
}


@Entity
@Table(name = "TBL_B")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "PROJ_TYPE")
public abstract class AbstractEntityB {

  // Other mappings

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ENTITY_A_ID")
  private EntityA               entityA;
}

It throws the following exception at runtime :

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.example.EntityBImpl.entityA in com.example.EntityA.bEntities

How to solve the problem without having to change the inheritance strategy ?

jerome
  • 2,029
  • 4
  • 38
  • 58
  • probably answered here https://stackoverflow.com/questions/4011472/mappedby-reference-an-unknown-target-entity-property (but hard to tell because of incomplete code) – Jacek Cz Aug 22 '17 at 16:18
  • I read It but it uses @MapedSuperclass so it will not be the same BDD structure, it will be with multiple tables and the user of join. I want if possible keep a single table. – jerome Aug 22 '17 at 16:36
  • 1
    If every element of the list is supposed to be of type EntityBImpl, why is entityA not defined in EntityBImpl? Suppose you have AnotherEntityB, also extending AbstractEntityB, and referring to EntityA: how could is be stored in the list, since it's not of typeEntityBImpl. Make the list a List, or put the ManyToOne association in EntityBImpl. – JB Nizet Aug 22 '17 at 16:37
  • Good point, I have updated my snippet of code now with AnotherEntityB, how this will work ? Will JPA able to construct the lists bEntities and anotherBEntities ? – jerome Aug 22 '17 at 19:14

0 Answers0