-->
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: Problem with CascadeType.DELETE_ORPHAN annotation
PostPosted: Thu Mar 12, 2009 11:49 am 
Newbie

Joined: Tue Mar 10, 2009 12:06 pm
Posts: 4
Hello!!

I'm using Hibernate 3 with annotations and I use it with Spring's Hibernate Template.

Here are two classes I use:

public class Car{
...
}

public class Driver{
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="driver")
@Cascade(value=CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "driver_id")
protected List<Car> cars;
}

Well, I've omitted some code, that doesn't have to do with my problem (as far as I can see).

When I remove some cars from a driver I've got no problems. But when I remove ALL OF THEM and the driver has no car, Hibernate doesn't delete them at all. They're there in the DB. If later I add one or more cars to this driver, the previously remove cars get deleted from the database.

It seems DELETE_ORPHAN it only works when I don't remove ALL the cars. It works well as long as I leave at least one. Did anybody know this issue? Am I doing anything wrong?

Thank you very much!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 6:42 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
Similar code works for me. Anything special with the way you are removing the car?
cars.remove(<a car object retrieved from the collection>)
cars.remove(0)

LinHib.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 4:11 am 
Newbie

Joined: Tue Mar 10, 2009 12:06 pm
Posts: 4
This is the part of my code that tries to remove the cars from the driver.

this.driver.getCars().removeAll(carsToDelete);
this.hibernateTemplate.saveOrUpdate(this.driver);

... and it works, as long as "carsToDelete" has not all the cars in "driver.cars". I mean, it works as long as I don't ask it to delete all its cars.

But even if I do, it will work if then I do this:

this.driver.getCars().add(new Car());

Now all the previous cars will be deleted from DB and only de new car will remain. It's like Hibernate needed to have at least one car in the collection to delete the others that are no longer in any collection.

Does anybody know why?

Thanks!


Top
 Profile  
 
 Post subject: Re: Problem with CascadeType.DELETE_ORPHAN annotation
PostPosted: Fri Jun 19, 2009 7:30 am 
Newbie

Joined: Fri Jun 19, 2009 7:20 am
Posts: 3
Code:
@Entity
@Table(name="Request")
public class Request implements Serializable {

private Long id;
private List<EmailValue> emailValues;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="request_id")
   public Long getId() {
      return id;
   }
   
   public void setId(Long id) {
      this.id = id;
   }

@OneToMany(fetch= FetchType.EAGER)
@JoinTable(name = "RequestEmailValue",
      joinColumns = {@JoinColumn(name="request_id")},
      inverseJoinColumns = {@JoinColumn(name="emailvalue_id")}
)
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public List<EmailValue> getEmailValues() {
   return emailValues;
}

public void setEmailValues(List<EmailValue> emailValues) {
   this.emailValues = emailValues;
}

Code:
@Entity
@Table(name = "EmailValue")
public class EmailValue implements Serializable{

   private Long id;
   private String value;
   
   public EmailValue() {
   }

   public EmailValue(String value) {
      this.value = value;
   }

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "emailvalue_id")
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   @Column(name = "value", nullable = false)
   public String getValue() {
      return value;
   }

   public void setValue(String value) {
      this.value = value;
   }
}



Nobody find the solution for this issue , i m in the same case.

I can delete a element in my collection WORKS

Code:
Request req = getSession().get(Request.class,new Long(id));
req.getEmailValues.remove(0);
getSession().update(req);



but if i CANT delete ALL the collection

Code:
Request req = getSession().get(Request.class,new Long(id));
req.setEmailValues(new ArrayList());
getSession().update(req);


i try to put a hashset the same behaviour.
thanks


Top
 Profile  
 
 Post subject: Re: Problem with CascadeType.DELETE_ORPHAN annotation
PostPosted: Mon Jun 22, 2009 5:52 am 
Newbie

Joined: Fri Jun 19, 2009 7:20 am
Posts: 3
I found the solution it is not a bug,

viewtopic.php?f=1&t=997727&p=2413725

Just add before your update

Code:
req = getSession().merge(req);
getSession().update(req);


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.