-->
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.  [ 5 posts ] 
Author Message
 Post subject: saveOrUpdate without cascades on set
PostPosted: Wed Mar 09, 2005 8:48 am 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Hello

I am having difficulties to update an entitiy. The problem is the entity I want to save is of type service as shown in the mapping below and it seems that "Hibernate" is trying to cascade and update the childs of service, which is a problem. I just want to save the Service entity.

Is there a way to handle that?

Any help greatly appreciated, thank you.

Regards
Tarik


Hibernate version:

2.0

Mapping documents:

Code:
<?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 default-cascade="none">
   <!--
    Created by the Middlegen Hibernate plugin 2.1

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->
   <class name="com.ACME.MMSCAdmin.beans.Service" table="SERVICE" lazy="true" >
      <cache usage="read-write"/>
      <id name="serviceID" type="long" column="SERVICE_ID">
         <generator class="native"/>
      </id>
      <property name="name" type="java.lang.String" column="NAME" not-null="true" length="100"/>
      <property name="serviceTable" type="java.lang.String" column="SERVICETABLE" not-null="true" length="100"/>
      <!-- Associations -->
      <set name="keywords"  lazy="true" order-by="KEYWORD" batch-size="20" outer-join="true">
         <key column="SERVICE_ID"/>         
         <one-to-many class="com.ACME.MMSCAdmin.beans.Keyword" />
      </set>
      <set name="abos"  order-by="SENDDATESTART" batch-size="100" outer-join="true">
         <key column="SERVICE_ID"/>         
         <one-to-many class="com.ACME.MMSCAdmin.beans.MMSAbo"/>
      </set>
   </class>
</hibernate-mapping>


Full stack trace of any exception that occurs:

Code:
Hibernate: update SERVICE set NAME=?, SERVICETABLE=? where SERVICE_ID=?
Hibernate: update KEYWORD set SERVICE_ID=null where SERVICE_ID=?
WARN  [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] net.sf.hibernate.util.JDBCExceptionReporter : SQL Error: 1216, SQLState: 23000
ERROR [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] net.sf.hibernate.util.JDBCExceptionReporter : Duplicate key or integrity constraint violation,  message from server: "Cannot add or update a child row: a foreign key constraint fails"
WARN  [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] net.sf.hibernate.util.JDBCExceptionReporter : SQL Error: 1216, SQLState: 23000
ERROR [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] net.sf.hibernate.util.JDBCExceptionReporter : Duplicate key or integrity constraint violation,  message from server: "Cannot add or update a child row: a foreign key constraint fails"
ERROR [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] net.sf.hibernate.impl.SessionImpl : Could not synchronize database state with session
FATAL [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] com.ACME.MMSCAdmin.daos.ServiceDAO : net.sf.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
FATAL [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] com.ACME.MMSCAdmin.struts.actions.ServicesAction : com.ACME.MMSCAdmin.struts.common.MMSCAdminException: Could not save or update service. (see below for lower-level details)
FATAL [Mi Mrz 9 13:20:08 CET 2005:] [http8080-Processor22] [] com.ACME.MMSCAdmin.struts.actions.ServicesAction : com.ACME.MMSCAdmin.beans.Service@1acc234[serviceID : =48,name : =MNC Testa]


Name and version of the database you are using:

MySQL 4.17

The generated SQL (show_sql=true):

Hibernate: update SERVICE set NAME=?, SERVICETABLE=? where SERVICE_ID=?
Hibernate: update KEYWORD set SERVICE_ID=null where SERVICE_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 11:22 am 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Hello, me again

Even if my stuff seems to be correct I'd be happy for any help or comment.
Thanks.

Tarik


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 04, 2005 1:27 pm 
Beginner
Beginner

Joined: Sun Jan 16, 2005 5:45 pm
Posts: 24
Location: Atlanta
I'm having the exact same problem (symptom) as described above and it is driving me nuts.
For me, it all boils down to the second SQL (set AC_ID=null)... Why is this being generated?
In my case, I should not be making any references to the Chart class.
I have cascade set to "none". Note, I actually want cascading but set it to none in an attempt to isolate the problem.

