I found the answer to the problem. I am sure I am not the first person experiencing this issue, but I didn't find a satisafactory explanation to this issue in any of the forums. So I am going to put as much detail to help whoever encounter the same issue.
Key here is the following properties in the hibernate config file:
Code:
<!--
This need to be mentioned in order to automatically bind the sessionfactory to the JNDI tree. Here in this case the JNDI name would be "oracle_session_factory". If we omit this property hibernate will not bind the sessionfactory to JNDI.
In the hibernate util class first look up sessionfactory using the following code:
SessionFactory factory = new InitialContext().lookup("oracle_session_factory");
If the above statement throws NamingException or return null value, do the following:
Configuration config = new Configuration().configure( "hibernate_config_file");
config.buildSessionFactory();
Please note, we need to call config.buildSessionFactory() to register the factory to the JNDI tree.
-->
<property name="session_factory_name">oracle_session_factory</property>
<--
Mention your datasource's JNDI name here
-->
<property name="connection.datasource">ssm_oracle</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<--
Since we are using CMT make sure the config contains the follwing value:
-->
<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>
<property name="connection.release_mode">auto</property>
<--
The following two entries are essential to do the automatic flush into the database at the end of the transaction. By specifying these, we do not need to explicitly flush and close the session, it will be automatically done at the end of the transaction. I did not specify these earlier, that was the reason the record was not getting inserted into DB.
-->
<property name="transaction.auto_close_session">true</property>
<property name="transaction.flush_before_completion">true</property>
By adding the above changes in the config files, we do not need to put any transaction begin, close, session flush, session close code in our source code. A working sample is given below:
Code:
public void saveEmploy(){
Session session = HibernateUtil.getCurrentSession();
com.ssm.business.Employ emp = new com.ssm.business.Employ();
emp.setName("Employ 46");
emp.setDepartment(10);
session.save(emp);
}
If anything is not clear, let me know, I shall try to put more detail.
Thanks
Shimit