如何使用Hibernate映射这些实体?需要绘制一到许多关系。外键没有更新

发布于 2025-01-29 05:40:03 字数 1258 浏览 1 评论 0 原文

在这种情况下,我有2个实体(用户表和uploadRecord表)。我需要绘制一对多关系,因为一个用户可以拥有许多上传记录。我需要将UserID用作用户表中的主要密钥,而外键则作为UploadRecord表。

我尝试使用此代码,但是UploadRecord表FK_Userid未更新。如何解决此问题?

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_UserId", referencedColumnName = "UserId")
    private List<UploadRecord> uploadRecord;

我编写了用户实体类和UploadRecord实体类,如下所示。

@Entity
@Table(name = "users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name= "UserId")
    private Long UserId;

    @Column(nullable = false, unique = true, length = 45)
    private String email;

    @Column(name = "fullName", nullable = false, length = 20)
    private String fullName;



    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_UserId", referencedColumnName = "UserId")
    private List<UploadRecord> uploadRecord;

//Getters and setters
@Entity
@Table(name = "uploadrecord")
public class UploadRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long uploadRecordId;

    @Column(nullable = false, unique = false, length = 1000)
    private String fileName;

//Getters and setters


In this case, I have 2 entities (Users table and UploadRecord table). I need to map a one-to-many relationship because one user can have many upload records. I need to use UserId as the primary key in the Users table and a foreign key as the UploadRecord table.

I tried using this code but the UploadRecord table fk_UserId is not updating. How to fix this issue?

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_UserId", referencedColumnName = "UserId")
    private List<UploadRecord> uploadRecord;

I wrote Users entity class and UploadRecord entity class as follows.

@Entity
@Table(name = "users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name= "UserId")
    private Long UserId;

    @Column(nullable = false, unique = true, length = 45)
    private String email;

    @Column(name = "fullName", nullable = false, length = 20)
    private String fullName;



    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "fk_UserId", referencedColumnName = "UserId")
    private List<UploadRecord> uploadRecord;

//Getters and setters
@Entity
@Table(name = "uploadrecord")
public class UploadRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long uploadRecordId;

    @Column(nullable = false, unique = false, length = 1000)
    private String fileName;

//Getters and setters


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2025-02-05 05:40:03

看来您尚未完成对这两个实体之间的关系进行建模。

像这样编辑您的模型:

用户:

@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
private List<UploadRecord> uploadRecords;

uploadRecord:

@ManyToOne
@JoinColumn(name = "userId")
private User user;

更多有关建模关系的详细信息: baeldung >

此外,请密切关注命名约定:

  • 用户ID-&gt; UserId
  • UploadRecord - &gt; uploadRecords(列表,sets,...) - &gt;复数

It seems you haven't finished modelling the relationship between these two entities.

Edit your models like this:

User:

@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
private List<UploadRecord> uploadRecords;

UploadRecord :

@ManyToOne
@JoinColumn(name = "userId")
private User user;

More details for modelling relations: Baeldung

Moreover keep an eye on naming convention:

  • UserId -> userId
  • uploadRecord -> uploadRecords (Lists, Sets, ...) -> plural
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文