2

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.

2 Answers 2

2

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>   
Sign up to request clarification or add additional context in comments.

4 Comments

Yes it worked, but doesn't hibernate handles that internally, or do we need to add @Transactional to do it.
Automatic transaction management is not enabled by default. But you can enable it. See [this] answer for more details.
Can you please provide the link for [this] word, I think you missed it.
@NagendraSingh Oh! my bad. I've updated that in my answer only. Just change hibernate.connection.autocommit property to true and hibernate configuration and you won't have to handle transactions manually then.
0

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");

}

2 Comments

I tried save, saveandupdate, persist, nothing works until I begin and commit the transaction.
Yes you need to commit the transaction to persist data to data base. But if you able to configure hibernate with spring then string will begin , commit your transaction. You just need to put @ Transactional on the method. github.com/keaz/mysite/blob/feature/website/src/main/java/com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.