我有一个主要实体:
@Data
@Entity
@Table(name = "MAINS")
public class Main {
...
@OneToMany(mappedBy = "main", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = LAZY)
private List<Chield> children;
我有一个子实体:
@Data
@Entity
@Table(name = "CHILDS")
public class Child {
@Id
@Column(name = "GUID")
private String guid;
@Column(name = "NAME")
private String name;
@Column(name = "Age")
private Integer age;
我尝试将一个Main
与许多Child
一起保存:
List<Child> children = new ArrayList<>();
for (int i = 0, i< 10; i++) {
Child child = new Child();
child.setId(i);
child.setName("Name"+i);
child.setAge(10+i);
children.add(child);
}
main.setChildren(children);
MainRepository.save(main);
但是我需要保存这些订购的孩子。我可以在Child
-order
中添加新列并设置child.setOrder(order++);
,当我选择此列表时,可以按顺序对它进行排序。
但是我可以做不同的事情吗?没有添加新列?
答案 0 :(得分:0)
如果我正确回答了您的问题,那么检查以下选项可能会有所帮助:
@OrderColumn(name = "index_id")
private List<Child> changes = new ArrayList<>();
OR
@OrderBy("id")
private Set<Child>list = new LinkedHashSet<>();
OR
@SortNatural
private SortedSet<Child> children = new TreeSet<Child>();
DOCS:naturalSort
自定义比较器的OR:@SortComparator(SortById.class)
DOCS:customSort
public class SortById implements Comparator<Child> {
Logger log = Logger.getLogger(SortById.class.getSimpleName());
@Override
public int compare(Child o1, Child o2) {
log.info("Child.compare");
return o1.getId().compareTo(o2.getId());
}
}