Hibernate Composite Key: foreign key has wrong num

2019-07-18 20:03发布

问题:

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?