2014-11-24 33 views
0

這是應用程序休眠的一個主要示例,我有這個錯誤我試圖通過在表上添加自動增量來解決問題MySQL數據庫但不改變錯誤org.hibernate.HibernateException:數據庫返回的沒有本地生成的標識值使用db mysql

 <hibernate-mapping> 
      <class name="Employee" table="EMPLOYEE"> 
       <meta attribute="class-description"> 
       This class contains the employee detail. 
       </meta> 
       <id name="id" type="int" column="id"> 
       <generator class="native"/> 
       </id> 
       <property name="firstName" column="first_name" type="string"/> 
       <property name="lastName" column="last_name" type="string"/> 
       <property name="salary" column="salary" type="int"/> 
      </class> 
     </hibernate-mapping> 

這是應用程序休眠的一個最好的例子,我有這樣的錯誤我試圖通過增加自動遞增表上解決問題MySQL數據庫但不會更改錯誤

<?xml version="1.0" encoding="utf-8"?> 
    <!DOCTYPE hibernate-configuration SYSTEM 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 
     <session-factory> 
     <property name="hibernate.dialect"> 
      org.hibernate.dialect.MySQLDialect 
     </property> 
     <property name="hibernate.connection.driver_class"> 
      com.mysql.jdbc.Driver 
     </property> 

     <!-- Assume test is the database name --> 
     <property name="hibernate.connection.url"> 
      jdbc:mysql://localhost:3306/anagrafica 
     </property> 
     <property name="hibernate.connection.username"> 
      root 
     </property> 
     <property name="hibernate.connection.password"> 
      root 
     </property> 

     <!-- List of XML mapping files --> 
     <mapping resource="Employee.hbm.xml"/> 

    </session-factory> 
    </hibernate-configuration> 



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

    public Employee() {} 
    public Employee(String fname, String lname, int salary) { 
     this.firstName = fname; 
     this.lastName = lname; 
     this.salary = salary; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String first_name) { 
     this.firstName = first_name; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String last_name) { 
     this.lastName = last_name; 
    } 
    public int getSalary() { 
     return salary; 
    } 
    public void setSalary(int salary) { 
     this.salary = salary; 
    } 
} 



import java.util.List; 
import java.util.Date; 
import java.util.Iterator; 

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

public class ManageEmployee { 
    private static SessionFactory factory; 
    public static void main(String[] args) { 
     try{ 
     factory = new Configuration().configure().buildSessionFactory(); 
     }catch (Throwable ex) { 
     System.err.println("Failed to create sessionFactory object." + ex); 
     throw new ExceptionInInitializerError(ex); 
     } 
     ManageEmployee ME = new ManageEmployee(); 

     /* Add few employee records in database */ 
     Integer empID1 = ME.addEmployee("Zara", "Ali", 1000); 
     Integer empID2 = ME.addEmployee("Daisy", "Das", 5000); 
     Integer empID3 = ME.addEmployee("John", "Paul", 10000); 

     /* List down all the employees */ 
     ME.listEmployees(); 

     /* Update employee's records */ 
     ME.updateEmployee(empID1, 5000); 

     /* Delete an employee from the database */ 
     ME.deleteEmployee(empID2); 

     /* List down new list of the employees */ 
     ME.listEmployees(); 
    } 
    /* Method to CREATE an employee in the database */ 
    public Integer addEmployee(String fname, String lname, int salary){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     Integer employeeID = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = new Employee(fname, lname, salary); 
     employeeID = (Integer) session.save(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
     return employeeID; 
    } 
    /* Method to READ all the employees */ 
    public void listEmployees(){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     List employees = session.createQuery("FROM Employee").list(); 
     for (Iterator iterator = 
          employees.iterator(); iterator.hasNext();){ 
      Employee employee = (Employee) iterator.next(); 
      System.out.print("First Name: " + employee.getFirstName()); 
      System.out.print(" Last Name: " + employee.getLastName()); 
      System.out.println(" Salary: " + employee.getSalary()); 
     } 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
    /* Method to UPDATE salary for an employee */ 
    public void updateEmployee(Integer EmployeeID, int salary){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = 
        (Employee)session.get(Employee.class, EmployeeID); 
     employee.setSalary(salary); 
     session.update(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
    /* Method to DELETE an employee from the records */ 
    public void deleteEmployee(Integer EmployeeID){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     try{ 
     tx = session.beginTransaction(); 
     Employee employee = 
        (Employee)session.get(Employee.class, EmployeeID); 
     session.delete(employee); 
     tx.commit(); 
     }catch (HibernateException e) { 
     if (tx!=null) tx.rollback(); 
     e.printStackTrace(); 
     }finally { 
     session.close(); 
     } 
    } 
} 

回答

5

忘記在數據庫表中使用auto_increment。該表的表定義一些東西,如下面的格式,

CREATE TABLE EMPLOYEE (
ID INT AUTO_INCREMENT, 
FIRST_NAME VARCHAR(20), 
LAST_NAME VARCHAR(20), 
SALARY INT, 
PRIMARY KEY (ID) 
) ENGINE=InnoDB; 
+0

我已經添加了自動增量,但不在數據庫上,因爲imostato設置字段自動增量,這也必須是主鍵字段,而且我沒有聲明爲主鍵。 現在,它的工作。謝謝 – user3494255 2014-11-24 12:20:30

+0

無需在Stackoverflow中表示感謝。我們使用upvote/downvote來欣賞Stackoverflow的答案。 – 2014-11-24 12:22:21

+0

好吧,我想象它是一種習慣,但是目前我沒有足夠的投票聲望。 – user3494255 2014-11-25 15:48:46

0

沒有其他解決方案可以更好地工作,除非您使用以下注釋

請使用

@GenericGenerator(name="gen",strategy="increment") 
    @GeneratedValue(generator="gen") 

答覆我。如果它不起作用

相關問題