0

我没有看到我的错误,在对 stackoverflow 和 Google 进行研究后,我认为代码应该是正确的。但是 Hibernate (spring-boot-starter-data-jpa 2.2.4) 仍然用null.

这是我的OneToMany课:

@Entity
@Table(name = "tablea", schema = "")
public class TableAEntity implements Serializable {

    private static final long serialVersionUID = 7890327260188587351L;

    @EmbeddedId
    private MyId id;
    
    @OneToMany(cascade = ALL, mappedBy = "tableA", orphanRemoval = true, fetch = FetchType.LAZY)
    private List<TableBEntity> tableBentries;
    
    // Getter + Setter
}

我的EmbeddedId班级:

@Embeddable
public class MyId implements Serializable {

    private static final long serialVersionUID = -8267953052238233498L;

    @Column(name = "id")
    private String id;

    @Column(name = "iddate")
    private Date iddate;

    @Column(name = "idint")
    private BigInteger idint;
    
    // Getter + Setter
}

最后是我的ManyToOne课:

@Entity
@Table(name = "tableB", schema = "")
public class TableBEntity implements Serializable {

    private static final long serialVersionUID = -4648090658471459969L;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name = "id", referencedColumnName = "id"),
        @JoinColumn(name = "iddate", referencedColumnName = "iddate"),
        @JoinColumn(name = "idint", referencedColumnName = "idint")
    })
    private TableAEntity tableA;
    
    // Some other attributes
    
    // Getter + Setter
}

如您所见,我想在两个表中使用属性 (ididdate)idint作为组合的主键。

当我创建TableAEntity对象时,我将几个TableBEntity对象添加到tableBentries属性中。并且对于每个TableBEntity我设置对TableAEntity(属性tableA)的引用。当然,MyId(属性id)对象已被填充。

保存对象后TableAEntity,Hibernate 还会存储除TableBEntity字段之外的所有字段ididdate并且idint(所有 JoinColumn 的)是null

任何的想法?

4

1 回答 1

0

似乎是@IdTableBEntity的问题造成的。如果我将它移到另一个属性,它会起作用。

于 2020-09-03T07:36:34.367 回答