1

我正在使用休眠将记录更新到表中。程序执行良好,但记录未更新。你能建议可能是什么问题吗?

配置文件

<?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();
        }
    }

}
4

2 回答 2

1

使用事务

Transaction transaction = session.beginTransaction();

// your save goes here

transaction.commit();
于 2012-05-21T04:12:21.803 回答
0

在事务中执行更新。一开始从 Session 中检索事务,最后提交。

于 2012-05-21T04:01:38.483 回答