在hibernate中持久收集时,为'PRIMARY'键重复输入'1-0'

时间:2015-06-15 17:29:28

标签: java hibernate

  userDetail.java
    @Entity
    @Table(name = "userdetails")
    public class UserDetails {
        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "id")
        private int userId;
        @Column(name = "username")
        private String userName;
        @CollectionOfElements
        private Set<Address> listOfAddress = new HashSet<Address>();
    //get and set


address.java

@Embeddable //use embeded in other class is optional
public class Address {

    private String street;
    private String city;
    private String state;
    private int pincode;
//get and set
test.java
  UserDetails user1 = new UserDetails();
        user1.setUserName("saurabh");

        Address hAddress = new Address();
        hAddress.setCity("bengaluru");
        hAddress.setStreet("baglur");

        Address oAddress = new Address();
        oAddress.setCity("new york");
        oAddress.setStreet("street 4th");


       user1.getListOfAddress().add(oAddress);
       user1.getListOfAddress().add(hAddress);

        session.save(user1);

while running test.java i am getting this exception.
Hibernate: insert into userdetails (username) values (?)
Hibernate: insert into userdetails_listOfAddress (userdetails_id, street, city, state, pincode) values (?, ?, ?, ?, ?)
Hibernate: insert into userdetails_listOfAddress (userdetails_id, street, city, state, pincode) values (?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

Caused by: java.sql.BatchUpdateException: Duplicate entry '1-0' for key 'PRIMARY'
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)

我有一个userdetail类,其中我将地址保存为集合,同时尝试运行我得到上述异常,我的动机是坚持地址的hashset或arraylist。提前谢谢

0 个答案:

没有答案