-->
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.  [ 1 post ] 
Author Message
 Post subject: NaturalId, how it works?
PostPosted: Thu Jul 14, 2011 1:19 pm 
Newbie

Joined: Thu Mar 31, 2011 9:08 am
Posts: 4
Hi everybody,

I´m having a problem with nativeid and i don´t know if i am doing right.

I have a class called Aircraft that has an generated id. But its natural id is the serial number(Integer) and Model( a custom class) like below:

Code:
public class Aircraft{
   
    @Id @GeneratedValue(strategy=GenerationType.AUTO,generator="COD_NS_AENV")
   @Column(name="COD_NS")
   private Integer id;
   @Column(name="NUM_IDENT_NS_AENV")
   @NaturalId
   private Integer serialNumber;
   @ManyToOne
   @NaturalId
   private Model model;
   
   .
   .
   .
}
   


I load this class from an XML and save into the database, like this:

Code:
.
.
.
Aircraft aircraft= (Aircraft)xstream.fromXML(reader);
Session session =  HibernateUtil.openSession();
Transaction t = session.beginTransaction();
      
session.saveOrUpdate(aircraft.getModelo());
session.saveOrUpdate(aircraft);
t.commit();
session.close();


It works fine for insert,but my problem is when I execute this code again with the same information, Hibernate inserts another aircraft. I want that Hibernate only updates the current registry.

I overrided the method equals() to test the serialNumber and Model attributes but it is never called.

I don´t know how to make Hibernate understand that an object with serialNumber and model are the NativeId.

Thanks in advance!

Alexandre


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.