-->
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: cascaded insert failed !
PostPosted: Tue Oct 07, 2003 11:44 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hello,

Today is my first day on Hibernate. I try to build a prototype application with some advanced features of Hibernate.
One of them is the cascading insert: My main object EMPLOYE owns a SET of SALAIRE (Salaries, in english). When I create a new employee with new salaries, Hibernate does the following:
- insert the new Employee : RIGHT !
- update the salaries : WRONG !

I have set the attribute "cascade=all" on the set Element, but the problem persists.

I may not set the right parameters in my mapping file, please HELP !

Thx,
Yann

-----------------------------------------

Here is my java code:
Code:
Session session = new Configuration().configure().buildSessionFactory().openSession();
      Employe employe = new Employe();
      employe.setNom("Welcome");
      employe.setPrenom("John");
      Set sals = new HashSet();
      Periode p1 = (Periode) (session.find("from Periode where periode = ?","1999",Hibernate.STRING)).iterator().next();
      Periode p2 = (Periode) (session.find("from Periode where periode = ?","1999",Hibernate.STRING)).iterator().next();
      sals.add(new Salaire(120,employe,p1));
      sals.add(new Salaire(110,employe,p2));
      employe.setSalaires(sals);
      session.save(employe);
      session.flush();
      session.connection().commit();

Here is my hbm file:

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

   <class name="Periode" table="periode">
      <id name="id" column="periode_id" type="long">
         <generator class="sequence">
            <param name="sequence">periode_id_seq</param>
         </generator>
      </id>
      <property name="nom" column="periode" type="string"/>
   </class>


   <class name="Adresse" table="Adresse">
      <id name="id" column="emp_id" type="long">
         <generator class="native"/>
      </id>
      <property name="adresse" type="string"/>
      <property name="codePostal" column="code_postal   " type="string"/>
      <property name="ville" type="string"/>
   </class>
   
   <class name="Employe" table="employe">
      <id name="id" column="emp_id" type="long">
         <generator class="sequence">
            <param name="sequence">employe_id_seq</param>
         </generator>
      </id>
      <property name="nom" type="string"/>
      <property name="prenom" type="string"/>
      <one-to-one name="adresse" class="Adresse" cascade="all" />
      <set name="salaires" cascade="all">
         <key column="emp_id"/>
         <one-to-many class="Salaire" />      
      </set>
   </class>
   
   <class name="Salaire" table="Salaire">
      <composite-id>
         <key-many-to-one name="employe" column="emp_id" class="Employe"/>
         <key-many-to-one name="periode" column="periode_id" class="Periode"/>
      </composite-id>
      <property name="salaire"  type="long"/>
   </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: cascaded insert failed !
PostPosted: Tue Oct 07, 2003 11:45 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
One more thing: the generated trace !

Code:
Hibernate: select periode0_.periode_id as periode_id, periode0_.periode as periode from periode periode0_ where (periode=? )
Hibernate: select periode0_.periode_id as periode_id, periode0_.periode as periode from periode periode0_ where (periode=? )
Hibernate: select employe_id_seq.nextval from dual
Hibernate: insert into employe (nom, prenom, emp_id) values (?, ?, ?)
Hibernate: update Salaire set salaire=? where emp_id=? and periode_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.persister.EntityPersister.update(EntityPersister.java:642)
   at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:611)
   at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:31)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2062)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2005)
   at Test.main(Test.java:38)


Top
 Profile  
 
 Post subject: Re: cascaded insert failed !
PostPosted: Tue Oct 07, 2003 11:48 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Please note that the "salaires" collection has a composite-id (M-N class).


Top
 Profile  
 
 Post subject: Reread the documentation on using composite id's
PostPosted: Tue Oct 07, 2003 11:56 am 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Reread the documentation on using composite id's.

When you use a composite identifier you have only one of two choices for the cascading, it either assumes everything is an update, or everything is an insert.

By default everything is assumed to be an update and you must explicitly save it (Sessin.save()) or, if you change your mapping to include 'unsaved-value="any"' everything will be assume to be an insert and you will have to explicitly call update.

Hibernate's prefered approach is to use Surrogate keys instead of Natural keys and thereby avoid ever having composite identifiers.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The "easiest" (and by that I mean short-term) fix is to appropriately define an "unsaved-value" attribute for the composite id mapping in the Salary class:

Code:
    <class name="Salaire" table="Salaire">
        <composite-id unsaved-value="any">
            ...
        </composite-id>
        ...


Specifically check out the section of the docs relating to composite-ids and you should really check out the section on using components as composit identifiers.

http://www.hibernate.org/hib_docs/reference/html/or-mapping.html#or-mapping-s1-4b

http://www.hibernate.org/hib_docs/reference/html/components.html#components-s2-3


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 12:08 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Oki, thanks, that works.

-----
Nevertheless, i get the following error during the parsing of the hbm file:

7 oct. 2003 18:05:06 net.sf.hibernate.util.XMLHelper$ErrorLogger error
GRAVE: Error parsing XML: XML InputStream(44)
org.xml.sax.SAXParseException: The content of element type "composite-id" must match "(key-property|key-many-to-one)+".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:252)
at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:273)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:838)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:792)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:732)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:719)
at Test.main(Test.java:27)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Of course you replaced the elipses (...) properly, right?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 07, 2003 12:31 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
hum hum.... i would you say ?
I am quite embarassed ;-)

yep, i let a "greater than" character walking in my xml code.

Thank you both for your quick help !


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.