aspose file tools*
The moose likes Object Relational Mapping and the fly likes EJB 3 CascadeType annotation. What for it is required exactly. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EJB 3 CascadeType annotation. What for it is required exactly." Watch "EJB 3 CascadeType annotation. What for it is required exactly." New topic
Author

EJB 3 CascadeType annotation. What for it is required exactly.

c shan
Greenhorn

Joined: Sep 30, 2010
Posts: 3
Hi All,

If I using on delete/update cascade on my DB , then is it required for me to use annotation at the entity level ? And if not used what will be the result?

Whenever a entity manager remove/update is called entity , finally the request goes to DB and DB performs the cascade operations if any. But if that's the case then I am not able to understand why CacadeType annotation are introduced?
Federico Nafria
Greenhorn

Joined: Sep 30, 2010
Posts: 9
I'll go with an example to make it easier:

Let's suppose we have the following classes: Person, Address and Country

a Person has one Address and an Address has one Country.

When you persist a Person, you want the Address to persist with it, don't you?

well, if you mark it with CASCADE.persist it will.

Now, what about country, we do not want to create a new country in the database every time we persist an Address. So we leave without changing the default CASCADE policy.

Hope it's been useful!







c shan
Greenhorn

Joined: Sep 30, 2010
Posts: 3
c shan wrote:Hi All,

If I using on delete/update cascade on my DB , then is it required for me to use annotation at the entity level ? And if not used what will be the result?

Whenever a entity manager remove/update is called entity , finally the request goes to DB and DB performs the cascade operations if any. But if that's the case then I am not able to understand why CacadeType annotation are introduced?



Looks like i didn't put my question in a right way.
What I mean is if cascade is supported at DB level , why do we need to add annotation on entity methods too ?

If I designed my DB schema having CASCADE on persist and CASCADE on delete TRUE and if delete master ID from DB (using ejb call ) then all the corresponding child records will be deleted automatically.

If I design my schema having CASCADE on persist and CASCADE on delete FALSE then now If try to delete the master ID from then will get error "Cannot delete or update a parent row: a foreign key constraint fails"

So, Its the DB who decides the cascade is to be supported or not then why should we write annotations on entity.

Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
If you have db cascades then you should not use cascading in your annotations. Of course, you still need the annotations (or XML) to map the state/relationships to the correct db columns, etc. However, be warned that having db cascades enabled can sometimes throw a wrench in the JPA provider, since it is not expecting the records to be deleted underneath it. For example, it might be caching the deleted objects. You might want to consider:

a) making sure that you end the persistence context immediately after a delete, or ensuring that you do not reference the objects that are being deleted by the db cascades

b) evict the objects, that are delete cascaded in the db, from the second level cache using the JPA 2 Cache API

c) make sure that you are not concurrently doing anything else that might be using the deleted objects


-Mike
Pro JPA 2: Mastering the Java Persistence API
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3 CascadeType annotation. What for it is required exactly.