浏览 10538 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-09-14
我这边是这样做的,但不成功,请各位帮忙看一下? 先贴我的applicationContext-hibernate.xml文件 <beans> <!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= --> <!-- setup datasource --> <bean id="sportsDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jdbc/DataSource</value> </property> </bean> <!-- Choose the dialect that matches your "dataSource" definition --> <bean id="sportsSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="sportsDataSource"/> </property> <property name="mappingResources"> <list> <value>com/sports/po/Course.hbm.xml</value> <value>com/sports/po/Dict.hbm.xml</value> <value>com/sports/po/Student.hbm.xml</value> <value>com/sports/po/Teacher.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cglib.use_reflection_optimizer">true</prop> </props> </property> </bean> <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA); --> <bean id="sportsTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sportsSessionFactory"/></property> </bean> <!--<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>--> <!-- ***** COURSE SERVICE *****--> <bean id="courseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="sportsTransactionManager"/></property> <property name="target"><ref local="courseTarget"/></property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- CourseTarget primary business object implementation --> <bean id="courseTarget" class="com.sports.service.impl.CourseServiceImpl"> <property name="courseDAO"><ref local="courseDAO"/></property> <property name="teacherDAO"><ref local="teacherDAO"/></property> </bean> <!-- DAO object: Hibernate implementation --> <bean id="courseDAO" class="com.sports.dao.impl.CourseDaoImpl"> <property name="sessionFactory"><ref local="sportsSessionFactory"/></property> </bean> <bean id="teacherDAO" class="com.sports.dao.impl.TeacherDaoImpl"> <property name="sessionFactory"><ref local="sportsSessionFactory"/></property> </bean> <!-- Add more services here --> </beans> 从以上配置文件上可以看到 courseService.class 是我的service 层主页面,它定义了以 insert开头的方法加入了事务控制。 下面是我 courseService.class文件的一个 insert开头的method代码片段: /** * 设置一门课程,并让现在的所有学生都默认报名 * @throws ApplicationException */ public void insertCourseAndAllStudentJoin(); throws Exception { try { CourseDto courseDto = new CourseDto();; courseDto.setName("Struts + Spring + Hibernate");; courseDto.setWeekType(3);; courseDto.setSectionType(3);; courseDto.setPopulation(300);; courseDto.setGradeType(3);; courseDto.setTeacherId(1);; courseDto.setPlace("还要研究 AOP。");; courseDto.setState(2);; courseDto.setCreateddate(new Timestamp(System.currentTimeMillis();););; courseDAO.insertCourse(courseDto);; courseDAO.insertStudentJoin(new Long(81););; } catch (Exception e); { e.printStackTrace();; throw e; } }说明一下: courseDAO.insertCourse(courseDto);;//会执行成功 courseDAO.insertStudentJoin(new Long(81););;//会抛出异常 但这时候courseDAO.insertCourse(courseDto);没有回滚,依然插入数据到数据库中,为什么在同个事务中,都发生异常了,还不会回滚? 帮忙看看,弄了好久了,不知道是哪里的问题? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-09-14
没有仔细看问题,汗.估计问题处在dao身上
|
|
返回顶楼 | |
发表时间:2004-09-14
youcai 写道 没有仔细看问题,汗.估计问题处在dao身上
能指点一下吗? 其实我的 Dao 类也是很简单代码,如下: package com.sports.dao.impl; import java.util.*; import net.sf.hibernate.*; import org.apache.commons.logging.*; import org.apache.commons.beanutils.BeanUtils; import org.springframework.orm.hibernate.HibernateCallback; import org.springframework.orm.hibernate.support.HibernateDaoSupport; import com.sports.po.Course; import com.sports.po.Student; import com.sports.dto.CourseDto; import com.sports.dao.CourseDao; /** * @author Hu Ronghua * * To change the template for this generated type comment go to * Window&Preferences&Java&Code Generation&Code and Comments */ public class CourseDaoImpl extends HibernateDaoSupport implements CourseDao { private static Log log = LogFactory.getLog(CourseDaoImpl.class);; /** * 增加一门课程 * @param course * @throws ApplicationException */ public void insertCourse(final CourseDto courseDto); throws Exception { Course coursePo = new Course();; BeanUtils.copyProperties(coursePo, courseDto);; getHibernateTemplate();.save(coursePo);; } /** * 设置学生报考的课程 * @param course * @throws ApplicationException */ public void insertStudentJoin(final Long courseId); { Course coursePo = (Course); getHibernateTemplate();.load(Course.class, courseId);; List lstAllStudents = getHibernateTemplate();.find("from com.sports.po.Student");; for (Iterator i = lstAllStudents.iterator();; i.hasNext();;); { Student studentPo = (Student); i.next();; coursePo.getLstStudent();.add(studentPo);; } getHibernateTemplate();.saveOrUpdate(coursePo);; } } |
|
返回顶楼 | |
发表时间:2004-09-14
我也这么用来创建一个对象,没有报错,出来一个奇怪的提示信息:JDBC3.0 Savepoint class is available,能打印新对象的id(hibernate返回的uuid.hex),但是对象没有真正保存到数据库中,正在困惑中。。。。。。(spring1.1+hibernate2.1.6)。
|
|
返回顶楼 | |
发表时间:2004-09-14
问题已解决。
把 <bean id="courseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="sportsTransactionManager"/></property> <property name="target"><ref local="courseTarget"/></property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> 改为<bean id="courseService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="sportsTransactionManager"/></property> <property name="target"><ref local="courseTarget"/></property> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> |
|
返回顶楼 | |
发表时间:2004-09-14
引用 4 <property name="transactionAttributes"> 5 <props> 6 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 7 <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> 8 <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> 9 <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> 10 </props> 11 </property> 是这样的, transactionAttributes 定义中,默认情况是只有 RuntimeException 才会自动Rollback, 所以你加上 -Exception就正常了, 你看看你的那个Exception, 是什么Exception |
|
返回顶楼 | |
发表时间:2004-09-15
好几天了,没有搞明白,hibernate能返回id,但是数据没有插入到数据库里面去,帮我看看有什么问题,
代码: 引用 public class EmployeeServiceImpl implements EmployeeService { /** * 创建联系信息 * @param o * @return */ public String createContactInformation(String base64) throws Exception { String result = null; try { Object o = Base64.decodeToObject(base64); ContactInformation pojo = (ContactInformation)BeanUtilsFactory.getBeanUtils() .copyProperties(o); result = contactInformationDAO.createContactInformation(pojo); } catch (BeanConvertedException bce) { System.out.println(bce.getLocalizedMessage()); } catch (Exception e) { System.out.println("exception:" + e.getLocalizedMessage()); throw e; } return result; } public class ContactInformationDAOHibernateImpl extends HibernateDaoSupport implements ContactInformationDAO { /** * 创建联系信息 * @param o * @return */ public String createContactInformation(ContactInformation o) throws DAOException { String result = null; result = (String)this.getHibernateTemplate().save(o); System.out.println("DAO,result:" + result); return result; } |
|
返回顶楼 | |
发表时间:2004-09-15
配置文件:
引用 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"><value>jdbc/acc4</value></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"> <property name="mappingResources"> <list> <value>com/sfexpress/domain/pojos/base/Currency.hbm.xml</value> <value>com/sfexpress/domain/pojos/base/ExchangeRate.hbm.xml</value> <value>com/sfexpress/domain/pojos/base/ContactInformation.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="dialect.Dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="jdbc.fetch_size">50</prop> <prop key="jdbc.batch_size">25</prop> <prop key="jdbc.use_scrollable_resultset">false</prop> <!-- 解决Hibernate的Encode的问题 --> <prop key="connection.useUnicode">true</prop> <prop key="connection.characterEncoding">UTF-8</prop> </props> </property> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="contactInformationDAO" class="com.sfexpress.server.persistence.hibernate.ContactInformationDAOHibernateImpl"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="transactionAttributes"> <props> <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <bean id="employeeService" parent="baseTxProxy"> <property name="target"> <bean class="com.sfexpress.server.business.facade.impl.EmployeeServiceImpl"> <property name="contactInformationDAO"><ref bean="contactInformationDAO"/></property> </bean> </property> </bean> |
|
返回顶楼 | |