Hibernate中键'PRIMARY'的重复条目'0'

时间:2014-04-27 00:57:27

标签: java mysql hibernate-mapping

我正在使用mysql使用Hibernate中的列表进行集合映射。 我有一个Employee.java和Certificate.java以及配置文件,如下所示。但是当运行下面的代码时,我得到重复条目'0'用于键'PRIMARY'

public class Certificate{
    private int id;
    private String name; 

public class Employee {
    private int id;
    private String firstName; 
    private String lastName;   
    private int salary;
    private List certificates;

<hibernate-mapping>
  <class name="com.tutorial.hibernate.mappings.collections_mysql_2.list.Employee"    table="EMPLOYEE">
     <id name="id" type="int" column="id">
     <generator class="native"/>
     </id>
     <list name="certificates" cascade="all">
     <key column="employee_id"/>
     <list-index column="id"/>
     <one-to-many class="com.tutorial.hibernate.mappings.collections_mysql_2.list.Certificate"/>
     </list>
     <property name="firstName" column="first_name" type="string"/>
     <property name="lastName" column="last_name" type="string"/>
     <property name="salary" column="salary" type="int"/>
  </class>

  <class name="com.tutorial.hibernate.mappings.collections_mysql_2.list.Certificate" table="CERTIFICATE">
     <id name="id" type="int" column="id">
     <generator class="native"/>
     </id>
     <property name="name" column="certificate_name" type="string"/>
  </class>
</hibernate-mapping>


public static Integer addEmployee(String fname, String lname,  int salary, List cert){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         Employee employee = new Employee(fname, lname, salary);
         employee.setCertificates(cert);
         session.saveOrUpdate(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return employeeID;
   }

表格结构

CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`salary` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

create table `CERTIFICATE` ( id INT NOT NULL auto_increment, 
`certificate_name` VARCHAR(30) default NULL, 
`employee_id` INT default NULL, 
PRIMARY KEY (`id`) )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

当多次调用以下代码以插入带有证书的员工时,我得到重复条目'0'用于键'PRIMARY'

ArrayList set2 = new ArrayList();
set2.add(new Certificate("BCA"));
set2.add(new Certificate("BA"));
Integer empID2 = ecc.addEmployee("Dilip", "Kumar", 3000, set2);

例外:

org.hibernate.exception.ConstraintViolationException: **Duplicate entry '0' for key 'PRIMARY'**
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy5.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1260)
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:279)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)

1 个答案:

答案 0 :(得分:1)

将id的生成器类从native更改为increment。

<id name="id" type="int" column="id">  
     <generator class="increment"></generator>  
</id>