2012-08-17 67 views
6

我所試圖做的是閱讀的代碼的文本文件,並將其插入到表名爲employee1。而我得到這個錯誤:標識從1改爲2

Hibernate: insert into EMPLOYEE1 (NAME, SALARY, MANAGER, ID) values (?, ?, ?, ?) identifier of an instance of com.Employee altered from 1 to 2 Exception in thread "main" org.hibernate.HibernateException: identifier of an instance of com.Employee altered from 1 to 2 at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669) at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:98)

守則Employee.java

public class Employee 
{ 

    private int id; 
    private String name; 
    private double salary; 
    private String manager; 


    public int getId() 
    { 
     return id; 
    } 

    public void setId(int s) 
    { 
     id = s; 
    } 
    //***************************************************// 
    public String getName() 
    { 
     return name; 
    } 

    public void setName(String s) 
    { 
     name = s; 
    } 
    //***************************************************// 
    public double getSalary() 
    { 
     return salary; 
    } 

    public void setSalary(double s) 
    { 
     salary = s; 
    } 
    //***************************************************// 
    public String getManager() 
    { 
     return manager; 
    } 

    public void setManager(String s) 
    { 
     manager = s; 
    } 
} 

的EmployeeEx.java守則(exection)

import java.io.File; 
import java.util.Scanner; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

import com.Employee; 

public class EmployeeEx 
{ 

public static void main(String[] args) 
{ 
    Session session = null; 

    try 
    {  
     Transaction transaction = null; 

     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     session =sessionFactory.openSession(); 

     Employee em = new Employee(); 

     File f=new File("c:/Class/Employee1.txt") ; 
     Scanner scan=new Scanner(f); 
     transaction = session.beginTransaction(); 
     while(scan.hasNext()) 
     {    

      String line=scan.nextLine();    
      String empArray[]=line.split(" "); 


      em.setId(Integer.parseInt(empArray[0])); 
      em.setName(empArray[1]); 
      em.setSalary(Double.parseDouble(empArray[2])); 
      em.setManager(empArray[3]); 

      session.save(em); 
      transaction.commit(); 
     } 


} 
catch(Exception e) 
{ 
    System.out.println(e.getMessage()); 
} 
finally 
{ 
    session.flush(); 
    session.close(); 

} 


} 

} 

配置文件:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> 
    <property name="hibernate.connection.username">system</property> 
    <property name="hibernate.connection.password">system</property> 
    <property name="hibernate.connection.pool_size">10</property> 
    <property name="show_sql">true</property> 
    <property name="dialect">org.hibernate.dialect.OracleDialect</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <!-- Mapping files --> 
    <mapping resource="contact.hbm.xml"/> 
    <mapping resource="com.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

映射文件:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
<class name="com.Employee" table="EMPLOYEE1"> 
    <id name="id" type="int" column="ID" > 
    <generator class="assigned"/> 
    </id> 

    <property name="name"> 
    <column name="NAME" /> 
    </property> 
    <property name="salary"> 
    <column name="SALARY"/> 
    </property> 
    <property name="manager"> 
    <column name="MANAGER"/> 
    </property> 
</class> 

</hibernate-mapping> 

回答

9

切實您有隻有一個Employee實例,你一再堅持同一個...

Employee em = new Employee(); // Not a right place.. 

while(scan.hasNext()) 
{    
    // Employee em = new Employee(); // Should be here... 
    em.setId(Integer.parseInt(empArray[0])); 
    em.setName(empArray[1]); 
    em.setSalary(Double.parseDouble(empArray[2])); 
    em.setManager(empArray[3]); 
    .... 
} 
1

變化從POJO和映射文件INT id字段長

0

使用Session.merge(Object)代替Session.save(Object)

相關問題