【问题标题】:jpa association children entitiesjpa协会儿童实体
【发布时间】:2012-07-11 07:43:41
【问题描述】:

我有一个实体 Person,它是另外两个实体的父级:Caller 和 Employee。这些实体使用带有鉴别器列的 SINGE_TABLE 策略实现:person_id。
我还有另一个实体:与 Person 具有多对多关系的位置。因此,一个人可能属于多个位置,一个位置可以有多个人。
用 manyToMany 映射 Location 和 Person 很容易,但现在我需要一种映射子实体的方法,因为在 location 中我需要一些方法,例如:getEmployees();和 getCallers();
我试过类似的东西:

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.  

我想一种解决方案是将位置向下移动到子级中,但是我必须复制/粘贴一些代码,毕竟位置是普通人的属性。

处理此类问题的正确方法是什么?

【问题讨论】:

    标签: hibernate jpa jpa-2.0


    【解决方案1】:

    您尝试的方法不起作用。如果您在 Caller 和 Location 之间有关联,则必须在 Caller 中定义该关联,而不是在 Person 中。员工也一样。而且我认为您还需要两个不同的连接表。

    【讨论】:

    • hmm.. 所以基本上我必须在员工和呼叫者类中复制 location 属性...
    • 就是这样。不过,我会避免在两个类中使用相同的名称,因为这样做会导致查询出现问题。
    • 这不太好,因为我有多个关系,我希望映射到这个人,以避免恰好出现此类问题:(
    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多