我有这种一对多的关系。这是我的一张桌子。
private Set<Images> imagesContainer = new HashSet<Images>(0);
@OneToMany(fetch = FetchType.LAZY, mappedBy = "container")
public Set<Images> getImagesContainer() {
return this.imagesContainer;
}
public void setImagesContainer(Set<Images> imagesContainer) {
this.imagesContainer = imagesContainer;
}
这是我的许多表:
private Container container;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_container", nullable = false)
public Container getContainer() {
return this.container;
}
public void setContainer(Container container) {
this.container = container;
}
我如何使用休眠插入一个带有许多容器的新容器。我会试试这个:
...
Set<Images> images=new HashSet<Images>();
images.add(img1);
images.add(img2);
Container c = new Container();
c.setImagesContainer(images);
....
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void save(Container c){
getHibernateTemplate().save(c);
}
不要工作!!!我得到“嵌套异常是 org.hibernate.exception.DataException:无法插入:...”