-->
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.  [ 7 posts ] 
Author Message
 Post subject: Parent-Children save problem with Hibernate 2.1.1
PostPosted: Wed Dec 24, 2003 7:16 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
hi everyone,
and sorry to bother you with another Parent/Children problem.

I'm just trying to do a simple session.save(parent) that should also save children. But it inserts the parent and then tries to update children (not saved yet)

Here is my code:

///////////////////////
[code]Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();[/code]

Parent p = new Parent();
p.setName("parentName");
p.setChildren(new ArrayList());
Child c = new Child();
c.setName("childName");
c.setParent(p);
p.getChildren().add(c);
session.save(p);

tx.commit();
HibernateUtil.closeSession();
//////////////////////////

And here is the config:
<class name="xpetstore.domain.Parent" table="T_PARENT">
<id name="parentId" length="10">
<generator class="assigned"/>
</id>
<bag
name="children"
lazy="false"
inverse="true"
cascade="all"
>
<key column="parent_id" />
<one-to-many class="xpetstore.domain.Child"/>
</bag>
<property
name="name"
type="java.lang.String"
column="name"
length="20"
/>
</class>

<class name="xpetstore.domain.Child" table="T_CHILD"
dynamic-insert="true">
<id name="childId" length="10" >
<generator class="assigned"/>
</id>
<property
name="name"
type="java.lang.String"
column="name"
length="20"
/>
<many-to-one
name="parent"
class="xpetstore.domain.Parent"
column="parent_id"
not-null="true"
/>
</class>

At last (sorry if it's a little long) the error message:

Hibernate: insert into T_PARENT (name, parentId) values (?, ?)
Hibernate: update T_CHILD set name=?, parent_id=? where childId=?
12896 [http8080-Processor4] ERROR impl.SessionImpl - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:625)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at xpetstore.util.HibernateProductService.getProductAndItems(Unknown Source)
at xpetstore.web.struts.action.product.ProductAction.execute(Unknown Source)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:446)

I have read the documentation and the FAQs, but haven't found a solution.
I'm using MySql and an hibernate.cfg.xml file.

Thanks a lot for any help
Gilles


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 7:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This one will help you http://www.hibernate.org/116.html#A11.
Read the doc on that subject too.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 9:21 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
"assigned"...
now I understand what that means...
what an id**t...
You rock my world! Thanks a lot.

I've thus changed my id properties to be Long in my java code, and set the generator to "native". I'm using mysql.
It seems to be taking hilo as default algorithm (thus it needs an extra table...)

Is there anyway that the generator could use the auto_increment mysql feature alone (meaning incrementing ids as they are created?)

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 9:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I would be very surprised if MySql does not implement either sequence or idendity.

If I remember well, identity is supported.

try "identity" generator

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 10:32 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
Thanks Emanuel for your prompt answer even on Christmas eve:

when I use "increment", it works.
when I use "identity" it fails when creating the SessionFactory the first time:
java.lang.ExceptionInInitializerError
...
log from Tomcat:
Caused by: net.sf.hibernate.MappingException: Dialect does not support Caused by: net.sf.hibernate.MappingException: Dialect does not support identity key generation
at net.sf.hibernate.dialect.Dialect.getIdentitySelectString(Dialect.java:272)
at net.sf.hibernate.persister.AbstractEntityPersister.<init>(AbstractEntityPersister.java:639)
at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:699)
at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:137)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:739)...

I have:
<property name="dialect">net.sf.hibernate.sql.MySQLDialect</property>

Any idea?
Thanks again
Gilles


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 10:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
s_gilou wrote:
Any idea?

So, I'm wrong on my guess on MySQL :-(

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 10:59 am 
Newbie

Joined: Wed Dec 24, 2003 6:37 am
Posts: 8
I guess I'll stay with increment since I'm not using a cluster.

Thanks a lot again for pointing me to the right direction,
"I owe you"

Thanks and like the good old saying:
"Meeeeeerry Christmas" (with santa voice:)

Thanks!
Gilles


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.