我正在努力與Hibernate,並找不到工作的解決方案來做我想做的。這是一個web服務btw。休眠/ @ElementCollection和@XmlRootElement
簡短說明: 我有一個對象(Composant)的列表,它必須嵌入到主對象(Projet)中。我想堅持我的數據庫(PostgreSQL)。要做到這一點我將它們設置爲:
我的第一臺被命名爲T_PROJET 它containes標記爲PRJ_NAME 列表中的ID,我掙扎在ComponentList 和過期日期PRJ_DATE
而爲該表被命名爲T_COMPOSANT。 有一個自動生成的ID:COMP_ID。 的名稱COMP_NOM 值COMP_MONTANT 和優先COMP_IMPORTANCE
在我的HMI,我可以創造一個謨與至少一個Composant但我可以添加更多(最多5 Composant一個謨),以及Composants會保存爲列表。
這裏是什麼,我往往會產生對我的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和@Embeddable註釋的東西,但無法弄清楚究竟在哪裏(經過了我的Antonio Goncalves的書,但沒有回答,或者我無法正確閱讀)。 任何想法,我應該找什麼?
感謝您的幫助!
感謝您的回答!它適用於第二種解決方案。 – raik