肥肥1115 2016-03-04 12:49 采纳率: 0%
浏览 2141

大牛帮帮忙!JPA-EntityManager只能查询,不能增删,估计是事务的问题,但不知道怎么改了

ApplicationContext.xml

ignore-resource-not-found="true" ignore-unresolvable="true" />


expression="org.springframework.stereotype.Controller" />
/context:component-scan

<!--配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="DriverClass" value="${jdbc.driver}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="initialPoolSize" value="${connection_pools.initial_pool_size}" />
    <property name="minPoolSize" value="${connection_pools.min_pool_size}" />
    <property name="maxPoolSize" value="${connection_pools.max_pool_size}" />
    <property name="maxIdleTime" value="${connection_pools.max_idle_time}" />
    <property name="acquireIncrement" value="${connection_pools.acquire_increment}" />
    <property name="checkoutTimeout" value="${connection_pools.checkout_timeout}" />
</bean>
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="classpath:config/jpa/persistence.xml" />
    <!--加载模型对象实例 -->
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.connection.isolation">2</prop>
            <prop key="javax.persistence.validation.mode">none</prop>
            <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider
            </prop>
        </props>
    </property>
</bean>
<!--添加配置文件 -->
<bean id="config" class="hxhz.util.common.CustomizedPropertyConfig">
    <property name="location" value="classpath:sys.properties" />
</bean>
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>
<!--事务 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
</bean>
<aop:config>
    <aop:pointcut id="businessService" expression="execution(* hxhz.*.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="create*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

persostence.xml

transaction-type="RESOURCE_LOCAL">
hxhz.model.entry.Address
hxhz.model.entry.Developlog

DAO

@Repository("testbankDaoImpl")
public class TestbankDaoImpl extends BaseDaoImpl implements TestbankDao {

public boolean addTestbank(TestBank testBank) {
    try {
        entityManager.persist(testBank);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

BaseDao

@Repository("baseDaoImpl")
public class BaseDaoImpl implements BaseDao {

@PersistenceContext(unitName="persistenceUnit")
protected EntityManager entityManager;

public void setEntityManagerFactory(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Autowired
private JpaTemplate jpaTemplate;
@Autowired
private JdbcTemplate jdbcTemplate;
private Class<T> entityClass;
  • 写回答

1条回答

  • devmiao 2016-03-04 21:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组