Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- public class Parent {
- // ...
- @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
- private Set<Child> children = new HashSet<Child>();
- // ...
- }
- @Entity
- public class Child {
- // ...
- @ManyToOne(fetch = FetchType.LAZY)
- private Parent parent;
- // ...
- }
- Parent parent = new Parent();
- em.persist(parent);
- // ...
- Child child = new Child();
- child.setParent(parent);
- em.persist(child);
- parent.getChildren().size(); // returns 0
- public void addChild(Child child) {
- child.setParent0(this);
- getChildren().add(individualNeed);
- }
- public void setParent(Parent parent) {
- parent.addChild(child);
- }
- public void setParent0(Parent parent) {
- this.parent = parent;
- }
- @Entity
- public class Parent {
- // ...
- @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY,
- cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
- @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
- private Set<Child> children = new HashSet<Child>();
- // ...
- }
- @Entity
- public class Child {
- // ...
- @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
- @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
- private Parent parent;
- // ...
- }
- Parent parent = new Parent();
- em.persist(parent);
- // ...
- Child child = new Child();
- child.setParent(parent);
- em.persist(child); //will cascade update to parent
- parent.getChildren().size(); // returns 1
- Parent parent = new Parent();
- Child child = new Child();
- parent.setChild(parent);
- em.persist(parent); //will cascade update to child
- child.getParent(); // returns the parent
Add Comment
Please, Sign In to add comment