我在Hibernate中苦苦挣扎,找不到可以解决我想要的工作的解决方案。顺便说一句,这是一个Web服务。

简短说明:
我有一个对象列表(Composant),必须将其嵌入主对象(Projet)中。我想将两者都保留在我的数据库(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),并且这些Composant将保存为列表。

这是我倾向于在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;
}
}


和“ 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的书,但没有答案,或者我只是无法正确阅读)。
关于我应该寻找什么的任何想法?

谢谢你的帮助!

最佳答案

@ElementCollection / @CollectionTable可能与@Embedded / @Column冲突。

@Embedded定义一个嵌入式对象,其中@ElementCollection定义一个嵌入式对象列表。

@Column定义一列,其中@CollectionTable定义集合的联接。

如果您不需要Composant作为实体:


@Embedded删除@ColumngetComposants()
@Table中删除​​Composant(已经在@CollectionTable中声明了)


如果您需要Composant作为实体:


@ElementCollection删除@CollectionTable@Embedded@ColumnProjet#getComposants()
@Embeddable删除Composant
@Entity添加到Composant
将注释添加到getComposants()


--

@ManyToMany
@JoinTable(
    name = "T_PROJET_COMPOSANT",
    joinColumns = @JoinColumn(name = "PROJ_ID"),
    inverseJoinColumns = @JoinColumn(name = "COMP_ID"),
)

07-28 11:56