尝试使用休眠保存对象集合时遇到一个特殊问题。看来,当我有多个相同类型的对象时,hibernate无法生成标识符,因此我得到了org.hibernate.NonUniqueObjectException
。
示例:
App1->网址
{strApplicationId:1; URLTypeEntity {strCode:1,strDescription:Reply},strURL:www.address1.com},
{strApplicationId:1; URLTypeEntity {strCode:1,strDescription:Reply},strURL:www.address2.com},
{strApplicationId:1; URLTypeEntity {strCode:2,strDescription:Home},strURL:www.address3.com}
如果我在集合中没有两个具有相同URLTypeEntity的URL,则不会触发该错误
@Entity
@Table(name = "tbl_urls")
public class URLEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="intCode")
private Integer intCode;
private String strApplicationID;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "intType", referencedColumnName = "intCode")
private URLTypeEntity objURLType;
private String strURL;
}
@Entity
@Table(name = "tbl_applications")
public class ApplicationEntity
{
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplicationID")
private List<URLEntity> colURLs;
}
答案 0 :(得分:1)
ApplicationEntity
也必须有一个ID。
答案 1 :(得分:0)
解决方案是将CascadeType从ALL更改为Merge
@OneToMany(级联= CascadeType.ALL,mappedBy =“ strApplicationID”) 更改为
@OneToMany(级联= CascadeType.MERGE,mappedBy =“ strApplicationID”)