I am a newbie to hibernate and I am trying to insert a row. The below is my code along with log. I am not getting any exception, however when verified in the Database, the row itself is not getting inserted.

hibernate-cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
      <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
      <property name="connection.url">jdbc:hsqldb:file:C:\softwares\hsqlDB\hsqldb</property>
      <property name="connection.username">SA</property>
      <property name="connection.password"></property>

      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.OracleDialect</property>

      <property name="connection.autocommit">true</property>

       <!-- mapping resources -->
      <mapping resource="com/uhg/hibernate/Employee.hbm.xml"/>

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

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="com.uhg.hibernate.Employee" table="EMPLOYEE">
      <id name="empId" type="int" column="EMPID">
      </id>
    <property name="empName" column="EMPNAME" type="string" length="20"></property> 
    <property name="empSal" column="EMPSAL" type="integer"></property> 
   </class>
</hibernate-mapping>

Employee.java

package com.uhg.hibernate;

import java.io.Serializable;

public class Employee implements Serializable
{
    private int empId;  
    private String empName;
    private int empSal;

    public Employee()
    {
        super();
    }

    public Employee(int empId,String name,int empSal)
    { 
        super(); 
        this.empName = name; 
        this.empId = empId;
        this.empSal = empSal;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getEmpSal() {
        return empSal;
    }

    public void setEmpSal(int empSal) {
        this.empSal = empSal;
    }
}

My Main class

package com.uhg.hibernate;

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

package com.uhg.hibernate;

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

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

public class HibernateQuery {

    private static SessionFactory factory;

    public static void main(String[] args) {

        try {
            Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

            factory = cfg.buildSessionFactory();

        } catch (Throwable ex) {
            System.out.println(ex);
        }


        insertDatabaseData(factory); 
    }

    private static void insertDatabaseData(SessionFactory factory1) {

        Session session = factory1.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Employee employee = new Employee();
            employee.setEmpName("Vijaya");
            employee.setEmpSal(1234);

            int successfulInd = (Integer) session.save(employee);
            session.flush();
            if (successfulInd > 0) {
                System.out.println("Insertion successful");
            } else {
                System.out.println("Insertion not successful");
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {  
            session.close();
        }
    } 
}

Logs:

Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: hibernate.cfg.xml
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: hibernate.cfg.xml
Nov 29, 2013 10:37:53 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/uhg/hibernate/Employee.hbm.xml
Nov 29, 2013 10:37:54 AM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.uhg.hibernate.Employee -> EMPLOYEE
Nov 29, 2013 10:37:54 AM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Nov 29, 2013 10:37:54 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
Nov 29, 2013 10:37:54 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
Nov 29, 2013 10:37:54 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: true
Nov 29, 2013 10:37:54 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:file:C:\softwares\hsqlDB\hsqldb
Nov 29, 2013 10:37:54 AM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=SA, password=****, autocommit=true}
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: HSQL Database Engine, version: 1.8.0
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: HSQL Database Engine Driver, version: 1.8.0
Nov 29, 2013 10:37:55 AM org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.OracleDialect
Nov 29, 2013 10:37:55 AM org.hibernate.dialect.Oracle9Dialect <init>
WARNING: The Oracle9Dialect dialect has been deprecated; use either Oracle9iDialect or Oracle10gDialect instead
Nov 29, 2013 10:37:55 AM org.hibernate.dialect.OracleDialect <init>
WARNING: The OracleDialect dialect has been deprecated; use Oracle8iDialect instead
Nov 29, 2013 10:37:55 AM org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
Nov 29, 2013 10:37:55 AM org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
Nov 29, 2013 10:37:55 AM org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
Nov 29, 2013 10:37:55 AM org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
Nov 29, 2013 10:37:55 AM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Nov 29, 2013 10:37:55 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Hibernate: select max(EMPID) from EMPLOYEE
Hibernate: insert into EMPLOYEE (EMPNAME, SALARY, EMPID) values (?, ?, ?)
Insertion successful

Any small help is highly appreciable. Thanks in advance.

Recommended Answers

All 2 Replies

could you post the table too?

TABLE EMPLOYEE(
EMPID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,
EMPNAME CHAR(25),
SALARY DECIMAL(10,2) NOT NULL
)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.