【发布时间】:2012-01-13 21:27:59
【问题描述】:
我有以下 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;
我们只是说这是他们想要的建模方式。这是指定的 OneToOne 关系。但是,当我去测试时,我收到以下错误:
嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [META-INF/spring/datasource-context.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.MappingException:composite-id 类必须实现 Serializable:com.myprojects.tet.data.entity.Draw
为什么Draw 中的employee 键被解释为复合ID?另外我该如何解决这个错误。 I have read 引用的实体必须是可序列化的,但它还需要实现方法equals 和hashCode(我还没有实现)。实施这两个是否应该解决该错误?
为清晰起见进行编辑:
我有两个建模表:EMPLOYEE 和 DRAW:
EMPLOYEE 有:整数 emp_id,以及许多其他列。
DRAW 有:整数emp_id,整数totalPoints。
绘图表将每隔一定时间填充和清除,因此列totalPoint 不能在表EMPLOYEE 中。我不确定我的实体关系是否正确(即,将实体 employee 作为 draw 的 id)。
【问题讨论】:
标签: java spring one-to-one composite-key hibernate-annotations