【问题标题】:Hibernate getting NULL in foreign key @oneToone mappingHibernate 在外键 @oneToone 映射中获取 NULL
【发布时间】:2020-01-10 13:51:22
【问题描述】:

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

Smartlist -> smartlistId`是PK

AccountEmailing -> smartlistId FK,PK

AccountEmailing 表可能没有初始引用记录,但后来它可能有记录

我只使用 smartlist 表存储库

我试过cascade.ALL,但在 FK 表 id 中得到了 null

我已尝试使用以下组合的以下代码,

  • smartlistAccountEmailing 的初始数据

这很有效

  • 带有智能列表的初始数据,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 stackoverflow.com/questions/59282221/…
  • *我试过下面的代码*:哪个代码?发布代码,准确说明您希望它做什么,以及它会做什么。

标签: java mysql hibernate jpa


【解决方案1】:

使用这些修改过的实体。您需要@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 使用父实体的主键值作为子实体的主键。

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 1970-01-01
    • 2021-11-20
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多