休眠中具有相同主键的一对一单向映射

休眠中具有相同主键的一对一单向映射

本文介绍了休眠中具有相同主键的一对一单向映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有一对一关系的类/表(元文件和行计数).Rowcount 仅适用于某些元文件.目前我有这个设置(简化):

I have two classes/tables (Metafile and RowCount) with a one to one relation. Rowcount only applys to certain metafiles. Currently I have this set up (simplified):

@Entity
@Table(name = "file")
public class MetaFile {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "file_id")
    private int fileId;
    // getters, setters, constructors...
}

RowCount.java

@Entity
@Table(name = "row_count")
public class RowCount implements Serializable {

    @OneToOne
    @JoinColumn(name="file_id")
    private MetaFile file;

    @Id
    @Column(name="file_id")
    private int file_id; // duplicate field needed so that crudrepo would recognise the id

    private int rows;

    public RowCount(MetaFile file, int rows) {
        this.file = file;
        this.rows = rows;
        this.file_id = file.getFileId();
    }
    // getters, setters...
}

我使用 crudrepositories 来简化持久性.

I'm using crudrepositories for both to simplify persistance.

我首先保存图元文件以获得分配的 ID,然后我使用该图元文件和新 ID 创建一个 rowcount 对象并保存它(如下所示).然而,第二次保存失败,因为元文件没有立即保存到数据库中,并且外键约束失败.

I first save the metafile to get an ID assigned, then I create a rowcount object using that metafile with the new ID and save it (shown below). This second save fails however, as the metafile isn't persisted to the database immediately, and the foreign key constraint fails.

metaFile = fileRepository.save(metaFile);

rowCountRepository.save(new RowCount(metaFile,getNumberOfRows(file));

元文件肯定会获得分配给它的有效 ID.有没有办法确保这两个持续发生?

The metaFile definitely gets a valid id assigned to it. Is there a way to make sure these two persists happen in order?

谢谢!

推荐答案

您可以按照 Hadi J 的建议使用 @mapsId 更改您的映射,以便在 RowCount 中具有单个 pk/fk 列

You can change your mapping with @mapsId as suggested by Hadi J to have a single pk/fk column in RowCount

@Entity
@Table(name = "row_count")
public class RowCount implements Serializable {

   @OneToOne
   @JoinColumn(name = "file_id")
   @MapsId
   private MetaFile file;

   @Id
   @Column(name="file_id")
   private int file_id;

   private int rows;

   public RowCount(MetaFile file, int rows) {
       this.file = file;
       this.rows = rows;
   }
   // getters, setters...

}

我会使用双向关系来简化保存:

I would use a bidirectional relationship to simplify saving:

@Entity
@Table(name = "file")
public class MetaFile {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "file_id")
    private int fileId;
    // getters, setters, constructors...

    @OneToOne(cascade = {CascadeType.ALL}, mappedBy = "file")
    private RowCount rowCount;
}

这样你就可以设置关系并保存

This way you can just set the relationship and save

RowCount rowCount = new RowCount(metaFile, getNumberOfRows(file));
metaFile.setRowCount(rowCount);
metaFile = fileRepository.save(metaFile);

这篇关于休眠中具有相同主键的一对一单向映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:27