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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.