我是 Hibernate 和 JPA 的新手,在设置复合键时遇到困难,如下定义:
@Entity
@Table(name = Entity.TABLE)
@IdClass(EntityPK.class)
public class MyEntity extends Entity {
@CollectionOfElements
@JoinTable(name="items",
joinColumns = @JoinColumn(name="items"))
private List<String> items;
@Id
private Type type;
@Id
private Level level;
// plus standard constructors/getters/setters
}
public class EntityPK implements Serializable {
private Type type;
private Level level;
// plus standard constructors/getters/setters
}
当我运行 dbunit 测试时,出现以下错误:
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.tnt.sech.domain.management.ReasonList from com.tnt.sech.domain.management.ReasonList has the wrong number of column. should be 2
如果您能看到我哪里出错了,请发帖!
最佳答案
在我看来,问题在于您不能使用复杂类型,例如 Type
和 Level
(不确定它们是什么)作为您的字段或属性复合键。来自 JPA 规范:
2.1.4 Primary Keys and Entity Identity
...
The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type;
java.lang.String
;java.util.Date
;java.sql.Date
. In general, however, approximate numeric types (e.g., floating point types) should never be used in primary keys. Entities whose primary keys use types other than these will not be portable. If generated primary keys are used, only integral types will be portable. Ifjava.util.Date
is used as a primary key field or property, the temporal type should be specified asDATE
.
它们到底是什么(枚举?)?
除此之外,映射复合键本身的方法也可以(请参阅下面的相关问题)。
相关问题
关于java - hibernate 复合键: foreign key has wrong number of columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586493/