-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: children objects do not get deleted in Database
PostPosted: Mon Mar 13, 2006 10:33 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
We have the following problem: our child objects do not get removed from the database.

Using Hibernate we persist objects of a class called "Aanvraag", this
class contains a list of other objects of the type "BijzondereBepaling".
When we remove "BijzondereBepaling" objects from this list they do not get
deleted in our database (Sybase). If we add elements to the list they do get added
in our database.

The relation between "Aanvraag" and "BijzondereBepaling" is "one to many"

We tried to follow the instructions from the reference manual paragraph 21.1 and
we've tried all suggestions mentioned in the FAQ and this forum but none of
the suggestions works.

Some things we played around with:

- not-null settings
- inverse settings
- "all-delete-orphan" cascade settings

We do not explicitly delete the children objects in our code, but use the following code snippet:
Code:
   HibernateUtil.beginTransaction();
   HibernateUtil.getSession().saveOrUpdate(aanvraagObj);
   HibernateUtil.commitTransaction();
   HibernateUtil.closeSession();

In the reference guide it is suggested to explictly delete the children, but this is something
we want to avoid (we do not want to know if the user removed a child from the parent - it is all
abstracted away from us).

We are Hibernate newbies and a bit in the dark, so can anyone help us with this problem?




-------------- AANVRAAG CLASS SNIPPET ------------

Code:
public class Aanvraag extends AbstractBusinessObject implements Values {
    private List bijzondereBepalinglijst;
    .. etc ..

-------------- AANVRAAG HIBERNATE MAPPING FILE ------------
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 package="nl.snsbank.fom">
   <class name="Aanvraag" table="AANVRAAG" >
      <!-- properties van AbstractBusinessObject -->
      <id name="objectID" type="long">
         <column name="id" />
         <generator class="native" />
      </id>
      <bag name="bijzondereBepalinglijst"
         cascade="all-delete-orphan"  inverse="true">
         <key column="aanvraag_id"
             
              foreign-key="fk_fbb_a" />
         <one-to-many class="bijzondereBepaling"/>
      </bag>
      
      ..etc.
   

-------------- BIJZONDEREBEPALING HIBERNATE MAPPING FILE ------------

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 package="nl.snsbank.fom">
   <class name="BijzondereBepaling" table="BIJZONDEREBEPALING" >
      <!-- properties van AbstractBusinessObject -->
      <id name="objectID" type="long">
         <column name="id" />
         <generator class="native" />
      </id>


      <many-to-one name="aanvraag" column="aanvraag_id" not-null="true"/>
      
      <!-- properties van BijzondereBepaling -->
      <property name="tekst" type="text" />
      
