【发布时间】:2018-01-26 07:58:49
【问题描述】:
我在 Hibernate 中苦苦挣扎,找不到可行的解决方案来做我想做的事。顺便说一句,这是一个网络服务。
简短说明: 我有一个必须嵌入主对象(Projet)中的对象(Composant)列表。我想将两者都保存在我的数据库(PostgreSQL)中。为此,我将它们设置如下:
我的第一个表名为 T_PROJET 它包含标记为 PRJ_NAME 的 id 我在 ComponentList 上苦苦挣扎的列表 和一个到期日 PRJ_DATE
对于较小的对象,该表被命名为 T_COMPOSANT。 得到一个自动生成的 id:COMP_ID。 名称 COMP_NOM 一个值 COMP_MONTANT 和优先 COMP_IMPORTANCE
在我的 HMI 上,我可以创建一个包含至少一个 Composant 的 Projet,但我可以添加更多(一个 Projet 最多 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
}
}
这是“Projet”的主要对象:
@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;
}
}
还有“合成物”:
@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 的书,但没有答案,或者我无法正确阅读)。 关于我应该寻找什么的任何想法?
感谢您的帮助!
【问题讨论】:
标签: java postgresql hibernate jpa jakarta-ee