Hibernate / @ElementCollection和@XmlRootElement

时间:2017-08-17 18:54:19

标签: java postgresql hibernate jpa java-ee

我正在与Hibernate斗争,无法找到一个可行的解决方案来做我想做的事情。这是一个网络服务btw。

简短说明: 我有一个对象列表(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),并且Composants将保存为List。

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

&#34; Composant&#34; :

@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;
}
}

每次尝试我都会得到一个&#34;在继承状态层次结构中找不到声明类&#34;错误。 我确定我错过了关于@ElementCollection和@Embeddable注释的一些内容,但无法弄清楚究竟在哪里(通过我的Antonio Goncalves&#39;书籍,但没有答案,或者我只是无法正确阅读)。 关于我应该寻找什么的任何想法?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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

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

@Column定义了一个列,其中@CollectionTable定义了集合的连接。

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

  1. @Embedded
  2. 移除@ColumngetComposants()
  3. @Table移除Composant(已在@CollectionTable中声明)
  4. 如果您需要Composant作为实体:

    1. @ElementCollection
    2. 移除@CollectionTable@Embedded@ColumnProjet#getComposants()
    3. @Embeddable
    4. 移除Composant
    5. @Entity添加到Composant
    6. 将注释添加到getComposants()
    7. -

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