作为一对一关系上的键的实体返回“复合ID”错误
我有以下 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?另外我该如何修复这个错误。 我已经读到引用的实体必须是可序列化的,而且它需要实现方法 equals
和 hashCode
(我还没有实现)。这两个的实施应该修复这个错误吗?
为清楚起见进行编辑:
我有两个建模表:EMPLOYEE
和 DRAW
:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
@JoinColumn
替换为@ PrimaryKeyJoinColumn
:Replace
@JoinColumn
with@PrimaryKeyJoinColumn
: