在多线程环境中,
这有效
Box box = new Box("B");
Toy t1 = box.addNewToy("t1");
Toy t2 = box.addNewToy("t2");
synchronized (em) {
em.getTransaction().begin();
em.persist(t1);
em.getTransaction().commit();
}
synchronized (em) {
em.getTransaction().begin();
em.persist(t2);
em.getTransaction().commit();
}
但这并不
Box box = new Box("B");
Toy t1 = box.addNewToy("t1");
synchronized (em) {
em.getTransaction().begin();
em.persist(t1);
em.getTransaction().commit();
}
Toy t2 = box.addNewToy("t2");
synchronized (em) {
em.getTransaction().begin();
em.persist(t2);
em.getTransaction().commit();
}
我收到如下错误:“对象引用了未保存的瞬态实例”、“具有相同标识符值的不同对象已与会话相关联”
有任何想法吗?
这是一个重现问题的最小 Maven 项目:http ://www.2shared.com/file/bGLmJ6aO/example.html
细节
java版本“1.7.0_17”,休眠4.2.3.Final,Ubuntu 11.04 natty,SQLite
class Toy {
@ManyToMany(mappedBy="toys",fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<Box> getBoxes() { return boxes; }
public void setBoxes(List<Box> boxes) { this.boxes = boxes; }
}
class Box {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<Toy> getToys() { return toys; }
public void setToys(List<Toy> toys) { this.toys = toys; }
public Toy addNewToy(String name) {
Toy toy = new Toy();
toy.setName(name);
toy.boxes.add(this);
toys.add(toy);
return toy;
}
}