Code:
11:03:49,761 INFO  [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?
11:03:49,777 INFO  [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?


Environment:
Windows XP Pro
JBoss: 4.0.2 with EJB 3.0
Spring: 1.2.1
MySql: 4.1 Connector: 3.1.8
Hibernate: 3.0.5

Architecture 3 tier:
Web Server
App Server
DB Server

DB access via EJB and Transfer Objects

Scenario:
Web tier calls EJB to update actype object. Cascade set to "false" for all child objects and collections.

Hibernate creates correct update SQL:

Code:
11:03:49,761 INFO  [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?


Then throws in this extra one that triggers the exception:
Code:
11:03:49,777 INFO  [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?


Actype.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
     default-cascade="none"   
     default-access="property"
     default-lazy="true"
     package="com.eflight.eloadsheet.domainobjects">
 
    <class name="Actype" table="actype">
        <meta attribute="class-description">
        Aircraft Type
        @author Paul Baker
        </meta>
        <id name="id" type="long">
            <meta attribute="field-description">
            ID field.
            </meta>
            <meta attribute="scope-set">public</meta>
            <meta attribute="scope-get">public</meta>
            <meta attribute="use-in-tostring">true</meta>
            <column name="ID" precision="20" />
            <generator class="native" />
        </id>
        <version  name="ver" column="VER" type="long"/>

        <property name="type" type="string">
            <meta attribute="use-in-tostring">true</meta>
            <column name="TYPE"   length="3"  not-null="true" />
        </property>

        <property name="suffix" type="string">
            <meta attribute="use-in-tostring">true</meta>
            <column name="SUFFIX" length="3"  not-null="true" />
        </property>

        <property name="model" type="string">
            <meta attribute="use-in-tostring">true</meta>
            <column name="MODEL"  length="60" not-null="true" />
        </property>
       
        <one-to-one name="OptLoadsheet" class="OptLoadsheet" cascade="none" constrained="true"/>
        <one-to-one name="WbType"       class="WbType"       cascade="none" constrained="true"/>
        <set name="cgCharts" lazy="true" cascade="none">
            <key column="AC_ID" />
            <one-to-many class="Chart" />
        </set>

    </class>
</hibernate-mapping>


DAO Class:

Code:
   /**
    * @see com.dygtig.domain.dao.ActypeDao#update()
    */
   public void update(Actype actype) {

      this.getHibernateTemplate().update(actype);
      this.getHibernateTemplate().flush();
      return;
   }


I have spent several hours trying to track this problem down... I will gladly supply any additional information...

Thanks in advance... Paul

Here is a bit more of the trace:

Code:
11:03:45,793 INFO  [DefaultListableBeanFactory] Creating shared instance of singleton bean 'transactionManager'
11:03:45,793 INFO  [JtaTransactionManager] Using JTA UserTransaction: org.jboss.tm.usertx.client.ServerVMClientUserTransaction@ea2352
11:03:45,793 INFO  [JtaTransactionManager] Using JTA TransactionManager: org.jboss.tm.TxManager@1b505fe
11:03:45,793 INFO  [DefaultListableBeanFactory] Creating shared instance of singleton bean 'actypeService'
11:03:49,761 INFO  [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?
11:03:49,777 INFO  [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?
11:03:49,918 WARN  [JDBCExceptionReporter] SQL Error: 1216, SQLState: 23000
11:03:49,918 ERROR [JDBCExceptionReporter] Cannot add or update a child row: a foreign key constraint fails
11:03:49,933 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
   at org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:713)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
   at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:711)
   at com.eflight.eloadsheet.daos.hibernate.ActypeDaoHibernateImpl.update(ActypeDaoHibernateImpl.java:57)
   at com.eflight.eloadsheet.service.impl.ActypeServiceImpl.update(ActypeServiceImpl.java:87)
   at com.eflight.eloadsheet.actypecrudejb.ActypeCrudBean.update(ActypeCrudBean.java:67)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 04, 2005 2:54 pm 
Beginner
Beginner

Joined: Sun Jan 16, 2005 5:45 pm
Posts: 24
Location: Atlanta
Ok, another post mentioned placing "update = "false" in the key element. I did and this fixed my problem. I don't understand why though.


Code:
        <set name="cgCharts" lazy="true" cascade="all-delete-orphan">
            <key column="AC_ID" update="false" />
            <one-to-many class="Chart" />
        </set>


I RTFM and still can't see the reasoning here:

Quote:
6.1.19. key
We've seen the <key> element crop up a few times now. It appears anywhere the parent mapping element defines a join to a new table, and defines the foreign key in the joined table, that references the primary key of the original table.

<key
column="columnname" (1)
on-delete="noaction|cascade" (2)
property-ref="propertyName" (3)
not-null="true|false" (4)
update="true|false" (5)
unique="true|false" (6)
/>
(1) column (optional): The name of the foreign key column. This may also be specified by nested <column> element(s).

(2) on-delete (optional, defaults to noaction): Specifies whether the foreign key constraint has database-level cascade delete enabled.

(3) property-ref (optional): Specifies that the foreign key refers to columns that are not the primary key of the orginal table. (Provided for legacy data.)

(4) not-null (optional): Specifies that the foreign key columns are not nullable (this is implied whenever the foreign key is also part of the primary key).

(5) update (optional): Specifies that the foreign key should never be updated (this is implied whenever the foreign key is also part of the primary key).

(6) unique (optional): Specifies that the foreign key should have a unique constraint (this is implied whenever the foreign key is also the primary key).


I think that this may be a bug... On an insert I would want the foreign key updated... I will have to look into this further.....

Any comments?

Thanks in advance.. Paul


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 8:05 pm 
Beginner
Beginner

Joined: Tue Jan 11, 2005 5:50 am
Posts: 43
Location: Zurich (Suisse)
Hi pbaker

I cannot retrieve the source I had, so I cannot tell you in detail. But what I did was to mark one side as inverse=true, this does not make sense for me but this way it worked.

Regards
Tarik


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