代码之家  ›  专栏  ›  技术社区  ›  delucaezequiel

Hibernate身份生成器问题

  •  0
  • delucaezequiel  · 技术社区  · 7 年前

    org.hibernate.NonUniqueObjectException .

    附件1-->网址
    {STRAPApplicationID:1;URLTypeEntity{strCode:1,strDescription:Reply},strURL:www.address1.com},
    {STRAPApplicationID:1;URLTypeEntity{strCode:1,strDescription:Reply},strURL:www.address2.com},
    {STRAPApplicationID:1;URLTypeEntity{strCode:2,strDescription:Home},strURL:www.address3.com}

    如果集合中没有两个URL具有相同的URLTypeEntity,则不会触发错误

    @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;
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   David Frank    7 年前

    ApplicationEntity 还必须有一个id。

        2
  •  0
  •   delucaezequiel    7 年前

    解决方案是将级联类型从ALL更改为Merge

    改为

    @OneToMany(cascade=CascadeType.MERGE,mappedBy=“StrapApplicationId”)