I am newbie to hibernate. If there is a table which has a composite key, how to update the table using hibernate.
+3
A:
You should be able to use a composite-id
for that.
Example copied from the link:
<composite-id
name="propertyName"
class="ClassName"
mapped="true|false"
access="field|property|ClassName">
node="element-name|."
<key-property name="propertyName" type="typename" column="column_name"/>
<key-many-to-one name="propertyName class="ClassName" column="column_name"/>
......
</composite-id>
You can then retrieve the record using load
instead of get
(pasted code from this example):
Book bk1 = new Book();
bk1.setBookId(1);
bk1.setBookName("Hibernate Examples");
bk1.setAuthor("ISHTEK");
Book bk2 = (Book) session.load(Book.class, bk1);
which you can then update after changing your values
session.update(bk1);
Peter Lang
2010-03-20 09:44:50
i have made this change in xml file, my doubt is like in the java code we will be getting the persistant object using the hibernate session, right . So in the get method we will be passing the class and the attribut, currently i have to check for two attribute before updating that record. how is that possible..
Hari
2010-03-20 10:45:59
@Hari: Please check my updated answer, hope I got your question right.
Peter Lang
2010-03-20 21:07:23