[英]How to save in database a hierarchy class?
I use Java and Hibernate to save my entities in database but it's not working as needed. 我使用Java和Hibernate将我的实体保存在数据库中,但是无法按需工作。 I have a entity Entreprise :
我有一个实体Entreprise:
@Entity
public class Entreprise{
@Id
private long identifier;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Person> persons;
...
// getters and setters
}
Here the Person entity which a super class of other entities: 这里的Person实体是其他实体的超类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Person{
@Id
private long identifier;
@Column
private String name;
...
// constructors,getter and setter
}
And my two subclasses : 还有我的两个子类:
@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
}
Here my method to save the entity in the database : 这是我将实体保存在数据库中的方法:
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);
But in the database, the insert was just made in the table Entreprise and Person. 但是在数据库中,插入只是在表Entreprise和Person中进行的。 Nothing was inserted in the table Employed.
未在“已使用”表中插入任何内容。 I don't know why if someone could explain me, please ?
我不知道为什么有人可以向我解释?
I try the following code does not also work : 我尝试以下代码也不起作用:
Employed employed = new Employed();
employed.setActualJob("electrician");
employed.setName("John");
Entreprise myEntreprise = new Entreprise();
entreprise.getPersons().addPerson(employed);
entrepriseDao.save(entreprise);
I don't understand why even if i use the subclass to set data it does not work. 我不明白为什么即使我使用子类来设置数据也不起作用。
Here is the ddl 这是DDL
Hibernate: insert into Entreprise_Person (Entreprise_identifier, person_identifier) values (?, ?);
You are adding a person reference to the enterprise´s list: 您正在将个人参考添加到企业列表中:
entreprise.getPersons().addPerson(person);
Doing that, you add only the person´s data, not the extended employed's data. 这样,您仅添加人员的数据,而不添加扩展员工的数据。 You should add the employed object instead.
您应该改为添加所使用的对象。
Try adding @PrimaryKeyJoinColumn(name = "YOUR_SUBCLASS_ID_FIELD_THAT_WILL_MATCH_THE_SUPERCLASS_ID_FIELD")
to all subclasses. 尝试将
@PrimaryKeyJoinColumn(name = "YOUR_SUBCLASS_ID_FIELD_THAT_WILL_MATCH_THE_SUPERCLASS_ID_FIELD")
到所有子类。
The subclasses need identifiers also, this is how they link to their superclass record. 子类也需要标识符,这就是它们链接到其超类记录的方式。
@Entity
@PrimaryKeyJoinColumn(name = "identifier")
public class Employed extends Person{
@Id
private long identifier;
@Column
private String actualJob;
// constructors,getter and setter
}
@Entity
@PrimaryKeyJoinColumn(name = "identifier")
public class Unemployed extends Person{
@Id
private long identifier;
@Column
private String lastJob;
// constructors,getter and setter
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.