      ..etc..
      



-------------- BIJZONDEREBEPALING TABLE CREATION SCRIPT ------------

Code:
create table dbo.BIJZONDEREBEPALING (
id numeric(19,0)
identity not null,
aanvraag_id numeric(19,0) not null,

GO


PRINT 'PRIMARY KEY CONSTRAINT(S)'
GO

ALTER TABLE dbo.BIJZONDEREBEPALING ADD primary key clustered (id)

GO


PRINT 'FOREIGN KEY CONSTRAINT(S)'
GO

ALTER TABLE dbo.BIJZONDEREBEPALING ADD constraint fk_fbb_a
foreign key (aanvraag_id)
references dbo.AANVRAAG (id)


GO



      
      


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 11:35 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
Did you try to remove the inverse="true" from the bag definitions and move it to the many-to-one!?

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 4:11 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Quote from the hibernate reference:

Quote:
Note that single valued associations (many-to-one and one-to-one associations) do not support orphan delete.


http://www.hibernate.org/hib_docs/v3/reference/en/html_single/

see also:
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-transitive

simon


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 6:27 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
hello,

unfortunatly, this solution doesn't work. we got the following stacktrace:


Code:
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:399)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
... 44 more
Caused by: org.xml.sax.SAXParseException: Attribute "inverse" must be declared for element type "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.addDTDDefaultAttrsAndValidate(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source) at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source) at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(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:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:398)
... 45 more
.
org.hibernate.MappingException: Error reading resource: ...../FomzBijzondereBepaling.hbm.xml



urs_b wrote:
Hello,
Did you try to remove the inverse="true" from the bag definitions and move it to the many-to-one!?

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 10:32 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
The question is, if you really need a bidirectional relationship.
Do the two entities have a lifecycle on their own?
Or is it more a parent/child relationship?
If so, you can remove the many-to-one relationship.

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 11:02 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
thanx for your reaction, but unfortunatly this doesn't work also. Your suggestion was our starting point where it all began ;-)


urs_b wrote:
Hello,
The question is, if you really need a bidirectional relationship.
Do the two entities have a lifecycle on their own?
Or is it more a parent/child relationship?
If so, you can remove the many-to-one relationship.

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 1:26 pm 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
I did not read your problem description well enough.
Anyway, delete commands are issued for children, when a child is removed from the collection if the cascade-delete option is used.

So, if you do something like this:

Code:
aanvraag.getBijzondereBepalinglijst().clear();
or
aanvraag.getBijzondereBepalinglijst().remove(child);


the objects should get deleted, because you have casade="all,delete-orphan" in your mapping.

If it still does not work, it might be that your equals() method of your BijzondereBepaling class is not correct (business key equality!).

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 5:01 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
hello,

Is it really neccesairy to call
Code:
aanvraag.getBijzondereBepalinglijst().clear();
or
aanvraag.getBijzondereBepalinglijst().remove(child);


What we want is the following:
At Timestamp T1, we have an aanvraag with a bijzonderebepalingen list with 3 elements.
At Timestamp T2, we have an aanvraag with a bijzonderebepalingen list with 2 elements (both were in the list at T1).
At timestamp T3 we execute the following code:
Code:
HibernateUtil.beginTransaction();
HibernateUtil.getSession().saveOrUpdate(nwAanvraag);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();


What we do not want is to call the clear of remove methods explicitly.
Is there a way to avoid this?

ps. our equals method is implemented correctly, according to us.



urs_b wrote:
Hello,
I did not read your problem description well enough.
Anyway, delete commands are issued for children, when a child is removed from the collection if the cascade-delete option is used.

So, if you do something like this:

Code:
aanvraag.getBijzondereBepalinglijst().clear();
or
aanvraag.getBijzondereBepalinglijst().remove(child);


the objects should get deleted, because you have casade="all,delete-orphan" in your mapping.

If it still does not work, it might be that your equals() method of your BijzondereBepaling class is not correct (business key equality!).

Hope this helps!
Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 5:08 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello,
I do not understand. How did you remove the one element between T1 and T2 from the list? Maybe you should show this code snipet as well.

Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 10:23 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
Hello Urs,

Thank you for your efforts.

An element from the list is removed using the remove() method.


Code:
public class Aanvraag extends AbstractBusinessObject implements FomzValues {
  private List bijzondereBepalinglijst;
..etc..


We have a web application in which a user can remove elements (children) from a collection (List)
that is contained within an other object (parent). The user can remove from and add elements to
this collection. Objects get persisted when the user presses a 'save' button.

Regards and thanks,

Marco


urs_b wrote:
Hello,
I do not understand. How did you remove the one element between T1 and T2 from the list? Maybe you should show this code snipet as well.

Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 10:34 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
marcodeklijn wrote:
An element from the list is removed using the remove() method.

Well thats what I ment in my previous post. You remove objects from the collection with remove(). This will delete the object from the database.
Code:
aanvraag.getBijzondereBepalinglijst().remove(child);


Urs


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 10:34 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

1. How exaclty to you add a child ? (paste the code).

2. Somebody had to tell Hibernate - delete x child. Who do you want to be that somebody ? (because somebody has to do it).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 15, 2006 11:09 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hi,
Code:
// add a child
parent.addChild(child);

// remove a child
// you can set a breakpoint in your equals() method of child
// and verify, if it is found in the list and the remove is really done.
// If it is done, a delete will be issued later, if you have casade="all,delete-orphan" in your mapping
parent.getChildren().remove(child);


class Parent {
  Set children = new HashSet();
  public void setChildren(Set childs) {
    children = childs;
  }
  public Set getChildren() {
    return children;
  }

  public void addChild(Child child) {
    children.add(child);
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 5:46 am 
Newbie

Joined: Mon Mar 13, 2006 10:28 am
Posts: 6
Hi Urs_b,

Thanks for being so persistent :-)

Why would we have to manually / explicitly tell Hibernate to remove objects from the list, while
we do not have to tell Hibernate that it has to add new objects ?

We prefer not to know about what happens inside the collection and let Hibernate figure it out
for itself if it has to insert, update or delete entries. The reasons for this are that
a) we are lazy, and b) we use a rather obscure framework which, during the handling of a user
request, converts all relevant objects to xml and then back from xml to objects again. We actually
do not know if an object has been removed or added to a collection. Unless we compare what
is in the database to what we have in memory - an undesirable thing to do.

Many regards,

Marco


urs_b wrote:
Hi,
Code:
// add a child
parent.addChild(child);

// remove a child
// you can set a breakpoint in your equals() method of child
// and verify, if it is found in the list and the remove is really done.
// If it is done, a delete will be issued later, if you have casade="all,delete-orphan" in your mapping
parent.getChildren().remove(child);


class Parent {
  Set children = new HashSet();
  public void setChildren(Set childs) {
    children = childs;
  }
  public Set getChildren() {
    return children;
  }

  public void addChild(Child child) {
    children.add(child);
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 6:09 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Hello Marco,
Sorry, but we are not getting any further, not understanding each other somehow.
Quote:
Why would we have to manually / explicitly tell Hibernate to remove objects from the list, while
we do not have to tell Hibernate that it has to add new objects ?

We are not telling hibernate, we are telling our business objects to remove/add children!!

Urs


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.