我有Netbeans + Hibernate4.3.x(JPA2.1)+ Spring Framework 4.0.1。使用HibernateTemplate写入/更新到MySql数据库时遇到问题:
org.springframework.dao.InvalidDataAccessApiUsageException:
Write operations are not allowed in read-only mode (FlushMode.MANUAL):
Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
如下所述: solution0, solution1, solution2,我尝试在我的xml配置中配置TransactionManager(并在我的dao类中添加@Transactional)。 但是错误仍然存在。
设置
hibernateTemplate.setCheckWriteOperations(false)
(在solution3中建议)没有帮助,在这种情况下,hibernate不会写入db。 请帮助一个贫穷的学生解决它。
的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ADMISSION"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>beans/Course.hbm.xml</value>
<value>beans/Student.hbm.xml</value>
<value>beans/Result.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
</bean>
<bean id="dao" class="dao.HibernateTemplateDao">
<property name="template" ref="template"></property>
</bean>
DAO:
package dao;
import beans.Course;
import java.util.*;
import org.springframework.orm.hibernate4.HibernateTemplate;
import beans.Item;
import beans.Result;
import beans.Student;
import org.hibernate.FlushMode;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// @Transactional // not working
// @Transactional(readOnly = false) //not working either
@Transactional(propagation = Propagation.REQUIRED, readOnly = false) //same error
public class HibernateTemplateDao implements Dao{
private HibernateTemplate template;
public void setTemplate(HibernateTemplate template){
this.template=template;
//template.setCheckWriteOperations(false); // write is not working
//template.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); EXC no session in current thread
}
public HibernateTemplate getTemplate(){
return template;
}
//CRUD operations
public List getAll (String tabName){
if(tabName.equalsIgnoreCase("Student"))
return template.loadAll(Student.class);
if(tabName.equalsIgnoreCase("Course"))
return template.loadAll(Course.class);
if(tabName.equalsIgnoreCase("Result"))
return template.loadAll(Result.class);
return null;
}
public List getId (Item item){
List l = new ArrayList();
Item i = null;
if(item.getClass().getSimpleName().equalsIgnoreCase("Student"))
i = template.get(Student.class, item.getId());
if(item.getClass().getSimpleName().equalsIgnoreCase("Course"))
i = template.get(Course.class, item.getId());
if(item.getClass().getSimpleName().equalsIgnoreCase("Result"))
i = template.get(Result.class, item.getId());
l.add(i);
return l;
}
public int save (Item item){
template.save(item);
return 1;
}
public int update (Item item){
template.persist(item);
return 1;
}
public int delete (Item item){
template.delete(item);
return 1;
}
}