-->
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: Many to One - How to save or delete
PostPosted: Thu Jan 04, 2007 1:59 pm 
Newbie

Joined: Tue Aug 16, 2005 9:21 am
Posts: 12
Hi,

I'm get following problem: I have two classes, one the Main object and another the Child. In the Main object I have a Collection of Child.

When I delete or insert a Child from Collection and save the Main object, hibernate doesn't insert or delete row in the Child table, but when I just update an Object that already exists, this one is corrected updated in the table.

Any help is welcome.

Thx
Rodrigo Souza
Brazil - Sao Paulo


Following my Objects with its annotations:

Main Object
Code:
@Entity
@SequenceGenerator(name = "SQ", sequenceName = "sq_person")
@Table(name = "person")
public class PersonVO extends AbstractVO {

...

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "SQ")
   public Integer getId() {
      return id;
   }

...

   @OneToMany(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   public Set<AddressVO> getAddresses() {
      return addresses;
   }

   public void setAddresses(Set<AddressVO> address) {
      this.address = address;
   }

}


Child Objec
Code:
@Entity
@Table(name = "person_address")
@SequenceGenerator(name = "SQ", sequenceName = "sq_person_address")
public class AddressVO extends AbstractVO {

...

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "SQ")
   public Integer getId() {
      return id;
   }

...

   @ManyToOne (cascade = CascadeType.ALL )
   public PersonVO getPerson() {
      return person;
   }

   public void setPerson(PersonVO person) {
      this.person = person;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 04, 2007 4:38 pm 
Newbie

Joined: Mon Jul 24, 2006 1:35 pm
Posts: 10
Hi,

This is just a quick note: check your mapping files for cascade attribute. Make sure it's save or update. Updates and inserts should work fine for children (unless child class has problems with pk generation during insert, but that's a different story). I don't think Hibernate actually deletes children via cascade. Check the docs - I could be wrong but I think you have to do something explicit to actually remove child records.

HTH,

Bratek


Top
 Profile  
 
 Post subject: Did you ever find a solution?
PostPosted: Tue Jan 09, 2007 11:57 am 
Newbie

Joined: Tue Jan 09, 2007 11:32 am
Posts: 2
Rodrigo,

I wanted to see if you ever found a solution since I am having the same exact problem.

Thanks in adavnce,

Keith


Top
 Profile  
 
 Post subject: Re: Many to One - How to save or delete
PostPosted: Tue Jan 09, 2007 12:56 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
rodrigo.bmts wrote:
Hi,
When I delete or insert a Child from Collection and save the Main object, hibernate doesn't insert or delete row in the Child table, but when I just update an Object that already exists, this one is corrected updated in the table.

Just to be sure:
When you say 'update an Object that already exists', you mean changing children on an object fetched by Hibernate (and are you adding or only deleting children? - behavior may differ)
When you say 'save the Main', you mean a new Main object, with a new set of children, saved into the database?

Parhaps you forgot to set the Child backpointer to the main object when
adding a new child?
Code:
public class PersonVO extends AbstractVO {
...
public void setAddresses(Set<AddressVO> address) {
    this.address.clear();
     // Assure that Person backpointers are set prior to adding Address to our set
    for (AddressV0 addr: address) {
         addAddress(addr);
    }
}

public void addAddresses(AddressVO  addr) {
     // Set the Person backpointer PRIOR to adding to the set
    addr.setPerson(this);
    this.address.add(addr);
}
}

Note that (not knowing about your implementation of equals(), hashCode()), I am assuming that hashCode may change when setting Person backpointer.

W/regards to mapping, I always try to be explicit:

cascade="save-update,delete,all-delete-orphan,evict"

Last but not least ... sets are nice, but with Hibernate, you must be carefull - using other collections is less risky (Set does not expect any member modifications - which may change hashCode() value).


Do not forget to rate!


Top
 Profile  
 
 Post subject: That did it
PostPosted: Tue Jan 09, 2007 3:06 pm 
Newbie

Joined: Tue Jan 09, 2007 11:32 am
Posts: 2
That was the answer thanks. I tried to rate the post but can't find anyway to do it as the rating links seem to be missing.

Keith


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 3:46 pm 
Regular
Regular

Joined: Wed Dec 07, 2005 4:19 pm
Posts: 53
Only the original poster (rodrigo) can rate the answer


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.