-->
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.  [ 2 posts ] 
Author Message
 Post subject: self agregate to same Class object, one-to-many association
PostPosted: Mon Jun 21, 2004 4:42 am 
Newbie

Joined: Mon Jun 21, 2004 2:46 am
Posts: 2
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


Top
 Profile  
 
 Post subject: unsaved-value
PostPosted: Tue Jun 22, 2004 2:40 am 
Newbie

Joined: Mon Jun 21, 2004 2:46 am
Posts: 2
I found a solution :
- in the Id element i had the folowing attribut - unsaved-value="any".

I suppose i had problem with the native generation of the uid, i use SQL Server.


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

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.