Anil
Anil

Reputation: 31

table not updating using hibernate

I'm using hibernate to update a record into table. Program is executing fine, but record is not being updated. can you please suggest what might be the issue.

configuration file

<?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>

table configuration file:

<?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 file for table

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;
    }
}

session factory file:

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

}

Upvotes: 1

Views: 1944

Answers (2)

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

Use Transaction

Transaction transaction = session.beginTransaction();

// your save goes here

transaction.commit();

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

Carry out your update inside a Transaction. Retrieve the transaction from the Session in the beginning and commit it in the end.

Upvotes: 0

Related Questions