2012-05-21 46 views
0

我正在使用hibernate將記錄更新到表中。程序執行正常,但記錄沒有被更新。你能否提出可能的問題。表未使用休眠更新

配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> 
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property> 
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 
    <property name="hibernate.connection.username">scott</property> 
    <property name="hibernate.connection.password">tiger</property> 
    <property name="show_sql">true</property> 
    <mapping resource="com/hibernate/demo/employee.hbm.xml"/> 
    </session-factory> 
</hibernate-configuration> 

表配置文件:爲表

<?xml version="1.0" encoding="UTF-8"?> 
<!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.hibernate.demo.Employee" table="EMP"> 
     <id name="empnum" type="integer" column="EMPNO"> 
    <generator class="assigned"/> 
    </id> 

    <property name="empnum" insert="false" update="false"> 
     <column name="EMPNO"/> 
    </property> 
    <property name="name"> 
     <column name="ENAME"/> 
    </property> 
    <property name="job"> 
     <column name="JOB"/> 
    </property> 

    </class>  
</hibernate-mapping> 

的Java文件

package com.hibernate.demo; 



/** 
* 
* @author Anil Modipalle 
*/ 
public class Employee { 

    private int empnum; 
    private String name; 
    private String job; 

    /** 
    * @return the empnum 
    */ 
    public int getEmpnum() { 
     return empnum; 
    } 

    /** 
    * @param empnum the empnum to set 
    */ 
    public void setEmpnum(int empnum) { 
     this.empnum = empnum; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the job 
    */ 
    public String getJob() { 
     return job; 
    } 

    /** 
    * @param job the job to set 
    */ 
    public void setJob(String job) { 
     this.job = job; 
    } 
} 

會話工廠文件:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package com.hibernate.demo; 

import java.io.File; 
import org.hibernate.*; 
import org.hibernate.cfg.Configuration; 

/** 
* 
* @author Anil Modipalle 
*/ 
public class Update { 

    public static void main(String args[]){ 

     Session ses = null; 

     try{ 

      SessionFactory sf = new Configuration().configure().buildSessionFactory(); 

      ses = sf.openSession(); 

      System.out.println("inserting record"); 

      Employee emp = new Employee(); 

      emp.setEmpnum(1010); 
      emp.setName("Anil"); 
      emp.setJob("Manager"); 

      ses.save(emp);    
     } 
     catch(Exception e){ 
     e.printStackTrace(); 
     } 

     finally{ 

     ses.flush(); 
     ses.close(); 
     } 
    } 

} 

回答

0

使用事務

Transaction transaction = session.beginTransaction(); 

// your save goes here 

transaction.commit(); 
+0

感謝約傑什,它的工作! – Anil

0

在事務中執行更新。從一開始就從Session中檢索事務並最終提交它。

+0

感謝沙善 – Anil