-->
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: Checking if a duplicate Object already exists in the db
PostPosted: Mon Mar 09, 2009 6:00 am 
Newbie

Joined: Mon Mar 09, 2009 5:23 am
Posts: 3
Hibernate version: 3.2.3.ga

Hi,

I have a question about assigning IDs when persisting a nested object.

I have read the sample chapter and have been through a couple of books but cant find anything enlightening to what must be a very common scenario.

I have 2 tables:


ARTICLE
ID
...
TAG_ID



TAG
ID
WORD



My Article POJO has-a Tag object.
When I want to save the ARTICLE I do a session.save(article).

Fine so far...the ARTICLE gets inserted and so does its nested TAG in their respective tables.

Now, I dont want to duplicate my TAGs (equality based on the WORD string). So when I perist an Article I want to INSERT the nested Tag if it doesnt already exist or reference the existing one if it does.

So my question is - is the only way of achieving the above by checking in my Java code ie:

(pseudo hql)
1. Fetch Tag where TAG.word = the tag word I intend to persist
2. If already exists, get TAG.id of existing record and assign to the nested Tag object I wish to persist (Im assuming that if I do session.save(object) on an already existing object then it only gets updated if any state has changed)


I am relatively new to the ORM world - can anyone tell me if what Im trying to achieve can be automated via Hibernate functionality or is my only option to check manually in my code as I suggest above?
Am I asking too much of Hibernate?




In my mapping for Article I have:

...
<many-to-one name="article"
class="com.Article" cascade="save-update" unique="true">
<column name="TAG_ID" not-null="true" />
</many-to-one>
....




PS. Regarding HQL- the Hibernate official documentation and books such as Manning's Hibernate in Action give lots of HQL examples but dont really discuss its limitations ie. Save and Update can only be used on data from exisitng tables. Can anyone point me in the direction of a good discussion of HQL, including its limitations? I really like its syntax being a programmer and prefer it over Criterion and native SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 10:04 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
In the article mapping shouldn't it be a mapping to the TAG not the article?
Code:
<many-to-one name="tag"
class="com.Tag" cascade="save-update" unique="true">
<column name="TAG_ID" not-null="true" />
</many-to-one>


And, this WILL work. The question is how are you retrieving the article? If you retrieve the article again, the tag property will be set as it was persisted. Then when you save it again, it will not insert a new one. If you create a new tag object and set it for the article, a new tag will be inserted when the article is saved.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 11:56 am 
Newbie

Joined: Mon Mar 09, 2009 5:23 am
Posts: 3
Hi


Thanks for the reply sowelie - I posted the wrong mapping snippet...

when you say "this WILL work" do you mean my scenario of retrieving the Tag if it exists and setting the ID manually on the Tag I wish to save?

I wanted to know more if Hibernate has any automated facilities for handling unique object>record persistence i.e ideally I dont want to hit the database to check if my Tag already exists - I just want to say to Hibernate "this nested Tag object property needs to be unique so if it already exists in the database then dont INSERT a new one but just set the foreign key on the parent Article record to be the ID of the existing Tag record"


Thanks


Top
 Profile  
 
 Post subject: Re: Checking if a duplicate Object already exists in the db
PostPosted: Wed Aug 05, 2009 11:22 am 
Newbie

Joined: Wed Aug 05, 2009 11:10 am
Posts: 1
Hi,

Have you found an answer to this problem please ? I am actually having the same experience.

I have a parent table, and a child table, linked by a foreign key.
I declare a many-to-one association into parent hbm file with a cascade all property.
I first persit a parent (save or persist) and it saves a parent and a child. Fine so far.
But in another session, i create a new parent object (functionnaly different than the one of the first session) and i make a reference to a new child object (but which is functionnaly the SAME than the one of the first session, and it is a Transient object at this point)

I would like to know if Hibernate may natively support and identify that i am managing the same child (and so do not try to persist it when i save the new parent) or do i have to develop a roundtrip to database and check if the child is already present?

Thanks all


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.