【问题标题】:Java Spring: ManytoOne EmbeddedId in child class with additional Id - getting null error on child specific PKJava Spring:具有附加 ID 的子类中的 ManytoOne EmbeddedId - 在特定于子的 PK 上出现空错误
【发布时间】:2021-11-11 18:53:24
【问题描述】:

我能够通过适用于 GET 请求的复合 pk 获得这种多对一关系,但在保存新条目时遇到问题。

我有一个带有嵌入 ID 的父实体。

@Embeddable
public class AnnualServiceHistoryPK implements Serializable {

    @Column(name = "year", columnDefinition = "int(4)")
    Integer year;
    @Column(name = "month", columnDefinition = "char(3)")
    String month;
    @Column(name = "route", columnDefinition = "varchar(32)")
    String route;

这是放在父实体中的:

@Entity(name = "AnnualServiceHistory")
@Table(name = "annual_service_history")
public class AnnualServiceHistory extends Auditable<String> implements Serializable 
{ 
@EmbeddedId
AnnualServiceHistoryPK annualServiceHistoryPK;

... other variables

    @OneToMany(mappedBy = "annualServiceHistory", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    List<AnnualServiceHistoryNonMachine> annualServiceHistoryNonMachineList;

我有一个子实体,它从父实体映射嵌入的 id 并添加和额外的本地 PK。

public class AnnualServiceHistoryNonMachine extends Auditable<String> implements Serializable {

    @Id
    @MapsId
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "year", referencedColumnName = "year")
    @JoinColumn(name = "month", referencedColumnName = "month")
    @JoinColumn(name = "route", referencedColumnName = "route")
    private AnnualServiceHistory annualServiceHistory;

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "toy_inventory", referencedColumnName = "id")
    ToyInventory toyInventory;

... other variables

我的问题是,当我在级联过程中出于某种原因将父级与子级列表一起保存时,当它生成子密钥时,它只是从 MapsId 部分创建密钥并抱怨 toyInventory 密钥为空.我不确定为什么它会丢失这个 ID,因为在执行存储库保存之前,子类在对象中有 toyInventory 键。

这是我收到的错误: "java.sql.SQLIntegrityConstraintViolationException: 列 'toy_inventory' 不能为空"

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    找到解决方案。 - https://medium.com/@bhagyajayashani/composite-key-handling-using-idclass-annotation-in-spring-boot-java-26f40bbd38a2

    从使用@embeddedId 更改为使用@classId。然后创建一个单独的连接对象,并基于单个变量而不是 EmbeddedId 创建连接。

    @Embeddable
    public class AnnualServiceHistoryPK implements Serializable {
    
        Integer year;
        String month;
        String route;
    
        public AnnualServiceHistoryPK() {
        }
    
    @Entity(name = "AnnualServiceHistory")
    @Table(name = "annual_service_history")
    @IdClass(AnnualServiceHistoryPK.class)
    public class AnnualServiceHistory extends Auditable<String> implements Serializable {
    
        @Id
        @Column(name = "year", columnDefinition = "int(4)")
        Integer year;
    
        @Id
        @Column(name = "month", columnDefinition = "char(3)")
        String month;
    
        @Id
        @Column(name = "route", columnDefinition = "varchar(32)")
        String route;
    
    ... other variables ...
    
        @OneToMany(mappedBy = "annualServiceHistory", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
        Set<AnnualServiceHistoryNonMachine> annualServiceHistoryNonMachineList;
    
    
    @Embeddable
    public class AnnualServiceHistoryNonMachinePK implements Serializable {
    
        Integer year;
        String month;
        String route;
        ToyInventory toyInventory;
    
        public AnnualServiceHistoryNonMachinePK() {
        }
    
    @Entity(name = "AnnualServiceHistoryNonMachine")
    @Table(name = "annual_service_history_non_machine")
    @IdClass(AnnualServiceHistoryNonMachinePK.class)
    public class AnnualServiceHistoryNonMachine extends Auditable<String> implements Serializable {
    
        @Id
        @Column(name = "year", columnDefinition = "int(4)")
        Integer year;
    
        @Id
        @Column(name = "month", columnDefinition = "char(3)")
        String month;
    
        @Id
        @Column(name = "route", columnDefinition = "varchar(32)")
        String route;
    
    ... other variables ...
    
        @ManyToOne
        @JoinColumns({
                @JoinColumn(name = "year", referencedColumnName = "year", insertable = false, updatable = false),
                @JoinColumn(name = "month", referencedColumnName = "month", insertable = false, updatable = false),
                @JoinColumn(name = "route", referencedColumnName = "route", insertable = false, updatable = false)
        })
        @JsonIgnore
        private AnnualServiceHistory annualServiceHistory;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多