簡體   English   中英

Hibernate 在外鍵 @oneToone 映射中獲取 NULL

[英]Hibernate getting NULL in foreign key @oneToone mapping

我在兩個類之間有這樣的關系:

Smartlist -> smartlistId` 是 PK

AccountEmailing -> smartlistId FK,PK

AccountEmailing 表可能沒有初始參考記錄,但后來可能有記錄

我只使用 smartlist 表存儲庫

我曾嘗試過cascade.ALL但在FK表ID中獲得空值

我已經嘗試了以下代碼,它使用以下組合,

  • 與初始數據smartlistAccountEmailing

這是有效的,我在兩個表中都有一條記錄,但后來更新了一條記錄,給我一個錯誤,因為 AccountEmailing 已經有一個條目( CascadeType.PERSIST只允許插入而不是更新子項)

  • 帶有智能列表的初始數據,而 AccountEmailing 中沒有數據

意味着它不會在 AccountEmailing 中創建一個條目,但稍后會添加一條記錄,它會為 AccountEmailing 創建一個插入語句,並且從下次它總是更新

我正在尋找一種解決方案,如何更新我的課程,以便我可以在 AccountEmailing 上執行 CRUD:

public class Smartlist {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "smartlistId")
    private Integer smartlistId;

    @OneToOne( mappedBy = "smartlist", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST, orphanRemoval = true)
    private AccountEmailing accountEmailing;
}

@Entity
@Table(name = "account_emailing")
public class AccountEmailing implements Serializable {

    @Id
    @OneToOne
    @JoinColumn(name = "smartlistId")
    private Smartlist smartlist;

}

使用這些修改后的實體。您需要@MapsId 以及智能列表實體中的 setter。

@Entity
public class Smartlist {

        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name = "smartlistId")
        private Integer smartlistId;

        @OneToOne( mappedBy = "smartlist", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
        private AccountEmailing accountEmailing;

        String name;

    public Integer getSmartlistId() {
        return smartlistId;
    }

    public void setSmartlistId(Integer smartlistId) {
        this.smartlistId = smartlistId;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addAccountEmail(AccountEmailing emailing)
    {
        accountEmailing=emailing;
        accountEmailing.setSmartlist(this);
    }


}



@Entity
@Table(name = "account_emailing")
public class AccountEmailing implements Serializable {

    @Id
    @Column(name="smartlistId")
    Integer id;

    @MapsId
    @OneToOne
    @JoinColumn(name = "smartlistId")
    private Smartlist smartlist;

    String name;

    public Smartlist getSmartlist() {
        return smartlist;
    }

    public void setSmartlist(Smartlist smartlist) {
        this.smartlist = smartlist;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

使用以下代碼關聯實體

 Smartlist smartlist=new Smartlist();
 smartlist.setName("SmartList");

 AccountEmailing accountEmailing=new AccountEmailing();
 accountEmailing.setName("AccountEmailing");

 smartlist.addAccountEmail(accountEmailing);

 smartListRepo.saveAndFlush(smartlist);

更新時,我們必須從父對象中獲取引用,否則它將無法工作,因為每次都會創建一個新對象

因此,對於上面的插入很好,對於更新以下需要應用

Smartlist smartlist = smartlistRepository.findOne(smartlistDTO.getSmartlistId());
// take an existence reference
AccountEmailing accountEmailing = smartlist.getAccountEmailing();
// perform update on accountEmailing
smartlist.setAccountEmailing(accountEmailing);
smartlistRepository.save(smartlist);

@MapsId 注解告訴 Hibernate 使用父實體的主鍵值作為子實體的主鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM