-->
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: cascade delete -orphan Problem with JoinTable Annotation
PostPosted: Sun Oct 11, 2009 3:08 pm 
Newbie

Joined: Sun Oct 11, 2009 2:49 pm
Posts: 3
I have a big object graph with some association within some objects.I am facing problem with deletion.

My annotation as below.

@Entity
@Table(name = "t_person")
public final class Person
{
.....................
......................

@OneToMany(fetch = FetchType.LAZY)
@Cascade(
{
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
})
@JoinTable(name = "t_per_cars", joinColumns =
{
@JoinColumn(name = "fk_to_person")
})
@IndexColumn(name = "car_list_position", base = 1)
private List<Car> cars = new ArrayList<Car>();
.........................................
..........................................

}

@Entity
@Table (name="t_car")
@GenericGenerator(name="car-uuid", strategy="uuid",
parameters={@Parameter(name="separator", value="-")})
public class Car implements Vehichel
{
..............
// Many other association
...............
}


in that case it generates the three tables between Person and Car object
t_person,t_per_cars,t_car

I need to delete the Person object as well as all the cascade object with related to persons
In my case all the related cars object with the Person.
But above mapping just remove the reference between person and car object .It didn't delete the actual car object from the t_car table.

So what should I make a change to delete the all object which are associate with person object while I am going to delete the person object??

There are more and more orphan data in my DB.

Is there anyway hibernate handle that ??


Top
 Profile  
 
 Post subject: Re: cascade delete -orphan Problem with JoinTable Annotation
PostPosted: Sun Oct 11, 2009 4:37 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi jigu4paint,

I'm not sure if
Code:
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
works stand alone without
Code:
org.hibernate.annotations.CascadeType.DELETE
or
Code:
org.hibernate.annotations.CascadeType.ALL

Anyway, you post
jigu4paint wrote:
Code:
public class Car implements Vehichel {
  ..............
  // Many other association
  ...............
}
So, your car instances may have other associations which keeps them from being deleted?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: cascade delete -orphan Problem with JoinTable Annotation
PostPosted: Sun Oct 11, 2009 4:46 pm 
Newbie

Joined: Sun Oct 11, 2009 2:49 pm
Posts: 3
So you mean I required DELETE in case to use DELETE_ORPHAN??

@OneToMany(fetch = FetchType.LAZY)
@Cascade(
{
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
})
@JoinTable(name = "t_per_cars", joinColumns =
{
@JoinColumn(name = "fk_to_person")
})
@IndexColumn(name = "car_list_position", base = 1)
private List<Car> cars = new ArrayList<Car>();


Anyway, you post
jigu4paint wrote:
Code:
public class Car implements Vehichel {
..............
// Many other association
...............
}
So, your car instances may have other associations which keeps them from being deleted?


Yes , just like Person and car association and many more..

for example.

public class Car implements Vehichel {

......................


@CollectionOfElements(targetElement = Part.class)
@JoinTable(name = "t_carpats", joinColumns = { @JoinColumn(name = "car_id", nullable=false) })
@MapKey(columns = @Column(name="part_id", nullable=false))
@Column(name = "partname")
public Map<String, Part> part = new HashMap<String,Part>();


// Many other association
...............
}


Top
 Profile  
 
 Post subject: Re: cascade delete -orphan Problem with JoinTable Annotation
PostPosted: Sun Oct 11, 2009 5:06 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
jigu4paint wrote:
So you mean I required DELETE in case to use DELETE_ORPHAN??

@OneToMany(fetch = FetchType.LAZY)
@Cascade(
{
org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE
org.hibernate.annotations.CascadeType.DELETE_ORPHAN
})
@JoinTable(name = "t_per_cars", joinColumns =
{
@JoinColumn(name = "fk_to_person")
})
@IndexColumn(name = "car_list_position", base = 1)
private List<Car> cars = new ArrayList<Car>();


Maybe, I don't know - try.

jigu4paint wrote:
Yes , just like Person and car association and many more..

for example.

public class Car implements Vehichel {

......................


@CollectionOfElements(targetElement = Part.class)
@JoinTable(name = "t_carpats", joinColumns = { @JoinColumn(name = "car_id", nullable=false) })
@MapKey(columns = @Column(name="part_id", nullable=false))
@Column(name = "partname")
public Map<String, Part> part = new HashMap<String,Part>();


// Many other association
...............
}

... and possibly one of these collections prevents car from being deleted. Does not look like you have a cascading delete down to the part collection. Since that one joins on the car_id column, not to be nullable, deletion of cars does not work.

CU
Froestel

btw: Please use BBCode - see buttons below the subject field - makes your posts much easier to read if used properly.

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: cascade delete -orphan Problem with JoinTable Annotation
PostPosted: Mon Oct 12, 2009 6:40 pm 
Newbie

Joined: Sun Oct 11, 2009 2:49 pm
Posts: 3
I tried with your opinion with DELETE which is not working.


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.