【发布时间】:2014-07-01 08:12:29
【问题描述】:
我使用 Java 和 Hibernate 将我的实体保存在数据库中,但它没有按需要工作。 我有一个实体企业:
@Entity
public class Entreprise{
@Id
private long identifier;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Person> persons;
...
// getters and setters
}
这里的Person实体是其他实体的超类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person{
@Id
private long identifier;
@Column
private String name;
...
// constructors,getter and setter
}
还有我的两个子类:
@Entity
public class Employed extends Person{
@Column
private String actualJob;
// constructors,getter and setter
}
@Entity
public class Unemployed extends Person{
@Column
private String lastJob;
// constructors,getter and setter
}
这是我在数据库中保存实体的方法:
Person person = new Employed();
person.setName("John");
Employed employed = (Employed) person;
employed.setActualJob("electrician");
Entreprise myEntreprise = new Entreprise();
entreprise.getPersons().addPerson(person);
entrepriseDao.save(entreprise);
但是在数据库中,插入只是在表 Entreprise 和 Person 中进行的。 Employed 表中没有插入任何内容。我不知道为什么有人可以解释一下吗?
我尝试下面的代码也不行:
Employed employed = new Employed();
employed.setActualJob("electrician");
employed.setName("John");
Entreprise myEntreprise = new Entreprise();
entreprise.getPersons().addPerson(employed);
entrepriseDao.save(entreprise);
我不明白为什么即使我使用子类来设置数据它也不起作用。
这里是ddl
Hibernate: insert into Entreprise_Person (Entreprise_identifier, person_identifier) values (?, ?);
【问题讨论】:
-
请显示 SQL 的 DDL。
标签: java hibernate inheritance