-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem mapping m-n association object as table
PostPosted: Thu Dec 18, 2003 4:46 pm 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
Hi,

I'm trying to, for various reasons, map an association object as a table. The table and object graph looks like this:

Code:
A -- * B ---* C
       |
       *
      BtoD * -- D


Here's the mapping on the relationship collections (psuedo just showing the cascades 'cause I'm convinced that's what's most importent):

Every class is using uuid.hex as id generator and null as unsaved strategy.
Code:
<class name="A" table="A">

  <set name="bs" inverse="true" cascade="all">
  </set>
</class>

<class name="B" table="B">

  <many-to-one name="A" column="A" class="A" cascade="save-update"/>

  <set name="cs" cascade="all">
  </set>

  <set name="BtoD" cascade="all">
  </set>
</class>

<class name="C" table="C">
 
  <many-to-one name="B" column="B" class="B" cascade="save-update"/>
</class>

<class name="BtoD" table="BtoD">

  <many-to-one name="B" column="B" class="B" cascade="save-update"/>

  <many-to-one name="D" column="D" class="D" cascade="all"/>
</class>

<class name="D" table="D">

  <set name="BtoD" cascade="all">
  </set>
</class>


The topmost object in the graph is the object A. This object is loaded from DB to UI in one session.
In UI, object A gets mofified and then sent down to a business object method trying to update the changes and delete one object from the graph looking like this:

Code:
public void deletaAnObject(Object objectToDelete) {

  Transaction txn = session.beginTransaction();

  session.update(updateTopmostObjectA);

  D theDObjectIdLikeToRemove = updateTopmostObjectA.getObjectD();

  // Done to remove itself from all associations
  theDObjectIdLikeToRemove.delete();

  session.delete(theDObjectIdLikeToRemove);

  txn.commit();
}


In my object D I invoke a method delete() just before session.delete(objectD);

That method does the following:

Code:
public void delete() {
  Set myBtoDObjects = this.getBtoDObjects();
  Iterator BtoDIterator = myBtoDObjects.iterator();
  while (BtoDIterator.hasNext()) {

    // This method is inoked on BtoD object to remove itself from any association
    ((BtoD) BtoDIterator.next()).delete();
  }
 
  myBtoDObjects.clear();
}


And the delete() method in BtoD object looks like this:

Code:
public void delete() {
  Set BtoDFromParentObjectB = this.getParentObjectB().getMyChildsBtoD();
  BtoDFromParentObjectB.remove(this);

  Set BtoDFromParentObjectD = this.getParentObjectD().getMyChildsBtoD();
  BtoDFromParentObjectD.remove(this);
 
}


I can se that the delete is performed, and when the session gets flushed I get the commonly "cascade during flush is dangerous".

I've tried to change the cascading, remove every possible (I think) associated object, using inverse on both B set and D set vice-versa and so on etc. (According to the docs and posts in this forum) without any luck.

I'm hoping that someone could give me some pointer on the problem!

Kind regards, Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 4:47 am 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
Maybe I'm not deleting/clearing all the collections I'll have to.

Or, maybe I'm supposed to do an explicit delete on object BtoD?

Since object B is updated it's cascading to saveOrUpdate() for its collections, am I right?

Regards, Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 4:55 am 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
Maybe I forgot; I'd like to delete D and it's related BtoD rows in db, B should not be deleted.

/Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
What happen if you flush session just after the object update ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 3:10 pm 
Regular
Regular

Joined: Wed Aug 27, 2003 2:55 am
Posts: 90
Location: Sweden
The thing is that I'm using Spring framework on top for transaction management.

Steve (at HIbernate) posted a DAO on this forum making sence, and I've been using it, somewhat modified.

My BO methos really looks like this (but transalated to Hibernate as above):

Code:
// DomainObject is a super interface of all VO's 'cause I've got 65 of them
// and don't want to specify 65 delete methods
public void deleteObject(final DomainObject domainObject) {
  TransactionTemplate template = new TransactionTemplate(
    this.setTransactionManager());
  return (Object) template.execute(new TransactionCallback() {
    public Object doInTransaction(TransactionStatus transactionStatus) {

      A myAObject = null;     

      // Do some changes on the domainObject which returns the topmost
      // object A
      myObjectA = doSomething(domainObject);
     
      // Try to re-associate the VO's with session
      update(myObjectA);

      //locate the proper dao for the domainObject and call delete on the
      // object passed as argument to this method
      dao.delete(domainObject);

      return myObjectA;
     
    }
  });
}


Don't know it's poosible from inside the above method to get hold of a Hibernate Session since I manage the session at DAO level.

I'v been trying EVERYTHING but all I get is:

"Flush is dangerous during cascade"
"Cannot insert NULL into 'some value in BtoD'"

Even tried to delete first the BtoD object (since it's a object mapped to a table and not a "real" association component).

Removed the cascades, then my inserts stops to function.

Re-started and re-configured step by step from A to D in my graph.

Re-read the docs over and over again.

I'm seriously not getting the cascade settings in mapping files and what will happen on when I call update/delete/save on my VO's.

I'm losing it!!!!

Regards, Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 4:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
eheh see my post on your architectural question

_________________
Emmanuel


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