2012-07-11 69 views
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是普通人的財產。

處理這類問題的正確方法是什麼?

回答

0

你試過的東西不行。如果您在呼叫者和位置之間存在關聯,則必須在呼叫者中定義關聯,而不是在人員中定義。員工也一樣。而我認爲你還需要兩個不同的連接表。

+0

嗯..所以基本上我必須重複位置屬性在僱員和來電班... – 2012-07-11 10:49:55

+0

就是這樣。儘管如此,我會避免在兩個類中使用相同的名稱,因爲如果你這樣做,你會在查詢中遇到問題。 – 2012-07-11 10:52:03

+0

這將不是很好,因爲我有多個關係,我希望映射到這個人,以避免確切的那種問題:( – 2012-07-11 12:30:42