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 native id is the serial number(Integer) and Model( a custom class) like below:
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:
.
.
.
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
Most likely Hibernate just uses
@Idproperty and generates new value every time. That happens because you read your object from XML and id is not in XML, I assume. I don’t think natural id will coexist well with surrogate autogenerated id. And forsaveOrUpdateHibernate will just look atidto determine if object is new.Here is more details on Hibernate forums.