Skip to content

Instantly share code, notes, and snippets.

@chongma
Created June 5, 2022 07:45
Show Gist options
  • Select an option

  • Save chongma/ac7a2adec120bee78e597a09f1e4b6a8 to your computer and use it in GitHub Desktop.

Select an option

Save chongma/ac7a2adec120bee78e597a09f1e4b6a8 to your computer and use it in GitHub Desktop.
@Entity
public class ChildNotWorks {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private int order;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
public ChildNotWorks() {
super();
}
public ChildNotWorks(Parent parent, int order) {
super();
this.parent = parent;
this.order = order;
}
}
@Entity
public class ChildWorks {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private int order;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
public ChildWorks() {
super();
}
public ChildWorks(Parent parent, int order) {
super();
this.parent = parent;
this.order = order;
}
}
public void doTheThing() {
Parent parent = new Parent();
// These seem to have sequential id numbers
List<ChildWorks> childsWorks = new ArrayList<>();
childsWorks.add(new ChildWorks(parent, 1));
childsWorks.add(new ChildWorks(parent, 2));
childsWorks.add(new ChildWorks(parent, 3));
parent.setChildsWorks(childsWorks);
// These id numbers are out of order
List<ChildNotWorks> childsNotWorks = new ArrayList<>();
childsNotWorks.add(new ChildNotWorks(parent, 1));
childsNotWorks.add(new ChildNotWorks(parent, 2));
childsNotWorks.add(new ChildNotWorks(parent, 3));
parent.setChildsNotWorks(childsNotWorks);
}
@Entity
public class Parent {
@Id
private int id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChildWorks> childsWorks;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChildNotWorks> childsNotWorks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment