Hello everyone! My name is Pasquale and I'm going crazy .... :-)
I have a little problem!!
I need save multiple contacts of the same "Client"!!
this is the mapping file:
Code:
<class catalog="ares" name="ares.beans.Cliente" table="cliente">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
............
<class catalog="ares" name="ares.beans.Contatto" table="contatto">
<composite-id class="ares.beans.ContattoId" name="id">
<key-property name="id" type="int">
<column name="id"/>
</key-property>
<key-property name="clienteId" type="int">
<column name="cliente_id"/>
</key-property>
</composite-id>
<many-to-one class="ares.beans.Cliente" fetch="select" insert="false" name="cliente" update="false">
<column name="cliente_id" not-null="true"/>
</many-to-one>
This is the simple code:
Code:
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 2; i++) {
Contatto contatto = new Contatto();
ContattoId contattoId = new ContattoId();
contattoId.setClienteId(1);
contatto.setId(contattoId);
contatto.setContatto("TEST" + i);
contatto.setDCreazione(new Date());
contatto.setDModifica(new Date());
contatto.setTipo("TELEFONO");
session.save(contatto);
}
tx.commit();
session.close();
}
this is the error:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session]where am I wrong? :-(