How to use Hibernate combined id without class?

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

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

I saw this construct without class = "BlahPK" in the documentation, in "Hibernate in Action" and elsewhere. When I try to get, I get:

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

This is a very simple data class, it has not been changed, I don’t need a key and, of course, I don’t want to redo my object and define a separate public class, just reading it with Hibernate. I currently have kludged to use rowid as id, but I would prefer the display to purely reflect how the table and object are used.

Disclaimer: I was looking for StackOverflow, and all I found was how to handle hibernation with a combination key that simply says, “Don't do this, but you can.”

+4
source share
1 answer

You can define several properties @Idin an entity class. as

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

  @Id
  private String passwordsBack;
..
}

This will only be supported by Hibernate, not JPA. While you are trying to load this into the session, you need to create a user instance and set the id properties and callsession.load(User.class, userinstance)

, . http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-identifier , , - 2.2.3.2.2. @Id.

EDIT: , xml, . , - . , .

. MyClass Serializable, PK. equals hashCode, id.

+13

All Articles