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?
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?
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
).
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:
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 ...)
There's a big difference between the two of them.
You can find more about them in: