-->
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.  [ 6 posts ] 
Author Message
 Post subject: Can class be identified when no PK specified?
PostPosted: Wed Jan 21, 2009 6:30 pm 
Newbie

Joined: Mon Oct 20, 2008 1:50 pm
Posts: 7
Hi,

An application that I'm working on has a Person class that consists of a Location class that contains a Country class.
These objects are read from a feed every day before inserting into or updating the database.
Primary keys will be generated automatically by Hibernate.

Say I have a Country class with attributes as follows:
countryID (Primary Key, not included in feed)
countryCode (Unique, included in feed)
countryDesc (Text, included in feed)

If I'm inserting Person objects which in turn contains Country objects, and a Country's countryDesc changes, will Hibernate go and insert a new Country object?
The problem I have is that the feed won't contain the Primary Key, but will Hibernate be able to figure out that it should update the existing Country instead of adding a new one?

Hope this makes sense. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 6:50 pm 
Newbie

Joined: Mon Oct 20, 2008 1:50 pm
Posts: 7
Sorry to bump this, but does anyone have any ideas please?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 7:26 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
If the Country object doesn't have the countryID populated, when you call Save, Hibernate should try to populate it based on the generator defined.

See section 5.1.4.6 in
http://www.hibernate.org/hib_docs/refer ... pping.html

If you can let the database assign countryID, countryCode may be used as the natural key.

---
Please rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 1:30 pm 
Newbie

Joined: Mon Oct 20, 2008 1:50 pm
Posts: 7
If I insert a Country object that contains only a code "USA" and description "United States"
it inserts a new row as follows:
1 - USA - United States, where 1 is the auto-generated PK

If I attempt to add a new Country object again only with code "USA" and description "United States of America" it throws a constraint violation exception from the database as it attempts to add a new Country object.

Is there any way at all that Hibernate will realize that the second Country object already exists based on it's natural ID and will update the existing row as opposed to attempting to insert a new one?

As I'd prefer to generate the PKs using Hibernate.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 2:32 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You'll have to write your own code for checking that. For example, something like this:

Code:
String hql = "select c from Country c where c.countryCode = :code";
Query q = session.createQuery(hql);
q.setString("code", "USA");
County c = (Country)q.uniqueResult();
if (c==null)
{
  c = new Country();
  session.save(c);
}
// ...set/update rest of the country properties


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 6:42 am 
Newbie

Joined: Sat Feb 21, 2009 1:30 pm
Posts: 5
Location: Bangalore
I think in hibernate you can define the custom PrimaryKey class for an entity.

Ex.
@Entity
@Table(name = "Person")
@IdClass(PersonPK.class)
public class Person {
.......

}

public class PersonPK{

// Here u can mention your custom primary key fields for the entity.
}


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