Ok it's very stupid example but it's very important for my knowldge in hibernate.
My primary key is PKID (surrogate key) in this case a db sequence.
My business key is email field. Then i won't that exists 2 user with the same email into a table.
About you it's a good approach this for my business key
Thanks in advance
Devis
Have you some link or example for to understand better this concept?
Code:
TuserDAO dao = new TuserDAO();
List li = dao.findByEmail("devis@devis.it");
Tuser vo = null;
if(li.size()<=0)
vo=new Tuser();
else
vo=(Tuser)li.get(0);
vo.setEmail("user@user.it");
vo.setDescr("Ciao");
dao.getSession().getTransaction().begin();
dao.SaveOrUpdate(vo);
dao.getSession().getTransaction().commit();
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.vo.Tuser" table="TUSER" schema="DEVIS" dynamic-update="true">
<id name="pkid" type="java.lang.Long">
<column name="PKID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">TUSER_SEQ</param>
</generator>
</id>
<version name="version" type="java.util.Date" column="VERSION" />
<property name="email" type="java.lang.String" unique="true">
<column name="email" not-null="true" />
</property>
<property name="descr" type="java.lang.String">
<column name="DESCR" length="100" />
</property>
</class>
</hibernate-mapping>
In my dao class
TuserDAO dao = new TuserDAO();
List li = dao.findByEmail("devis@devis.it");
Tuser vo = null;
if(li.size()<=0)
vo=new Tuser();
else
vo=(Tuser)li.get(0);
vo.setEmail("user@user.it");
vo.setDescr("Ciao");
dao.getSession().getTransaction().begin();
dao.SaveOrUpdate(vo);
dao.getSession().getTransaction().commit();