如何与重复列保持继承关系

时间:2014-07-06 04:36:36

标签: java hibernate jpa

我有继承关系的代码:

@Entity
@Table(name = "PERSON")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "PERSON_TYPE", discriminatorType = DiscriminatorType.STRING, length = 4)

public abstract class Person implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ID_PERSON_ID")
private Long id;

...other attributes
}

@Entity
@Table(name="STUDENT")
@DiscriminatorValue(value = "STUD")
public class Student extends Person implements Serializable{

@OneToMany(targetEntity = ListingRatings.class, mappedBy = "student", cascade=CascadeType.ALL, orphanRemoval=true)
private List<ListingRatings> listingRatings;

...other attributes

}

@Entity
@Table(name="LISTING_RATING")
public class ListingRatings implements Serializable{

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID_LISTING_RATING_ID")
private Long id;

@ManyToOne(targetEntity = Teacher .class, optional = false )
@JoinColumn(name = "ID_PERSON_ID", referencedColumnName = "ID_PERSON_ID",                      insertable=true , updatable=true )
private Teacher teacher;

@ManyToOne(targetEntity = Student.class, optional = false )
@JoinColumn(name = "ID_PERSON_ID", referencedColumnName = "ID_PERSON_ID")
private Student student;

...other attributes
}

@Entity
@Table(name="TEACHER")
@DiscriminatorValue(value="TEAC")
public class Teacher extends Person implements Serializable{

@OneToMany(targetEntity = ListingRatings .class, mappedBy = "teacher",    fetch=FetchType.EAGER)
private List<ListingRatings> listingRatings ;

}

当我试图坚持这种关系时,我有这个例外

引起:org.hibernate.MappingException:实体映射中的重复列: cl.programarte.gym.model.ListingRatings column:ID_PERSON_ID(应使用insert =&#34; false&#34; update =&#34; false&#34;}进行映射。

我不知道这是否可行。 Changing,insertable = true,updatable = true to insertable = false,updatable = false,一切正常。 但是我需要坚持老师的身份,但事实并非如此。

也许可以为这种情况制作一个复合键? 它推荐给你?

非常感谢。

0 个答案:

没有答案