-->
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.  [ 4 posts ] 
Author Message
 Post subject: SaveOrUpdate behavior
PostPosted: Wed Sep 03, 2008 5:30 am 
Newbie

Joined: Wed Sep 03, 2008 5:01 am
Posts: 3
Hi,
in my application I have mapped an association (many to many) between the entities "content" and "document".
Now I'd like to implement the concept of "priority" in showing the documents associated with a content in the order I decide in my CMS.
I succeed in passing to the controller the doc ids in the order I want, but when the controller calls the SaveOrUpdate nothing changes in the order of the documents stored on database.
I think it happens because the objects in the session are the same, I only changed the order of their priority.

In my log there are these rows:
16:04:37 DEBUG Cascade - cascade ACTION_SAVE_UPDATE for collection: com.XXXX.contentDocuments
16:04:37 DEBUG CascadingAction - cascading to saveOrUpdate: com.XXXX.Document
16:04:37 DEBUG AbstractSaveEventListener - persistent instance of: com.XXXX.Document
16:04:37 DEBUG DefaultSaveOrUpdateEventListener - ignoring persistent instance
16:04:37 DEBUG DefaultSaveOrUpdateEventListener - object already associated with session: [com.XXXX.Document#10]

I use Hibernate 3.2.

Are there some settings or configuration to force the objects to be saved ?

Thanks.
MB


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

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Hibernate uses automatic dirty checking, which means that all you
have to do is to commit your transaction. Hibernate will detect and save changes to the database automatically. A typical unit of work is like this:

1. begin transaction: tx = session.beginTransaction();
2. load some objects: obj = session.get(...);
3. update some properties: obj.setValue(...);
4. commit the transaction: tx.commit();

There are a few possible things that causes updates to not be written to the database. For example, a property mapped with update="false" or a collection mapped with inverse="true".

Things get more complex if you add long-running conversations, which uses different sessions for loading and committing. There is a good document about this on the Hibernate wiki: http://www.hibernate.org/42.html

To help you with your problem it would be nice to see some code and mapping documents.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 9:43 am 
Newbie

Joined: Wed Sep 03, 2008 5:01 am
Posts: 3
In my application I use Spring+Hibernate, so the transaction management is handled by Spring.
I use DAO for adding, getting and setting my properties.
In my case I have the Content entity having a Set of Documents (mapped through an "@hibernate set").

I think that I could use an attribute "priority" added to the relation content/document (mapped in the table content_document, now in in there are in relation the keys of tables content and document).
I think to use this attribute to store the priority when saving the association content/document and use it later for sort my documents when showing.

But how can I map in Hibernate this attribute "priority" as a relationship attribute ?

Thanks.
MB


Top
 Profile  
 
 Post subject: I did it
PostPosted: Thu Sep 04, 2008 4:32 am 
Newbie

Joined: Wed Sep 03, 2008 5:01 am
Posts: 3
Yes, I did it.
I added a field "priority" to the relationship table between content and document.

I modified the annotation in my java class for the content-document association:

/**
* @hibernate.map table="content_document" cascade="save-update" order-by="priority asc"
* @hibernate.collection-key column="content_id"
* @hibernate.index-many-to-many column="doc_id" class="com.XXXX.Document"
* @hibernate.collection-element column="priority" type="int" not-null="true"
*/

before I used "set" instead of "map".

So I succeed in handling the attribute priority. When I change the priority of the documents, the object in session now changes, so I can save the changes (showing order) even if the objects in session are the same.


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