0
我有一个实体Person,它是另外两个实体的父对象:Caller和Employee。这些实体使用SINGE_TABLE策略与鉴别器列person_id来实现。
另外我还有另一个实体:与Person有ManyToMany关系的位置。所以,一个人可能属于多个地点,一个地点可能有多个人。
很容易映射位置和人与manyToMany,但现在我需要一种方法来映射子实体,因为在位置我需要一些方法,如:getEmployees();和getCallers();
我想是这样的:jpa协会儿童实体
public class Location implements Serializable, Comparable<Location> {
private static final long serialVersionUID = 1L;
@ManyToMany(mappedBy="locations")
private List<Caller> callers = new ArrayList<Caller>();
@ManyToMany(mappedBy="locations")
private List<Employee> employees = new ArrayList<Employee>();
}
@Entity
@DiscriminatorValue("0")
@Access(AccessType.FIELD)
public class Caller extends Person {
private static final long serialVersionUID = 1L;
@Column(name = "company_name")
private String companyName;
@Column(name = "individual")
private Boolean individual;
}
@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("1")
public class Employee extends Person {
private static final long serialVersionUID = 7526471155622776147L;
}
@Entity
@Access(AccessType.FIELD)
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="person_type",discriminatorType=DiscriminatorType.INTEGER)
@Table(name="persons")
public class Person implements Serializable, Comparable<Person>{
private static final long serialVersionUID = 7526471155622776147L;
@ManyToMany
@JoinTable(name="persons_locations",
joinColumns={@JoinColumn(name="person_id")},
inverseJoinColumns={@JoinColumn(name="location_id")})
private List<Location> locations;
}
,但是当我尝试编译应用程序,我得到这个错误:
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: xxx.entities.yyy.Caller.locations in xxx.vs.entities.yyy.Location.callers.
我想一个解决方案会向下移动位置到孩子,但随后我必须复制/粘贴一些代码,毕竟Location是普通人的财产。
处理这类问题的正确方法是什么?
嗯..所以基本上我必须重复位置属性在雇员和来电班... – 2012-07-11 10:49:55
就是这样。尽管如此,我会避免在两个类中使用相同的名称,因为如果你这样做,你会在查询中遇到问题。 – 2012-07-11 10:52:03
这将不是很好,因为我有多个关系,我希望映射到这个人,以避免确切的那种问题:( – 2012-07-11 12:30:42