views:

249

answers:

1

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!

A: 

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

Pascal Thivent
Yes - they are enumerations. I hadn't realised they couldn't be used, thanks for the info. How else could I achieve this behaviour??
lainie
@Lainie I'm afraid you'd have to map them as string to obtain this exact behavior (maybe performing some conversion into enums in getters/setters). But honestly, it's kinda weird to use a limited set of values (NxM) for the key of a table which is typically unlimited. If you can change your database design, my suggestion would be to use a real `Id`.
Pascal Thivent
I know - it was weird design, which reflected a specific and fixed dataset. Went with the real Id solution - thanks for your help!
lainie