spring配置管理hibernateTemplate然后直接注解使用

balckJack 发布于 2015/05/06 20:07
阅读 1K+
收藏 0

这是spring配置:spring-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- spring的配置 管理各种bean 包括数据库的配置bean -->
<!-- 配置注解扫描包 -->
<context:component-scan base-package="www.ichint.lostPlatform" />


<!-- 加载文件 -->
<context:property-placeholder location="classpath:spring-config/jdbc.properties" />


<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
</bean>


<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 1.指定hibernate的属性配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
<!-- 2.指定hibernate实体类的映射文件 -->
</property>
<property name="configLocations">
<list>
<value>
classpath*:spring-config/hibernate-mapping.xml
</value>
</list>
</property>
</bean>


<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<bean id="transactionBese"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true" abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>


<!-- Hibernate Template Bean配置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

下面是:DbStorageImp(dao层代码)

@Repository
public class DbStorageImpl implements DbStorage{

@Autowired
HibernateTemplate jdbcTemplate;

@Override
public void save(User user) {
    jdbcTemplate.save(user);
}
}


报错:

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.


求指导,小弟不是很懂,网上也找了很多教程还是没解决,
加载中
0
勇敢的蜗牛_Z
勇敢的蜗牛_Z
直接吧hibernateTemplate注入进去直接用不就完事了
0
N
New_World
Write operations are not allowed in read-only mode,我用HibernateTemplate也有同样的问题,估计是tx的问题,求大神解答
返回顶部
顶部