-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Primary Keys Regenerated Issue
PostPosted: Wed Aug 13, 2008 10:53 am 
Newbie

Joined: Wed Aug 13, 2008 10:30 am
Posts: 4
Hibernate version: 3.2
Mapping documents: See below
Name and version of the database you are using: MySQL 5.0

Hi all - I have been using Hibernate for about 3 weeks now and am very impressed. However, I have run into a small problem which is causing me some issues - I have searched the forums and the web and cannot find an answer so I would be grateful if someone could point me in the right direction.

The problem is that my primary keys are being regenerated each time I update the database - this is causing me issues as I have no natural identifier and therefore one of the assumptions of my application is that the primary keys will not change. Below is the sample of my Party mapping document. I am using Hibernate Tools to generate both my schema and code.

This is the secnario - I create a new Vendor (a subclass of Party) and save it. I then add some Services to this Vendor and save them. They are persisted succesfully however the PartyID (primary key of my Party record) has been updated to a new value (MAX KEY + 1).

How can I prevent my primary keys from being updated?

Thanks in advance,

mark


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mycompany.model">
<class name="Party" table="t_party">
<meta attribute="class-description">
</meta>
<id column="party_id" name="partyID" type="int">
<meta attribute="scope-set">protected
</meta>
<generator class="native"/>
</id>
<property column="company_name" name="companyName" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="title" name="title" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="first_name" name="firstName" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="last_name" name="lastName" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="gender" name="gender" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="email" name="email" not-null="true" type="string">
<meta attribute="field-description"/>
</property>
<property column="phone" name="phone" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="mobile" name="mobile" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="fax" name="fax" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<map name="addresses" cascade="all">
<meta attribute="field-description">Addresses for this party
</meta>
<key column="party_id"/>
<map-key column="address_type" type="string"/>
<one-to-many class="Address"/>
</map>
<many-to-one class="User" column="user_id" name="user" not-null="false" unique="true" cascade="all"/>
<joined-subclass name="Vendor" table="t_vendor">
<key column="vendor_id"/>
<property column="short_description" name="shortDescription" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="long_description" name="longDescription" not-null="false" type="text">
<meta attribute="field-description"/>
</property>
<property column="website" name="website" not-null="false" type="string">
<meta attribute="field-description"/>
</property>
<property column="active" name="active" not-null="false" type="yes_no">
<meta attribute="field-description"/>
</property>
<set name="services" cascade="all">
<meta attribute="field-description">Services offered by this vendor
</meta>
<key column="vendor_id"/>
<one-to-many class="VendorService"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 7:44 am 
Newbie

Joined: Wed Aug 13, 2008 10:30 am
Posts: 4
On further investigation I discovered that my keys were not being regenerated, but rather my update method was inserting new rows into the database. The problem was that I was retrieving an Object from the database, closing the session, displaying it in the UI, and then opening a new session and calling session.save(object) to write it back to the database. Changing session.save(object) to session.saveOrUpdate(object) fixed my problem. Unfotunately, the book I used to learn Hibernate did not give examples of using saveOrUpdate but this was explained in detail in the Hibernate documentation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.