-->
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: delete() and saveOrUpdate() causes HibernateOptimisticLockin
PostPosted: Tue Sep 02, 2008 6:02 pm 
Newbie

Joined: Tue Sep 02, 2008 5:29 pm
Posts: 1
All,

I am new to Hibernate (version 3.2.3.ga). I need to make changes to an exsisting application which was written with Hibernate. The appl is working fine before I added my change.

In a nutshell, there are two objects DMStagingTable and DMStagingIndex.
DMStagingTable._indexes is a set of DMStagingIndex instances.

My objective is to delete DMStagingIndex objects and update its persistent counter parts.

I had tried:

/////// Version 1

remove DMStagingIndex from the set DMStagingTable._indexes

call session.saveOrUpdate(DMStagingTable) // legacy call; maybe I should not be calling it.

the call does not bomb but it does not update the database

/////// Version 2

call session.delete(DMStagingIndex) // my idea

remove DMStagingIndex from the set DMStagingTable._indexes

call session.saveOrUpdate(DMStagingTable) // legacy call

the call bombs with HibernateOptimisticLockingFailureException

I did not make the call to saveOrUpdate((DMStagingTable)) but it is from existing code; I think it is trying to update the objects for different reasons. I don't think I can avoid this call.

Summary:

Any pointer will be greatly appreciated. So far working with Hibernate is quite painfull. I have been reading the Hibernate documentation, but I have not been able to solve this problem on my own. I also have the book java persistence with Hibernate. It has a lot of information, but by combinating JPAPI and Hibernate, it makes thing unnecessary complicated. Please suggest other essential readings; my goal is just to master Hibernate for now; Java Persistence can wait. There must be some trick in delete objects and make the state consistent with both memory and database instances.


Thanks,

Steve

///////////////////////////////////////////////////////////////////////////
//details of the relationship between DMStagingTable and DMStagingIndex==
///////////////////////////////////////////////////////////////////////////
(1)
public class DMStagingTable
@OneToMany(targetEntity=DMStagingIndex.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy="stagingTable")
...
private Set<DMStagingIndex> _indexes = new HashSet<DMStagingIndex>();

(2)
public class DMStagingIndex
@ManyToOne(targetEntity=DMStagingTable.class, cascade={CascadeType.ALL})

@ManyToMany(fetch=FetchType.EAGER, targetEntity=DMStagingColumn.class)

@JoinTable(name="DM_STAGING_INDEX_COLUMNS",
joinColumns=@JoinColumn(name="STAGING_INDEX_ID", referencedColumnName="ID"),
inverseJoinColumns=@JoinColumn(name="STAGING_COLUMN_ID", referencedColumnName="ID"))


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 2:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Could be that you need to map the @OneToMany as inverse, since the @ManyToOne is the "owning" side of the relation. I think version 2 fails because both the deletion and the removal from the set causes Hibernate to update/delete the same row twice.


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.