-->
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.  [ 8 posts ] 
Author Message
 Post subject: Problems saving more than one object of the same class
PostPosted: Wed Feb 01, 2006 2:17 pm 
Newbie

Joined: Wed Feb 01, 2006 1:58 pm
Posts: 4
Location: Northern Hemisphere
Hi,

I have a HSQLDB (version 1.8) with 3 tables (Spiel, Satz, Zug).
There are one-to-many-relations between Spiel and Satz and between Satz and Zug.

The database works fine with Hibernate (version 3.1) except for one problem.

In a normal execution of the main programm, one Spiel-object is created, then a Satz-object which is related to the Spiel-object, and then some Zug-objects that are related to this Satz-object.

This works fine as long as I try to only save one Satz-object.
When I try to create a second Satz-object, I get:

Code:
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:353)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at vierGewinnt.database.DAO.speichereSatz(DAO.java:250)
   at vierGewinnt.database.DAO.main(DAO.java:101)


Zug-, and Spiel-objects can be saved without any problems, but everytime I try to save a 2nd Satz-object, this error occurs.


The mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="vierGewinnt.database.Satz" table="SATZ">
      <id name="satzId" column="satzId" type="long">
         <generator class="increment" />
      </id>
      <property name="satzNr" type="long"/>
      <property name="sieger" />

      <many-to-one name="spiel" class="vierGewinnt.database.Spiel"
         column="spielID" />

      <set name="zuege" table="ZUEGE" lazy="false">
         <key column="satzId" />
         <one-to-many class="vierGewinnt.database.Zug" />
      </set>
   </class>

</hibernate-mapping>


Satz.java
Code:
package vierGewinnt.database;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Satz implements Serializable
{
   private Long satzId;
   
   private Long satzNr;
   
   private int sieger;
   
   private Spiel spiel;
   
   private Set zuege ;
   
   
   public Satz() {
      zuege = new HashSet();
   }

Getters and Setters

hashCode() and equals()
}


part of the DAO.java
Code:
   public void speichereSatz(Long spielId, Long satznr, int sieger)
   {
      //Satzdaten speichern
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      Satz satz = null;
      Spiel spiel = null;
      Transaction tx = null;
      try {
         tx = session.beginTransaction();
         spiel = (Spiel)session.createCriteria(Spiel.class)
         .add(Restrictions.eq("spielId",spielId))
         .uniqueResult();
         satz = new Satz();
         satz.setSpiel(spiel); //Beziehung zum Spiel setzen
         satz.setSatzNr(satznr);
         satz.setSieger(sieger);      
         session.save(satz);
         tx.commit();
      } catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         System.out.println("Fehler beim Speichern des Satzes");
      }
}


Any ideas?

Thanks in advance...

Greetings from Germany,
JBond

_________________
There are 10 types of people - those who understand binary, and those who don't.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 5:05 pm 
Newbie

Joined: Thu Nov 17, 2005 1:44 pm
Posts: 6
Where's the mapping for the Spiel table?

_________________
Thank you for a Great product!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 6:10 pm 
Newbie

Joined: Wed Feb 01, 2006 1:58 pm
Posts: 4
Location: Northern Hemisphere
Spiel.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

   <class name="vierGewinnt.database.Spiel" table="SPIEL">
      <id name="spielId" column="spielId" type="long">
         <generator class="increment" />
      </id>
      <property name="start" />
      <property name="ende" />
      <property name="spieler1" />
      <property name="spieler2" />
      <property name="sieger" />

      <set name="saetze" table="SATZ" lazy="false">
         <key column="satzId" />
         <one-to-many class="vierGewinnt.database.Satz" />
      </set>
      
   </class>

</hibernate-mapping>


but I don't think, that it has something to do with the Spiel-table, because I tried the whole preocedure without any association between Spiel und Satz - and the same error occured...

