-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can you force an update, instead of delete/insert
PostPosted: Wed May 16, 2007 5:08 pm 
Newbie

Joined: Wed May 16, 2007 4:13 pm
Posts: 5
I've been doing Hibernate for all of maybe 2 months and I must say, it's been great once I've learned how to use it. I'm actually using Spring 2.0 also and it really eliminates alot of coding, especially code concerning transactions and sessions.

I have a User (see mapping below) that has a unique constraint in the database on username& mainclientid. When I do an update, I get constraint violation exception. I notice that it happens because Hibernate is doing an Insert, not an Update. I've read (and seen) that Hibernate actually does a delete, and an insert for accomplishing an update. Normally this isn't a problem as I don't have this additional constraint on any other tables I'm dealing with.

I read in HIbernate in Action, that you would define my constraint as a composite-id (p335), but in the mapping schema you can have either <id> or <composite-id>. I don't really want to use the composite-id in all my foreign key relationship, so eliminating the <id> isn't really an option.

So as a work around to my constrain violation, can I force Hibernate to update instead of delete/insert? I believe I can do nothing to force the delete before the insert.

This is my User mapping file<hibernate-mapping>
<class name="com.its.safer.app.domain.User" table="User">
<id name="userID" column="UserID">
<generator class="increment" />
</id>
<property name="username" column="UserName" />
<property name="password" column="Password" />
<property name="name" column="Name" />
<property name="mainClientID" column="MainClientID" />
<property name="callCenter" column="callCenter" />
<property name="invalidAttempts" column="InvalidLoginAttempts" />
<property name="locked" column="IsLocked" />
<property name="lastChangeDate" column="DateModified" />
<property name="lastLoginDate" column="lastLogin" />
<set name="groups" table="UserGroupMap" cascade="none" lazy="false">
<key column="UserID"/>
<many-to-many class="com.its.safer.app.bean.Group" column="UserGroupID"/>
</set>
<set name="userExtension" cascade="all" lazy="false">
<key column="UserID" />
<one-to-many
class="com.its.safer.app.bean.UserExtension" />
</set>

</class>
<query name="com.its.safer.app.domain.User.byUserNameAndMainClient">
<![CDATA[from User where userName = :userName and mainClientID = :mainClientID]]>
</query>
<query name="com.its.safer.app.domain.User.byMainClient">
<![CDATA[from User where mainClientID = :mainClientID]]>
</query>
</hibernate-mapping>


I am using Hibernate 3.0 with Spring 2.0 so I don't do anything with Session. I simply call getHibernateTemplate().update(user); My database is MySQL 5.0.


Thanks for any help I can get! I've been searching for several hours for a work around!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 17, 2007 12:52 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I'm trying to make an educated guess here without seeing the code snippet that does the update or the exception.

So you have a one-many relationship from the User table to the UserExtension table. I'd assume this is a bidirectional relationship. In a one-many relationship, usually the many side is the owner, in this case UserExtension.

So in the mapping file for User, you need to set inverse=true for userExtension. This tells Hibernate to ignore it (from the User side). The link will be handled from the UserExtension side.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject: Can you force an update, instead of delete/inser
PostPosted: Thu May 17, 2007 10:59 am 
Newbie

Joined: Wed May 16, 2007 4:13 pm
Posts: 5
Actually the relationship with userextension is really one-to-one, but with a userextension having it's own unique key, that made me need to do the one-to-many relationship. It's not the saving of the data that I'm having the problem with. When I change my user table to have a composite key of username/mainClientID, save a record, then do to do an update, I get a unique constraint violation because instead of hibernate issuing an "update", it's doing a delete/insert. BUT, it does them in the order of insert (get the violation), then delete.

So, i'm asking how I can force hibernate to issue an "update" instead of delete/insert.

OR, can I force the delete to happen before the insert (I've read that you can't control this)

OR what kind of work-around is there on having a <composite-id> like entity in my mapping file in addition toi <id>

Thanks in advance for any help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.