Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

457
Views
save method is not able to save the object using Hibernate + Spring

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.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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>   
about 4 years ago · Santiago Trujillo Report

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

}

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!