My Dao layer has a save method as:
public void savePerson(PersonBean personBean) {
Session currentSession;
try {
currentSession = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
currentSession = sessionFactory.openSession();
System.out.println("Opened Session");
}
currentSession.merge(personBean);
System.out.println("Data Saved");
}
And the applicationContext.xml is defined as :
<bean id="oracleDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:{mylocalInstance}" />
<property name="username">
<value>PersonDataBase</value>
</property>
<property name="password">
<value>person</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="oracleDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations" value="PersonBean.hbm.xml" />
</bean>
<bean id="testTransactional" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--<tx:annotation-driven transaction-manager="testTransactional"/>-->
<bean id="personDao" class="com.dao.PersonDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="personService" class="com.service.PersonServiceImpl">
<property name="personDao" ref="personDao"/>
</bean>
It is able to create the tables but the data is not saved, as I have to show the sql, this is the sql generated when trying to save:
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@68be8808] of Hibernate SessionFactory for HibernateTransactionManager
Opened Session
Hibernate:
select
max(PERSON_ID)
from
PERSON_BEAN
Data Saved
Why is select query being generated when I am trying to save it.
You need to commit your transaction as well.
Try this:
public void savePerson(PersonBean personBean) {
Session currentSession;
try {
currentSession = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
currentSession = sessionFactory.openSession();
System.out.println("Opened Session");
}
currentSession.beginTransaction();
currentSession.merge(personBean);
currentSession.getTransaction().commit()
System.out.println("Data Saved");
}
EDIT
You can also set hibernate.connection.autocommit
property to true in Hibernate configuration if you don't want to handle transactions manually.
<property name="hibernate.connection.autocommit">true</property>
Try currentSession.save(personBean) and if you properly configured the Spring then you don't need to
beginTransaction() Spring will handle the transaction .
public void savePerson(PersonBean personBean) { Session currentSession;
try {
currentSession = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
currentSession = sessionFactory.openSession();
System.out.println("Opened Session");
}
currentSession.beginTransaction();
currentSession.save(personBean);
currentSession.getTransaction().commit()
System.out.println("Data Saved");
}