-->
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.  [ 4 posts ] 
Author Message
 Post subject: create one-to-many with ConcurrentModificationException
PostPosted: Sun Aug 07, 2005 5:55 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
I think this is an one-to-many with proxied object setting problem ...

The scenario : Item and Series have bi-directional many-to-one relation , that is :
one Item may belong to ZERO (not-found="ignore") or ONE Series ,
one Series may contain ZERO or MANY Items.

In Item.java , I have one method : setSeries(Series s)
Code:
  public void setSeries(Series s)
  {
    if (getSeries() != null)
      getSeries().getItems().remove(this);
    series = s;
    if (s != null)
      s.getItems().add(this);
  }



In Series.java , I have one method : addItem(Item item)
Code:
  public void addItem(Item item)
  {
    if (item == null)
      throw new IllegalArgumentException("Null item");
    items.add(item);
    item.setSeries(this);
  }


When I create one Series with more (or equal to) 2 items , it will throw ConcurrentModificationException
(Creating series with one item will be OK)
The code is below:

Code:
    Series series1 = new Series();
    series1.setName("series1");

    Item item1 = new Item();
    item1.setNumber("item1");
    Item item2 = new Item();
    item2.setNumber("item2");
   
    series1.addItem(item1);
    series1.addItem(item2);
   
    seriesDao.create(series1);  <-- This is where the exception was thrown

SeriesDao is just a class extends HibernateDaoSupport (from Spring) and implements AbstractDao<T> (just CRUD actions)
Code:
public abstract class AbstractHibernateDao<T> extends HibernateDaoSupport implements AbstractDao<T>
{
  ...
  public Serializable create(T po)
  {
    return getHibernateTemplate().save(po);
  }
  ...
}


It seems the problem occurs because of iterating the relationsip of proxied_Series and proxied_Items.

I know it can be solved with access="field" in <many-to-one name="series" ...> (and I really solved it with this)
But I think access="field" is a little ... 'dirty' ....

Is there any way to get out of the Exception without access="field" ???

Thank you very much.



Hibernate version:3.0.5

Mapping documents:
Item :
Code:
<class name="Item">
  <many-to-one name="series" column="SeriesID" class="Series" lazy="true" cascade="save-update" not-found="ignore"/>
</class>


Series :
Code:
<class name="Series">
  <set name="items" lazy="true" inverse="true" cascade="save-update">
    <key column="SeriesID"/>
    <one-to-many class="Item"/>
  </set>
</class>



Full stack trace of any exception that occurs:
Code:
ERROR - onTearDown error
java.util.ConcurrentModificationException
   at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
   at java.util.HashMap$KeyIterator.next(Unknown Source)
   at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:366)
   at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
   at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
   at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:490)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:495)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:468)
   at org.springframework.test.AbstractTransactionalSpringContextTests.endTransaction(AbstractTransactionalSpringContextTests.java:206)
   at org.springframework.test.AbstractTransactionalSpringContextTests.onTearDown(AbstractTransactionalSpringContextTests.java:152)
   at org.springframework.test.AbstractDependencyInjectionSpringContextTests.tearDown(AbstractDependencyInjectionSpringContextTests.java:253)
   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 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)

Name and version of the database you are using:MySQL 3.23.58


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 6:14 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
Quote:
Is there any way to get out of the Exception without access="field" ???


You should either modify Item.setSeries() method to simple setter (in which do not modify properties of Series object) or use access="field". I don't think, that direct field access by Hibernate is a wrong way - standart serialization is using this method and nobody complains about this.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 6:33 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
sergeya wrote:
Quote:
Is there any way to get out of the Exception without access="field" ???


You should either modify Item.setSeries() method to simple setter (in which do not modify properties of Series object) or use access="field". I don't think, that direct field access by Hibernate is a wrong way - standart serialization is using this method and nobody complains about this.


Thank you for replying so quickly.

Code:
  public void setSeries(Series s)
  {
    if (getSeries() != null)
      getSeries().getItems().remove(this);
    series = s;
    if (s != null)
      s.getItems().add(this);
  }


The reason I wrote Item.setSeries(Series s) like this is to tear-down the relationship between old Series and Item.

I hope anyone can just call Item.setSeries(Series) to setting or replacing it's series.

If the relationship with old series is not correctly torn down , it may throw exception like 'deleted object would be re-saved by cascade'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 5:53 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
smallufo wrote:
I hope anyone can just call Item.setSeries(Series) to setting or replacing it's series.

If the relationship with old series is not correctly torn down , it may throw exception like 'deleted object would be re-saved by cascade'


You want too many from one method. :-)

Firstly, you want from user code establish relationships between entity - this is one task. Second, at time of loading from DB, Hibernate also needed ability to restore these links. But Hibernate don't know about side behavoir of setter. It's supposed that setter wouldn't concerning about maintaing relationships between entity - it is task of Hibernate.

You should separate these concerns - or by using two methods or by using access="field".


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