java - hibernate 复合键: foreign key has wrong number of columns

标签 java hibernate orm jpa foreign-keys

我是 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

如果您能看到我哪里出错了,请发帖!

最佳答案

在我看来,问题在于您不能使用复杂类型,例如 TypeLevel (不确定它们是什么)作为您的字段或属性复合键。来自 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. If java.util.Date is used as a primary key field or property, the temporal type should be specified as DATE.

它们到底是什么(枚举?)?

除此之外,映射复合键本身的方法也可以(请参阅下面的相关问题)。

相关问题

关于java - hibernate 复合键: foreign key has wrong number of columns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586493/

相关文章:

java - Hibernate 在更新父对象时不更新子对象

java - 在 Cayenne 中设置数据库路径

java - 为什么 Hibernate HQL 可以工作,而 Criteria 却不能?

java - 在 Hibernate 中,如何仅在序列化期间而不是在反序列化期间忽略属性

hibernate - Grails 在域[re​​wards.OnlineOrder] 引用[rewards.OrderItem] 上缺少列[order_items_order_item] 的类型或列

php - 当主键为 varchar 时,无法从 Laravel 的 Eloquent 中检索列值

VM Survivor Old Gen 中的 Java Survivor1、Survivor2

java - Apache POI Word 在表格后添加文本的最佳方法

java - 套接字 ClassCastException HashMap

java - 斯坦福解析器的类型化依赖