_________________
There are 10 types of people - those who understand binary, and those who don't.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 6:16 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
may be a bug in the hashCode or equal method. You will probably not need to implement them.

Please post your complete code.
Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 6:56 pm 
Newbie

Joined: Wed Feb 01, 2006 1:58 pm
Posts: 4
Location: Northern Hemisphere
I implemented the equals and hashCode methods after the 1st occurance of this error, so they can't be the cause...

the whole Satz.java
Code:
package vierGewinnt.database;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Satz implements Serializable
{
   private Long satzId;
   
   private Long satzNr;
   
   private int sieger;
   
   private Spiel spiel;
   
   private Set zuege ;
   
   
   public Satz() {
      zuege = new HashSet();
   }
   
   public Long getSatzId()
   {
      return satzId;
   }

   private void setSatzId(Long satzId)
   {
      this.satzId = satzId;
   }

   public Long getSatzNr()
   {
      return satzNr;
   }

   public void setSatzNr(Long satzNr)
   {
      this.satzNr = satzNr;
   }

   public int getSieger()
   {
      return sieger;
   }

   public void setSieger(int sieger)
   {
      this.sieger = sieger;
   }

   public Spiel getSpiel()
   {
      return spiel;
   }

   public void setSpiel(Spiel spiel)
   {
      this.spiel = spiel;
   }
   
   public Set getZuege()
   {
      return zuege;
   }

   public void setZuege(Set zuege)
   {
      this.zuege = zuege;
   }      
   
   public boolean equals(Object andererSatz)
   {
      if (this == andererSatz) return true;
        if ( !(andererSatz instanceof Satz) ) return false;

        final Satz satz = (Satz) andererSatz;

        if ( !satz.getSpiel().equals(this.getSpiel())) return false;
        if ( !satz.getSatzNr().equals(this.getSatzNr())) return false;
      
      return true;
   }
   
    public int hashCode() {
        int result;
        result = getSpiel().hashCode();
        result = 7 * result + getSatzNr().hashCode();
        return result;
    }

}


_________________
There are 10 types of people - those who understand binary, and those who don't.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 7:07 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
I cannot see something specifiq. May be Hibernate is not properly guessing the type of your sieger column. Specify it.
Another thing you might try is changing the increment generator to assigned and just assign a random number.

Did you give the full stack trace? I am just wondering if your db did not return a more detailed exception

Sebastian

PS:
instead of writing the package name everywhere you can declare the package attribute in your
<hibernate-mapping package = ....

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 7:38 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
In your DAO, you're not adding your satz to your spiel. Your Set mapping isn't inverse, so it expects to be the "master" side of the relationship. So when you commit the transaction, you're setting the satz's spielId to be spielId (in DAO.java) and implicitly emptying spiel's Satz collection (because there are no satzs in the in-memory version of spiel). These are contradictory actions and are probably causing the problem.

If Satz.setSpiel() calls spiel.getSatz().add(this), then it would work. Alternatively, you could add inverse="true" to the Satz mapping in Spiel. If you did that, you'd have to refresh spiel after saving satz, so that its getSatz() Set was correct. If you want to keep your current mapping, then don't save satz; instead, add it to spiel and save spiel.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 6:57 am 
Newbie

Joined: Wed Feb 01, 2006 1:58 pm
Posts: 4
Location: Northern Hemisphere
thx for your help - but I found the problem somewhere else...


in the Spiel.hbm.xml I had
Code:
      <set name="saetze" table="SATZ" lazy="false" >
         <key column="satzId" />
         <one-to-many class="Satz" />
      </set>


instead of
Code:
      <set name="saetze" table="SATZ" lazy="false" >
         <key column="spielId" />
         <one-to-many class="Satz" />
      </set>


damn copy and paste - took me almost a week to find that...



the rest of my code works fine - I don't need to add the Satz-object to the Set in the Spiel-object...


Thanks again and greetings from Germany,
JBond

_________________
There are 10 types of people - those who understand binary, and those who don't.


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