-->
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: Many to one unidirectional from the many side
PostPosted: Tue Jan 10, 2006 11:50 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
Hi, I have a many-to-one relation from attribute to component. I want to preserve the component class unchanged but having a link from attribute to component. The problem comes when I try to delete a component that has an attribute linked to it. It gives me an exception when i delete the component object. One strange thing is the type of Exception: GenericJDBCException: Could not execute JDBC batch update
Is any problem in having a unidirectional from the many side and having to cascade delete when deleting the one side? It looks like it's not cascading the delete from the one side, but how can I tell it to cascade if I don't have a many attribute in the one side?

Hibernate version: 3.1

Mapping documents:

<hibernate-mapping>

<class name="com.yell2.enhancers.termenhancers.Attribute" table="attribute">
<id name="id" type="long">
<column name="id" not-null="false" />
<generator class="increment" />
</id>

<property name="type" column="type" />
<property name="value" column="value" type="java.lang.String" length="5000" />

<many-to-one cascade="all" name="component" class="com.yell2.core.enhancer.Component"
column="component_id" insert="true" update="true" />


</class>
</hibernate-mapping>


<class name="com.yell2.core.enhancer.Component" table="component" lazy="true">
<id name="id" type="long">
<column name="id" not-null="false" />
<generator class="increment" />
</id>
<!-- i took some irrelevant properties from here-->

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():


Session session = com.yell2.core.enhancer.HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.delete(comp);
tx.commit();
HibernateUtil.closeSession();

Full stack trace of any exception that occurs:

EVERE: failed batch
10-Jan-2006 15:40:05 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
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:145)
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 com.yell2.core.server.repository.hibernateimplementation.HibernateRepository.deleteComponent(HibernateRepository.java:90)
at com.yell2.enhancers.termenhancers.test.CompanyNameTermEnhancerTest.tearDown(CompanyNameTermEnhancerTest.java:40)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: failed batch
at org.hsqldb.jdbc.jdbcStatement.executeBatch(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(Unknown Source)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 19 more

Name and version of the database you are using:

HSQL 8.1

The generated SQL (show_sql=true):

Hibernate: insert into attribute (type, value, component_id, id) values (?, ?, ?, ?)
Hibernate: update component set created_date=?, status=?, status_msg=?, type=?, updated_date=?, value=? where id=?
Hibernate: delete from component where id=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 7:26 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,
I know that this is a little complicated at the beginning but actually it is easy
;-)


attribute.setComp(null);
/*
you must do this for each attribute holding a link to the comp
because
Hibernate will not look through the session for attributes holding a link and then set the field comp to null.

alternative, which may leed to other problems (stale data in other sessions)
cascade defined in your database
clear session
attribute.setComp(null); for your current attribute

*/
session.delete(comp);

_________________
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: Tue Jan 10, 2006 7:27 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Please rate, if the post helped.

_________________
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: Still didn't get it
PostPosted: Wed Jan 11, 2006 7:23 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 8:00 pm
Posts: 22
hi LaLiLuna

I still didn't get it. You you mean is that if I don't have a link from component to attribute I can't have the cascading delete from parent to child (component-attribute)?
The reason the link is unidirectional is that I want to have the component independent from future classes (and tables) that depend on them, but at same time not having orphan attributes children...

Emerson


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 7:46 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
yes, you understood it. ;-)

attrib.comb link to comp
if you delete comp
you must find a way to set the attr.comb to null or delete the attr
you can do this in the database if it supports
or by issuing the query with Hibernate.

Code:
ALTER TABLE examples.test
  ADD CONSTRAINT test_test2_id_fkey FOREIGN KEY (test2_id)
      REFERENCES examples.test2 (id) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE set null;

Code:
or
ALTER TABLE examples.test DROP CONSTRAINT test_test2_id_fkey;

ALTER TABLE examples.test
  ADD CONSTRAINT test_test2_id_fkey FOREIGN KEY (test2_id)
      REFERENCES examples.test2 (id) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE cascade;

_________________
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  
 
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.