我有一个Java Spring MVC和我的车型之一是java.util.Collections.swap(名单<?>名单,INT I,诠释J)什么都不做
类别@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Category extends AuditModel {
@JsonView(DataTablesOutput.View.class)
String name;
boolean active;
@Column(columnDefinition = "TEXT")
String description;
@Column(length = 70)
String metaTitle;
@Column(length = 160)
String metaDescription;
String friendlyUrl;
@OneToMany
List<Category> secondaryCategories;
String image;
Long position;
}
我尝试交换的两个位置使用java.util.Collections.swap(List list,int i,int j)的secondaryCategories列表中的类别没有任何反应,也没有错误。
@Override
public ResponseEntity moveCategoryUpOrDown(Long id, Long parentId, String direction) {
if (id == parentId) {
return new ResponseEntity<>("Home category cat't be moved",HttpStatus.BAD_REQUEST);
}
Category category = categoryRepository.findOne(id);
Category parent = categoryRepository.findOne(parentId);
List<Category> secondaryCategories = parent.getSecondaryCategories();
if (direction.compareTo("up") == 0) {
if (secondaryCategories.indexOf(category) <= 0)
return new ResponseEntity<>(HttpStatus.OK);
int i = secondaryCategories.indexOf(category);
int j = secondaryCategories.indexOf(category) - 1;
// Category previous = secondaryCategories.get(j);
Collections.swap(secondaryCategories, i, j);
// categoryRepository.saveAndFlush(previous);
// categoryRepository.saveAndFlush(category);
categoryRepository.saveAndFlush(parent);
}
... etc ..
我也试过EntityManager分离对象之前交换没有成功。我做了我自己的交换方法,但我无法交换。
如何在secondaryCategories中交换两个类别?
你没有*与实体*,你保存它们,并期望它们改变...... – meskobalazs
该列表是如何排序的?如果您不放置'@ OrderBy'或'@ OrderColumn',那么顺序就是元素的id。因此,“交换”在数据存储区 –