我正在与Hibernate作斗争,找不到一个可行的解决方案来做我想做的事。这是一个网络服务btw。
我有一个对象列表(Composant),它必须嵌入到主对象(Projet)中。我想在我的数据库(PostgreSQL)中持久化这两者。为此,我将其设置为:
我的第一个表名为T_PROJET
我在ComponentList上苦苦挣扎的列表
和到期日PRJ_date
获得了自动生成的id:COMP\u id。
A值COMP\u MONTANT
在我的HMI上,我可以用至少一个组件创建一个项目,但我可以添加更多组件(一个项目最多5个组件),组件将保存为列表。
以下是我倾向于在HMI上生成的JSON示例:
{
"name":"Example",
"dateLimite":"2018-08-08",
"composant_1": {
"name":"Vol",
"montant": 1200,
"importance":1
},
"composant_2": {
"name":"Truc",
"montant": 1200,
"importance":1
},
"composant_3": {
"name":"Bidule",
"montant": 1200,
"importance":1
},
"composant_4": {
"name":"Machin",
"montant": 1200,
"importance":1
},
"composant_5": {
"name":"Wesh",
"montant": 1200,
"importance":1
}
}
@Entity
@Table(name = "T_PROJET")
@XmlRootElement
public class Projet implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private List<Composant> composants;
private Date dateLimite;
public Projet() {
super();
}
public Projet(String name, List <Composant> composants, Date dateLimite) {
this.name = name;
this.composants = composants;
this.dateLimite = dateLimite;
}
@Id
@Column(name = "PRJ_NOM", nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ElementCollection
@CollectionTable(name="T_COMPOSANT", joinColumns = @JoinColumn(name = "COMP_ID"))
@Embedded
@Column(name="COMP_ID", nullable = true)
public List<Composant> getComposants() {
return composants;
}
public void setComposants(List <Composant> composants) {
this.composants = composants;
}
@Column(name="PRJ_DATE")
public Date getDateLimite() {
return dateLimite;
}
public void setDateLimite(Date dateLimite) {
this.dateLimite = dateLimite;
}
}
和“Composant”:
@Embeddable
@Table(name = "T_COMPOSANT")
@XmlRootElement
public class Composant implements Serializable {
private static final long serialVersionUID = 1L;
private int composant_id;
private String name;
private int montant;
private int importance;
public Composant(){
super();
}
public Composant(final int composant_id, final String name, final int montant, final int importance) {
this.composant_id = composant_id;
this.name = name;
this.montant = montant;
this.importance = importance;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "COMP_ID")
public int getComposant_id() {
return composant_id;
}
public void setComposant_id(int composant_id) {
this.composant_id = composant_id;
}
@Column(name = "COMP_NOM",nullable = true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="COMP_MONTANT", nullable = true)
public int getMontant() {
return montant;
}
public void setMontant(int montant) {
this.montant = montant;
}
@Column(name = "COMP_IMPORTANCE", nullable = true)
public int getImportance() {
return importance;
}
public void setImportance(int importance) {
this.importance = importance;
}
}
我确信我错过了一些关于@ElementCollection和@embeddeble注释的内容,但我不知道具体在哪里(浏览了Antonio Goncalves的书,但没有答案,或者我就是看不懂)。
谢谢你的帮助!