表没有使用hibernate更新

时间:2012-05-21 03:48:24

标签: hibernate

我正在使用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();
        }
    }

}

2 个答案:

答案 0 :(得分:1)

使用交易

Transaction transaction = session.beginTransaction();

// your save goes here

transaction.commit();

答案 1 :(得分:0)

在交易中执行更新。在开始时从Session中检索事务并在最后提交它。