Hibernate Composite Key: foreign key has wrong number of columns

谁说胖子不能爱 提交于 2019-12-11 04:06:54

问题


I am new to Hibernate and JPA and am having difficulty setting up a composite key as defined below:

@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
}

When I run my dbunit tests I get the following error:

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

Please post if you can see where I'm going wrong!


回答1:


In my opinion, the problem is that you can't use complex types such a Type and Level (not sure what they are) as field or property of your composite key. From the JPA specification:

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.

What are they exactly (enumerations?)?

Apart from that, the approach to map a composite key itself is OK (see related question below).

Related questions

  • How to map a composite key with Hibernate?


来源:https://stackoverflow.com/questions/3586493/hibernate-composite-key-foreign-key-has-wrong-number-of-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!