我在两个类之间有这样的关系:

Smartlist-> smartlistId`是PK

AccountEmailing-> smartlistId FK,PK

AccountEmailing表可能没有初始参考记录,但以后可能有记录

我只使用智能列表表存储库

我试过cascade.ALL但在FK表ID中为null

我已经尝试了以下与以下组合配合使用的代码,



  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以及智能列表实体中的设置器。

@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使用父实体的主键值作为子实体的主键。

10-08 19:38