delucaezequiel
delucaezequiel

Reputation: 639

Hibernate identity generator issue

I am having a particular issue when trying to save a collection of objects with hibernate. It seems that when I have more than one object of the same type, hibernate fails to generate the identifier, so I get a org.hibernate.NonUniqueObjectException .

Example:

App1 --> urls
{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}

If I do not have two URLs with the same URLTypeEntity on the collection, the error is not triggered

@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;
}

Upvotes: 0

Views: 224

Answers (2)

delucaezequiel
delucaezequiel

Reputation: 639

The solution was changing the CascadeType from ALL To Merge

@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplicationID") Changed To

@OneToMany(cascade = CascadeType.MERGE, mappedBy = "strApplicationID")

Upvotes: 0

David Frank
David Frank

Reputation: 6092

ApplicationEntity must also have an id.

Upvotes: 1

Related Questions