3

Can any one explain me the hibernate annotation how to use different types of cascade like delete,upadte,save-update?

How can I make sure that when an Owner is deleted, its car is deleted as well (but not the other way around)

 @Entity
    public class Owner
    {
        @OneToOne(cascade=CascadeType.ALL)
        private DrivingLicense license;

        @OneToMany(mappedBy="owner", cascade={CascadeType.PERSIST, CascadeType.MERGE})
        private Collection cars;

        ...
    }

    @Entity
    public class DrivingLicense
    {
        private String serialNumber;

        ...
    }

    @Entity
    public class Car
    {
        private String registrationNumber;

        @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE})
        private Owner owner;

        ...
    }
Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
user3214269
  • 219
  • 2
  • 8
  • 23
  • 4
    may know why u guys are down voted to my quetion? I have searched lot to find the answer for this in google but i dint understand how to use – user3214269 Jan 20 '14 at 09:50
  • i read that one that all are there in xml mapping not in annotation if u send me the link of that that would be great. – user3214269 Jan 20 '14 at 09:55
  • This one is good: http://www.datanucleus.org/products/datanucleus/jpa/orm/cascading.html – kostja Jan 20 '14 at 10:00
  • "Did not understand" is too broad a problem. In order to get a good answer, the question must be focused. If you have found a particular part that you do not understand, ask about that very part, perferably with a code example. If you have not understood the tutorial in principle, StackOverflow won't help much - you will have to go back a step and repeat the introductory material. – kostja Jan 20 '14 at 10:07
  • I understand little bit from your link i have doubt regarding that if i delete the owner i want to delete car also but if delete car i dont want to delete owner u to do that. – user3214269 Jan 20 '14 at 10:22

1 Answers1

2

The pitfall here is that there are two CascadeType enums. One is of javax.persistence, the other is from hibernate. In general I would prefer to use the persistence one.

For cascade update, keep in mind that "update" is a hibernate term, and jpa doesn't know this. Jpa doesnt need it, because in JPA the idea is that your updated fields will flush to the database automatically. If you made any changes in relations, those will flush as well. So you dont need a cascade on update.

For save-update, this is a hibernate shortcut to using either persist or update depending on the state of the object. Since you're already covering persist and update(see above), you dont need a cascade on this.

To cascade a delete, you probably want to use @OrphanRemoval instead. This will make sure that if the parent of a relation is removed, the child is gone, too (but not the other way around).

    @OneToMany(mappedBy="owner", cascade={CascadeType.PERSIST, CascadeType.MERGE})
    @OrphanRemoval
    private Collection cars;
Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53