• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic