Question

I am trying to map a key in xml as follows:

    <composite-id>
        <key-property name="userId" column="USER_ID" />
        <key-property name="passwordsBack" column="PASSWORDS_BACK" />
    </composite-id>

I have seen this construction, without class="BlahPK", in the documentation, in "Hibernate in Action," and elsewhere. When I try to fetch, though, I get:

Initial SessionFactory creation failed.org.hibernate.MappingException: composite-id class must implement Serializable: MyClass

This is a very simple data class, it's not mutable, I don't need a key and certainly don't want to remodel my object and define a separate public class just to read it with Hibernate. Currently, I have it kludged to use rowid as the id, but I'd rather have the mapping cleanly reflect how the table and object are used.

Disclaimer: I searched StackOverflow and all I found was how to handle composite key hibernate, which just says "don't do it but you can."

Was it helpful?

Solution

You can define multiple @Id properties in the entity class. like

@Entity 
class User implements Serializable {
  @Id
  private String userId;

  @Id
  private String passwordsBack;
..
}

This will only be supported by Hibernate not with JPA. While you try to load this into session, you need to create an instance of User and set the id properties and the call the session.load(User.class, userinstance)

I found this hibernate documentation which will help understanding this. http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier Section you need to look for is 2.2.3.2.2. Multiple @Id properties.

EDIT: Just realized that you need the xml description and not the annotation based solution. There might be something similar to this approach. Let me see if I can get you a reference.

Update: If you can change your class MyClass to implement Serializable, that will fix the problem and no need to have another public PK class. You need to have the equals and hashCode methods which will use you id fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top