I have two tables say parent and child. Requirement is whenever we add entry to parent, insert should also happen in child. But when we update parent child should not get updated. Which hibernate annotation (CascadeType) should I use?
Asked
Active
Viewed 23 times
1 Answers
0
Considering you have List<Child>
in Parent
class.
So annotation will be like
@OneToMany(mappedBy="parent", cascade = CascadeType.PERSIST)
private List<Child> children = new ArrayList<Child>();
According to this:
CascadeType.PERSIST : means that save() or persist() operations cascade to related entities.

Amogh
- 4,453
- 11
- 45
- 106
-
Thanks for replying. CascadeType.PERSIST is not saving the child record. I am doing session.save(parent) – user3681970 Dec 11 '16 at 11:16
-
The complete details can be found in a separate stackoverflow question which I posted sometime back here http://stackoverflow.com/questions/41084320/confusion-in-giving-hibernate-association-annotation. Please help i am working on weekend breaking my head on this from past several hours – user3681970 Dec 11 '16 at 11:17
-
@user3681970 after `session.save(parent)` can you see records in parent table? – Amogh Dec 11 '16 at 11:22
-
Yes. I can see in parent table but not in child table. This might be because of https://www.mkyong.com/hibernate/cascade-jpa-hibernate-annotation-common-mistake/ – user3681970 Dec 11 '16 at 11:25