I am creating a table with two primary keys. The first one is
id(Integer) and second one is
My mapping file contains
<class name="Person" table="PERSONS">
<composite-id name="pkField" class="PKField">
<key-property name="personId" column="person_id"/>
<key-property name="email" column="email"/>
<property name="firstName" column="first_name"/>
<property name="lastName" column="last_name"/>
<property name="age" column="age"/>
Now I am trying to select data using load method, but it's raising a typeMisMatch Exception.
My load function is:
Person p1=(Person) ss.load(Person.class,new Integer("1"));
How do you retrieve data if the table contains two primary keys. Please help me.
you have to use
Person p1=(Person) ss.load(Person.class,new PKField("1","email"));
and PKField must implement .equals() and hashcode() methods, hibernate relies on these methods to cache and compare the data.
Create an instance of
PKField and pass it to load() method.
Make sure your composite key class satisfies the conditions specified here
A composite-id means that records are identical if all parts of the composite id match. That is, the rows
ID | EMAIL ----+------------------- 1 | [email protected] 1 | [email protected]
represent different objects to hibernate. You therefore need to pass all parts of the composite key to
load(...) to identify the object that should be loaded.
If your intention is that the id by itself uniquely identifies a record, and the same for email, declare one as identifier and the other as natural-id. You might have to use queries to get the entity by natural-id (not sure, I never use them myself).