-->
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.  [ 3 posts ] 
Author Message
 Post subject: lost in cascade
PostPosted: Wed Mar 02, 2011 5:41 am 
Newbie

Joined: Wed Mar 02, 2011 5:21 am
Posts: 1
Hi there,

I'm quite lost with the cascade.

I will simplify my stuff in order to ask, so the example is quite stupid :)
I have 3 classes : Human, Clothes and Dressing-Room

So basically the implementation is something like
Code:
@Entity @Table @AndSoOn
class Human {
    private List<Cloth> clothes;

    @OneToMany(mappedBy = "human", cascade = CascadeType.ALL ?, fetch = FetchType.LAZY)
    public List<Cloth> getClothes()
    {
        return clothes;
     }
}

@Entity @Table @AndSoOn
class DressingRoom {
    private List<Cloth> clothes;

    @OneToMany(mappedBy = "dressingRoom", cascade = CascadeType.???, fetch = FetchType.LAZY)
    public List<Cloth> getClothes()
    {
        return clothes;
     }
}

@Entity @Table @AndSoOn
class Cloth {
    private Human human;
    private DressingRoom dressingRoom;

    @ManyToOne
    public Human getHuman()
    {
        return human;
     }

   @ManyToOne
    public DressingRoom getDressingRoom()
    {
        return dressingRoom;
     }
}


What are the good cascade annotations for the following workflow :
If a Human is deleted all his clothes are deleted also
If a DressingRoom is deleted, the clothes are not deleted (dressingRoom become null)
A single Cloth can, of course, be deleted without any impact neither on Human or DressingRoom (I guess I have to play with deleteOrphan for this one, but where ?)

Thanks for any help you could give me


Top
 Profile  
 
 Post subject: Re: lost in cascade
PostPosted: Wed Mar 02, 2011 9:53 pm 
Newbie

Joined: Mon Sep 24, 2007 8:11 am
Posts: 16
>>If a Human is deleted all his clothes are deleted also
I believe you want to use:
@Cascade( {org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
on the Human.clothes list. I believe this is better than the javax.persistence.CascadeType in the OneToMany annotation.

>>If a DressingRoom is deleted, the clothes are not deleted (dressingRoom become null)
No cascade here at all.

>>A single Cloth can, of course, be deleted without any impact neither on Human or DressingRoom (I guess I have to play with deleteOrphan for this one, but where ?)
No - Delete_Orphan will only delete a child object that is annotated with it when the parent object is deleted.


Top
 Profile  
 
 Post subject: Re: lost in cascade
PostPosted: Wed Mar 09, 2011 1:53 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

I disagree with the previous comment:

>>If a Human is deleted all his clothes are deleted also
Cascade.ALL on the OneToMany annotation

>>If a DressingRoom is deleted, the clothes are not deleted (dressingRoom become null)
Cascade.MERGE, Cascade.PERSIST, Cascade.UPDATE on the OneToMany annotation

>>A single Cloth can, of course, be deleted without any impact neither on Human or DressingRoom
Either remove the Cloth the parent collection and delete it yourself from the database
Or use Cascade.DELETE_ORPHAN and just remove it from the parent collection!


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