I've got a problem implementing a relation to one-to-many beetwin an object end a set of objects of same class.
The class code is the following :
/***********************************************************/
public class Groupe {
private long m_uid;
public long getUid(){
return m_uid;
}
public void setUid(long primaryKey){
m_uid = primaryKey;
}
private String m_nom;
private Set m_sousGroupes = new HashSet();
public Groupe(){
}
public Groupe(String id){
setNom(id);
}
public String getNom() {
return m_nom;
}
public void setNom(String string) {
m_nom = string;
}
public Set getSousGroupes() {
return m_sousGroupes;
}
public void setSousGroupes(Set set) {
m_sousGroupes = set;
}
public boolean equals(Object object) {
if(!( object instanceof Groupe))
return false;
final Groupe g = (Groupe)object;
return g.getNom().equals(getNom());
}
public int hashCode() {
return getNom().hashCode();
}
}
/***********************************************************/
the mapping file :
/***********************************************************/
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="test" >
<class name="Groupe" table="GROUPE">
<id name="uid" type="long" column="uid">
<generator class="native"/>
</id>
<property name="nom" column="nom" type="string" not-null="true"/>
<set name="sousGroupes" cascade="all">
<key column="idGroupePere"/>
<one-to-many class="Groupe"/>
</set>
</class>
</hibernate-mapping>
/***********************************************************/
sql creation table
/***********************************************************/
create table GROUPE (uid NUMERIC(19,0) IDENTITY NOT NULL, nom VARCHAR(255) not null, idGroupePere NUMERIC(19,0) null, primary key (uid))
alter table GROUPE add constraint FK7DD0CDC614B9F149 foreign key (idGroupePere) references GROUPE"
/***********************************************************/
i tried the code :
/***********************************************************/
conf.addFile("./hbmXml/groupe.hbm.xml");
conf.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession(conn);
Transaction tx =s.beginTransaction();
Groupe g1 = new Groupe("Groupe1");
Set lesSGs = new HashSet();
Groupe sg = new Groupe("sg1");
lesSGs.add(sg);
g1.setSousGroupes(lesSGs);
s.saveOrUpdate(g1);
//s.saveOrUpdate(g1); //alternative method
tx.commit();
s.close();
/***********************************************************/
when i use save method the code i've got the exception :
/***********************************************************/
10:36:26,997 ERROR SessionImpl:2375 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at test.GroupeRW.main(GroupeRW.java:51)
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:689)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2368)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at test.GroupeRW.main(GroupeRW.java:51)
/***********************************************************/
when i use saveorupdate method the code i've got the exception :
/***********************************************************/
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: test.Groupe
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1474)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
at test.GroupeRW.main(GroupeRW.java:50)
/***********************************************************/
Has anybody have an idea in order to resolve my problem ?
Thanks
|