我有订单POJO和POJO项,这两个实体之间的映射关系如下所示:
@Entity
@Table(name = "ordertable")
public class Order {
@Id @GeneratedValue
@Column(name="orderid", unique = true, nullable = false)
private long id;
@OneToMany(fetch=FetchType.LAZY)
@JoinTable(name = "order_item", joinColumns = @JoinColumn(name = "orderId"), inverseJoinColumns = @JoinColumn(name = "itemId"))
private Set<Item> items = new HashSet<Item>();
}
@Entity
@Table(name="itemtable")
public class Item {
@Id @GeneratedValue
@Column(name="itemid", unique = true, nullable = false)
private long id;
@Column(name="title")
private String title;
}
在我的数据库中有一个名为order_item的表:
我不允许我添加具有相同itemId的其他项目并给我错误,如:
错误:org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 重复条目&#39; 1&#39;关键词&#39; UK_d9fp313gthlw9q6hv0e4aanlm&#39;
错误:org.hibernate.internal.SessionImpl - HHH000346:托管刷新期间出错[无法执行语句]
如何检查同一项目是否可以,因为它们属于不同的订单?