0

i have a problem that my project is based on spring mvc integrated with hibernate.in this update is not working.. dao

   @Override
        public void updateAuditorium(Auditorium auditorium) {
        openSession().update(auditorium);
    }

service

@Transactional
    public void update(Auditorium auditorium) {
    auditoriumDAO.updateAuditorium(auditorium);

    }

configuration

<beans:bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <beans:property name="url" value="jdbc:mysql://localhost:3306/auditoriumbooking" />
        <beans:property name="username" value="root" />
        <beans:property name="password" value="root" />
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="packagesToScan">
            <beans:list>
                <beans:value>com.company.product.model</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

After function executed noting happend.

4
  • Could you provide us with any console logs? Can't figure out anything with what you have put up here. Sep 20, 2014 at 15:03
  • you mean tomcat log?
    – boycod3
    Sep 20, 2014 at 15:08
  • you need to commit the transaction.
    – ares
    Sep 20, 2014 at 15:09
  • im using sessionFactory.how can i commit transation.
    – boycod3
    Sep 20, 2014 at 15:12

2 Answers 2

1

All the insert/ update functions must be within a transaction

    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    //update/insert operations here

    session.update(auditorium);

    tx.commit();

    session.close();

If you are handling the session bu yourself (factory.openSession()), then you can perform read only operations without a transaction. However, if you configure session handling in hibernate configuration file, then hibernate will open and close the session for you and a transaction is required even for read only operations. In that case you have to write factory.getCurrentSession(). The transaction is mandatory because hibernate closes the session automatically when it detects end of a transaction.

0

Since you are using sessionFactory, you have to use Transaction for db manipulation. You have to create the session, then begin a transaction in the session and do the db manipulation and then save it to the database through committing the transaction

Transaction transaction = null;
Session session = null;
try {
    //create the session object with openSession() and then begin transaction on session         
    session = sessionFactory().openSession();
    transaction = session.beginTransaction();
    session.update(auditorium);
    transaction.commit();

} catch(HibernateException e) {
    transaction.rollback();
} finally {
    session.close();
}

EDIT: After the question edit:

you are using @Trasactional annotation. so you have to create a HibernateTransactionManager bean(if not created already) and use

<tx:annotation-driven/>

Create a HibernateTransactionManager & define <tx:annotation-driven/> in

application-context.xml

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.