spring整合hibernate无法级联更新

二零一九是猪年 发布于 2016/11/29 13:31
阅读 454
收藏 0

Linux基金会免费官方培训及考试申请即将截止,戳这里申请!>>>

hibernate配置文件如下:

文章


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.itdamo.pojo.Article" table="article" catalog="itdamo_db" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" column="name"/>
        <property name="url" column="url"/>
        <property name="content" column="content"/>
        
        <many-to-one name="articleClass" column="article_class_id" cascade="save-update" class="com.itdamo.pojo.ArticleClass"/>
    </class>
</hibernate-mapping>
文章分类



<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.itdamo.pojo.ArticleClass" table="article_class" catalog="itdamo_db" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" column="name"/>
        <property name="url" column="url"/>
        <property name="count" column="[count]"/>
    </class>
</hibernate-mapping>
在junti单元测试中测试:

但是用spring和hibernate整合后无法执行update级联更新操作,只能插入新的文章,请问是不是aop在spring的事务处理代码中没有配置好?目前找不到原因,数据两张表也有外键关联!

spring配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       ">
	
	<context:property-placeholder location="/WEB-INF/config/itdamo.properties" ignore-resource-not-found="true" ignore-unresolvable="true" />
    
	<context:component-scan base-package="com.itdamo">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
	</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="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
			</props>
		</property>
		<property name="mappingResources">
		     <list>
		           <value>com/itdamo/pojo/User.hbm.xml</value>
		           <value>com/itdamo/pojo/Article.hbm.xml</value>
		           <value>com/itdamo/pojo/ArticleClass.hbm.xml</value>
		     </list>
		</property>
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<aop:config>
		<aop:pointcut id="bussinessService"
			expression="execution(* com.itdamo.service.*.*(..))" />
		<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="false" propagation="NOT_SUPPORTED" />
			<tx:method name="find*" read-only="false" propagation="NOT_SUPPORTED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
		
</beans>






加载中
0
songhuiTan
songhuiTan

,,感觉你测试那个不能算是级联更新,,你试试将ArticleClass这个类删掉(先用ArticleClass获取所有的Article),看看会不会将关联的Article删掉,,,


0
L
LikeWind丶
级联不是这边就是另外一边,两个都试一下不就行了
0
大賢者
大賢者

ssh 对新手来说略复杂,什么包冲突,配置到头大,经历过就好。

无法更新,肯定你的哪里有问题,仔细看书,学。

0
二零一九是猪年
二零一九是猪年
问题解决了,是事务配置失败的情况,在使用springmvc的时候要先注册aop,否则会事务失败!
返回顶部
顶部