我有这样的东西
@Entity
public class A implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
private Long id;
@Basic(optional = false)
private String name;
@ManyToMany
private List<B> bList = new ArrayList<>(0);
public A() {
}
public List<B> getbList() {
return bList;
}
public void setbList(List<B> bList) {
this.bList = bList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
和
@Entity
public class B implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
private Long id;
@Basic(optional = false)
private Integer number;
@ManyToMany
private List<A> aList = new ArrayList<>(0);
public B() {
}
public List<A> getaList() {
return aList;
}
public void setaList(List<A> bList) {
this.aList = bList;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
还
public class C {
public C() {
}
public boolean addAtoB(Session hibernate, A a, B b) throws HibernateException {
a.getbList().add(b);
b.getaList().add(a);
hibernate.saveOrUpdate(a);
hibernate.saveOrUpdate(b);
A aa = (A) hibernate.get(A.class, a.getId());
B bb = (B) hibernate.get(B.class, b.getId());
if (aa.getbList().contains(b) && bb.getaList().contains(b)) {
return true;
}
return false;
}
public boolean removeAfromB(Session hibernate, A a, B b) throws HibernateException {
a.getbList().remove(b);
b.getaList().remove(a);
hibernate.saveOrUpdate(a);
hibernate.saveOrUpdate(b);
A aa = (A) hibernate.get(A.class, a.getId());
B bb = (B) hibernate.get(B.class, b.getId());
if (!(aa.getbList().contains(b) && bb.getaList().contains(b))) {
return true;
}
return false;
}
public boolean addListOfAtoB(Session hibernate, List<A> a, B b) throws HibernateException {
boolean test = false;
for (int i = 0; i < a.size(); i++) {
A aa = a.get(i);
addAtoB(hibernate, aa, b);
}
test = true;
return test;
}
public boolean removeListOfAtoB(Session hibernate, List<A> a, B b) throws HibernateException {
boolean test = false;
for (int i = 0; i < a.size(); i++) {
A aa = a.get(i);
removeAfromB(hibernate, aa, b);
}
test = true;
return test;
}
public Integer halfOfB(Session hibernate, List<B> b) throws Exception {
for (int i = 0; i < b.size(); i++) {
B bb = b.get(i);
List<A> aa = bb.getaList();
if (aa.size() <= 5) {
return aa.size();
} else {
Integer br = (Integer) hibernate.createCriteria(B.class)
.setProjection(Projections.max("number")).uniqueResult() + 1;
B bbb = new B();
bbb.setNumber(br);
List<A> half = aa.subList(aa.size() / 2, aa.size());
//error
removeListOfAtoB(hibernate, half, bb);
addListOfAtoB(hibernate, half, bbb);
halfOfB(hibernate, b);
}
}
throw new Exception("error in halfOfB");
}
}
halfOfB 是一种递归方法,它将名称列表减半到新列表中,直到该列表在多个 B 下至少有 5 个来自 A 的名称。
在有人问为什么不是一对多之前,因为我需要两个实体都有多个应用程序,所以多对多是唯一的选择。
junit 中的所有方法都是绿色的,除了测试 halfOfB 抛出 java.util.ConcurrentModificationException。根据这个,如果我使用 indexed 不应该抛出 ConcurrentModificationException 。另外,如果我颠倒顺序
addListOfAtoB(hibernate, half, bbb);
removeListOfAtoB(hibernate, half, bb);
我仍然得到同样的错误任何想法如何使这项工作?