简体   繁体   中英

How to update a table with composite key using hibernate?

I am newbie to hibernate. If there is a table which has a composite key, how to update the table using hibernate.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM