0

关于继承,我有一个关于休眠的问题。如果我有以下课程:

@Entity
@Table(name = "people")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class Person

@Entity
@Table(name="teachers")
@DiscriminatorValue("t")
public class Teacher extends Person 

@Entity
@Table(name="students")
@DiscriminatorValue("s")
public class Student extends Person

然后我想做的是在下面的类中处理所有这些:

public class Course {

...
@ManyToMany
private List<Person> students;
...

}

我希望能够将学生列表一般地视为列表而不是列表,这样学生或老师就可以成为学生。无论如何我可以这样做并且仍然让它们作为学生/教师对象持续存在吗?hibernate“聪明”是否足以找出真正的类?hibernate有这个能力吗?

4

1 回答 1

0

Your configuration is just fine, Hibernate can handle this. Whenever an instance of Student is persisted, Hibernate will generate two insert statements, one for the people table and another for the students table. You can do this for example:

Person p = new Student();
session.persist(p);

Nothing changes when there is an association. For example:

Course c = new Course();
c.students = new ArrayList<Person>();

Person p = new Student();
s.persist(p);

c.people.add(p);
session.persist(c);

PS: You might want to change Course.students to Course.people because that list can hold Teacher instances also.

于 2013-06-21T16:52:33.537 回答