java - Hibernate 在外键 @oneToone 映射中获取 NULL

标签 java mysql hibernate jpa

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

Smartlist -> smartlistId` 是 PK

AccountEmailing -> smartlistId FK,PK

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

我只使用 smartlist 表存储库

我尝试过 cascade.ALL 但 FK 表 id 为空

我尝试了以下代码,该代码与以下组合一起使用,

  • initial data with smartlist and AccountEmailing

这是有效的,我在两个表中都获得了一条记录,但后来更新了一条记录,给我一个错误,因为 AccountEmailing 已经有一个条目(CascadeType.PERSIST 只允许插入,不允许子项更新)

  • initial data with smartlist and no data in 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 以及 smartlist 实体中的 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 使用父实体的主键值作为子实体的主键。

关于java - Hibernate 在外键 @oneToone 映射中获取 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59683055/

相关文章:

java - 没有 OutOfMemory 异常的内存泄漏?

java - 我正在向名为“狐狸和猫头鹰”的狐狸和兔子模拟添加类,但我在“猫头鹰”类中遇到问题

phpMyAdmin: "URI too large"

java - 使用 JPA 没有列表/集合的 ManyToOne 关系

java - 如何在 Java Swing 中使 JScrollPane 透明

java - Java中如何让ThreadPoolExecutor立即执行

mysql - 在 Datagridview 中以格式显示数据

mysql - 从索引中删除元素时,键 '1-1110' 的重复条目 'INDEX'

java - 如何保留父实体而不更新具有多对多关系的子实体?

java - 从 Hibernate 更新获取 SQL 脚本