2

I use hibernate 4.2.

It doesn't give me hibernate variant of OneToMany annotation, but only javax.persistence.OneToMany.

So I use it as

public class Parent  {
      ...........
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
        private Set<Child> children = new HashSet<Child>();

When I do update of Parent I expect children collection to be updated by cascade MERGE.

session.update(parent);

But it doesn't update Child entities of children collection. It only sends update statement for Parent entity.

So how can I update on cascade Child entities of children collection?

I cannot use org.hibernate.annotations.CascadeType because it is not supported by javax.persistence.OneToMany.

Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83

2 Answers2

4

If you want to use hibernate's CascadeType, define @Cascade(..) separately on field/method level,

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@Cascade({CascadeType.PERSIST, CascadeType.MERGE, CascadeType.SAVE_UPDATE}) //example
private Set<Child> children = new HashSet<Child>();
Wundwin Born
  • 3,467
  • 19
  • 37
1

Instead of persist or merge did you try with all?.

   @OneToMany(orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)

Try it just in case that you want to remove this child in case that the father would be removed.

In my example works

paul
  • 12,873
  • 23
  • 91
  • 153