-->
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: problem with one-to-many mapping
PostPosted: Thu Dec 04, 2003 3:47 pm 
Newbie

Joined: Tue Dec 02, 2003 1:08 pm
Posts: 5
Hi,

I'm sorry if this is something very obvious, but I've been trying to insert some data into PostGreSQL using Hibernate and it has not been working so far. I'm using Hibernate 2.1rc1.

I have 2 classes Molecule, Atom

Code:
public class Molecule {
// attributes

private List atoms;

//getter/setter methods
}

public class Atom {
// attributes
//getter/setter methods
}



The association between the two is unidirectional. My mapping documents are as follows:

Code:
<class name="Molecule" table="MOLECULE" discriminator-value="M">
<id name="id" column="molecule_id" type="long">
   <generator class="sequence"/>
</id>
<property name="commonName" column="common_name" type="java.lang.String"/>
<property name="molecularFormula" column="molecular_formula" type="java.lang.String" not-null="true"/>
<bag name="atoms" cascade="save-update" lazy="true">
    <key column="molecule_id"/>
    <one-to-many class="cseo.db.chemical.Atom"/>
</bag>
</class>

<class name="Atom"  table="ATOM" discriminator-value="A">
<id name="id" column="atom_id" type="long" unsaved-value="null">
    <generator class="sequence"/>
</id>
<property name="atomicNo" column="atomic_number" type="int" not-null="true"/>
<property name="charge" column="charge" type="double" not-null="true"/>
</class>



Now when I try to insert into the database I get the following exception:

Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into MOLECULE (common_name, molecular_formula, molecule_id) values (?, ?, ?)
Hibernate: insert into ATOM (atomic_number, charge, atom_id) values (?, ?, ?)
Hibernate: update ATOM set molecule_id=? where atom_id=?
net.sf.hibernate.HibernateException: Batch update row count wrong: 0 at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:118)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:55)
at net.sf.hibernate.impl.BatcherImpl.prepareBatchStatement(BatcherImpl.java:105)
at net.sf.hibernate.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:537)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2303)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2260)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2182)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)

Could anyone please let me know what I am doing wrong? Thanks in advance,

Priya


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 3:56 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
try unsaved-value=0 , instead of null for both atom and molecule

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 4:02 pm 
Newbie

Joined: Tue Dec 02, 2003 1:08 pm
Posts: 5
Thanks for the reply! Just tried that, it doesnt work. I still get the same exception.

Priya


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 4:12 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Take a look at documentation Parent/Child relationship "8.2. Bidirectional one to many".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 04, 2003 6:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Remove discriminator, these are not super classes.
try hibernate.jdbc.batch_size=0 and see what happen.

Show the between openSession() and session.close()

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2003 3:38 pm 
Newbie

Joined: Tue Dec 02, 2003 1:08 pm
Posts: 5
Hi,

I wasnt able to get to this until now, and I tried the
Code:
hibernate.jdbc.batch_size=0
and I dont get the error anymore. But, what I do get is

Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into MOLECULE (common_name, molecular_formula, molecule_id) values (?, ?, ?)
Hibernate: insert into ATOM (atomic_number, atom_id) values (?, ?)
Hibernate: update ATOM set molecule_id=? where atom_id=?
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.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:540)
at net.sf.hibernate.impl.ScheduledCollectionRecreate.execute(ScheduledCollectionRecreate.java:23)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2303)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2260)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2182)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)


which I think makes sense as the object hasnt yet been committed and so it cannot update it. Is it possible to persist collection relationships like this without having to save the objects in the collection explicitly first? I would like to leave all this to Hibernate as my actual molecule and atom objects are very complicated each containing 2 or more lists of other objects.

The code between openSession() and session.close() is as follows

Code:
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(_molecule);
transaction.commit();
session.close();


Thanks,
Priya


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 5:07 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It has not been commited but it has been executed into the db (2 inserts can be seen). Check the values inserted by setting log mode to debug for net.sf.hibernate.type

_________________
Emmanuel


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.