Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

456
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda