作为一对一关系上的键的实体返回“复合ID”错误

发布于 2024-12-27 04:52:32 字数 1755 浏览 2 评论 0原文

我有以下 2 个实体:

   @Entity(name = "Employee")
   @Table(name = "EMPLOYEE")
   public class Employee implements Serializable {
      @Id
      @Column(name = "EMP_ID", insertable = true, updatable = false, nullable = false)
      private int id;

我们

@Entity(name = "Draw")
@Table(name = "DRAW")

public class Draw extends AuditableEntity {
    @Id
    @OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "EMP_ID", referencedColumnName = "EMP_ID")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
    private Employee employee = null;

只是说这就是他们想要的建模方式。这是指定的一对一关系。但是,当我去测试时,出现以下错误:

嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源[META-INF/spring/datasource-context.xml]中定义的名为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常是 org.hibernate.MappingException:composite-id 类必须实现可序列化:com.myprojects.tet.data.entity.Draw

为什么解释 Draw 中的 employee 键作为复合 ID?另外我该如何修复这个错误。 我已经读到引用的实体必须是可序列化的,而且它需要实现方法 equalshashCode (我还没有实现)。这两个的实施应该修复这个错误吗?

为清楚起见进行编辑

我有两个建模表:EMPLOYEEDRAW

EMPLOYEE 有:整数 emp_id,还有很多其他专栏。

DRAW 具有:整数 emp_id、整数 totalPoints

绘制表将每隔一定时间填充和清除,因此 totalPoint 列不能位于表 EMPLOYEE 中。我不确定我的实体关系是否正确(即将实体 employee 作为 draw 的 id)。

I have the following 2 entities:

   @Entity(name = "Employee")
   @Table(name = "EMPLOYEE")
   public class Employee implements Serializable {
      @Id
      @Column(name = "EMP_ID", insertable = true, updatable = false, nullable = false)
      private int id;

and

@Entity(name = "Draw")
@Table(name = "DRAW")

public class Draw extends AuditableEntity {
    @Id
    @OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "EMP_ID", referencedColumnName = "EMP_ID")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
        org.hibernate.annotations.CascadeType.MERGE})
    private Employee employee = null;

Let's just say that's the way they want it modeled. This is a OneToOne relationship as specified. However, when I go to test I get the following error:

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/spring/datasource-context.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: composite-id class must implement Serializable: com.myprojects.tet.data.entity.Draw

Why is the employee key in Draw interpreted as a composite-id? Also how do I fix this error. I have read that the entity referenced has to be serializable but also that it needs to implement method equals and hashCode (which I haven't implemented yet). Should implementation of those two fix that error?

Editing for clarity:

I have two modeled tables: EMPLOYEE and DRAW:

EMPLOYEE has: integer emp_id, plus a lot of other columns.

DRAW has: integer emp_id, integer totalPoints.

The draw table will be populated and purged every certain time, so column totalPoint cannot be in table EMPLOYEE. I am unsure if my entity relations are correct (i.e. having entity employee as the id for draw).

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

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

发布评论

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

评论(1

Bonjour°[大白 2025-01-03 04:52:32

@JoinColumn 替换为 @ PrimaryKeyJoinColumn

@Id
@Column(name = "EMP_ID")
private int id;

@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@PrimaryKeyJoinColumn(name = "EMP_ID", referencedColumnName = "EMP_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE})
private Employee employee;

Replace @JoinColumn with @PrimaryKeyJoinColumn:

@Id
@Column(name = "EMP_ID")
private int id;

@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@PrimaryKeyJoinColumn(name = "EMP_ID", referencedColumnName = "EMP_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE})
private Employee employee;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文