Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CascadeType vs FetchType

I would like to now what is the difference between CascadeType and FetchType in Hibernate?

They seem very similar but I guess they are not interchangeable, right? When to use them? Can they be used both at the same time?

like image 388
jarosik Avatar asked Mar 16 '26 08:03

jarosik


2 Answers

These are 2 different things:

The CascadeType in Hib. could be REFRESH, MERGE, ..., ALL you put it under the related entity and it determines the behavior of the related entity if the current entity is: refreshed, updated, deleted, e.t.c.. So whenever you entity is affected the CascadeType tells if the related entity should be affected as well.

The FetchType could be only of 2: EAGER and LAZY. This one you as well put under the related entity and it determines whether the related entity should be initialized right away when the current entity is initialized (EAGER) or only on demand (LAZY).

like image 188
davidluckystar Avatar answered Mar 17 '26 22:03

davidluckystar


Cascading is used for propagating entity state transitions from a Parent entity to a Child.

Fetching is used for loading associated entities and you can have:

  • global fetch policies (defined through entity mapping)
  • query-time fetch policy (using the HQL/JPQL FETCH directive)
like image 27
Vlad Mihalcea Avatar answered Mar 17 '26 20:03

Vlad Mihalcea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!