3

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?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
jarosik
  • 4,136
  • 10
  • 36
  • 53
  • 1
    does my answer help you? if so please accept/vote it. – cнŝdk Jul 16 '15 at 08:47
  • @jarosik if so please accept/vote it. – ooozguuur Dec 16 '15 at 07:41
  • The two are very different. Refer to page 89 for FetchType. For Cascading refer to section 3.2 (page 80) http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam=1508489100_12b9c9f1827b7c7734914284a141af08 – Witold Kaczurba Oct 20 '17 at 08:51

4 Answers4

11

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).

davidluckystar
  • 928
  • 5
  • 15
  • Specifically this applies not only to Hibernate but JPA in general (As per JSR338). Doc: http://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf?AuthParam=1508489100_12b9c9f1827b7c7734914284a141af08 – Witold Kaczurba Oct 20 '17 at 08:46
3

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)
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Hi @Vlad. What happens if i set `CascadeType` to `MERGE` and `REFRESH` and `FetchType` to `LAZY`? I tested it and i see that the hibernate ignores the `FetchType.LAZY' and fetches all children; is it correct? – Arash Jul 31 '22 at 09:41
  • 1
    @ArashNo, it's not correct. Fetching and cascading have nothing in common. – Vlad Mihalcea Jul 31 '22 at 09:53
1

Both are different configurations, you can relate it with simple SQL.

Cascade tells you what happens when one entity gets updated ( on delete cascade in sql)

Fetch tells how the query is going to be executed ( join, lazy ...)

1

There's a big difference between the two of them.

  • CascadeType is a property used to define cascading in arelationship between a parent and a child.
  • FetchType is a property used to define fetching strategies which are used to optimize the Hibernate generated select statement, so that it can be as efficient as possible.

You can find more about them in:

  1. Hibernate – fetching strategies examples
  2. Hibernate JPA Cascade Types
cнŝdk
  • 31,391
  • 7
  • 56
  • 78