2016-03-16 64 views
0

当我使用休眠来保存实体时,我发现会话无法将其保存在事务中。我必须刷新和清除session.but但我没有这样做之前在其他项目中,是否有任何错误的交易配置在我的XML文件?任何答案赞赏︰)休眠不能保存数据

hibernate.cfg.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <context:property-placeholder location="classpath:application.properties"/> 


    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${driverClassName}"></property> 
     <property name="url" value="${url}"></property> 
     <property name="username" value="${db.userName}"></property> 
     <property name="password" value="${db.password}"></property> 
    </bean> 


    <bean id="sessionFactory" name="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <!--<prop key="current_session_context_class">thread</prop>--> 
      </props> 
     </property> 

     <property name="packagesToScan" value="com.gtis"/> 
    </bean> 


    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 


    <!--<bean id="transactionProxy" 
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
      abstract="true"> 
     <property name="transactionManager" ref="transactionManager"></property> 
     <property name="transactionAttributes"> 
      <props> 
       <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> 
       <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop> 
       <prop key="del*">PROPAGATION_REQUIRED</prop> 
       <prop key="*">PROPAGATION_REQUIRED</prop> 
      </props> 
     </property> 
    </bean>--> 
    <!--<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
     <tx:attributes> 
      <tx:method name="create*" propagation="REQUIRED" /> 
      <tx:method name="save*" propagation="REQUIRED" /> 
      <tx:method name="add*" propagation="REQUIRED" /> 
      <tx:method name="update*" propagation="REQUIRED" /> 
      <tx:method name="remove*" propagation="REQUIRED" /> 
      <tx:method name="del*" propagation="REQUIRED" /> 
      <tx:method name="import*" propagation="REQUIRED" /> 
      <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" /> 
     </tx:attributes> 
    </tx:advice> 

    <aop:config> 
     <aop:pointcut id="serviceOperation" expression="execution(* com.gtis.service.*Service.*(..))" /> 
     <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> 
    </aop:config>--> 

</beans> 

package com.gtis.dao; 

import com.gtis.model.Student; 
import org.apache.commons.lang3.StringUtils; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.stereotype.Repository; 

import javax.annotation.Resource; 
import java.util.List; 

/** 
* Created by u on 2016/3/15. 
*/ 
@Repository 
public class StudentDao { 

    @Resource 
    private SessionFactory sessionFactory; 

    private Session getSession(){ 
     return sessionFactory.getCurrentSession(); 
    } 

    public Student query(String id) throws Exception{ 
     if(StringUtils.isBlank(id)){ 
      return (Student)getSession().get(Student.class,id); 
     }else{ 
      throw new Exception("id is required"); 
     } 
    } 

    public List<Student> queryAll(){ 
     String hql = "from com.gtis.model.Student"; 
     Query query = getSession().createQuery(hql); 
     return query.list(); 
    } 

    public void save(Student student)throws Exception{ 
     if(student!=null){ 
      System.out.println("sessionFactory="+sessionFactory); 
      System.out.println("session="+getSession()); 
//   Session session = getSession(); 
      getSession().save(student); 
//   session.flush(); 
//   session.clear(); 
//   session.close(); 
     }else{ 
      throw new Exception("object is required"); 
     } 
    } 

    public void delete(Student student)throws Exception{ 
     if(student!=null){ 
      getSession().delete(student); 
     }else{ 
      throw new Exception("object is required"); 
     } 
    } 
} 

服务

package com.gtis.service; 

    import com.gtis.dao.StudentDao; 
    import com.gtis.model.Student; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Service; 
    import org.springframework.transaction.annotation.Transactional; 

    import java.util.List; 

    /** 
    * Created by u on 2016/3/15. 
    */ 
    @Service 
    public class StudentService { 
     @Autowired 
     StudentDao studentDao; 

     public Student getStudent(String id) throws Exception { 
      return studentDao.query(id); 
     } 

     public List<Student> getAll() throws Exception{ 
      return studentDao.queryAll(); 
     } 

     @Transactional 
     public void save(Student student)throws Exception{ 
      studentDao.save(student); 
     } 

     @Transactional 
     public void delete(Student student) throws Exception{ 
      studentDao.delete(student); 
     } 
    } 


> Controller 

package com.gtis.controller; 

import com.gtis.model.Student; 
import com.gtis.service.StudentService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 

/** 
* Created by u on 2016/3/15. 
*/ 
@Controller 
public class StudentController { 
    @Autowired 
    StudentService studentService; 

    @RequestMapping("get") 
    public String get(){ 
     Student student = new Student(); 
     student.setName("avc"); 
     try { 
      studentService.save(student); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return "somwhere"; 
    } 
} 
+0

您的日志中没有错误? – pleft

+0

没有错误,但控制台没有打印插入sql而没有刷新会话,当我刷新并清除会话时,它确实打印了。 – Aezio

+0

试试这个:@Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW)。你有一个读写事务,所以你应该使readOnly = false。 – Waqar

回答

0

DAO应该实现一个接口,并通过注入该Autowired接口,您Service它使用@Transactional注释才能工作。

所以:

@Repository 
public class StudentDao implements StudentDaoInterface { 
    //your rest code here 
} 

和:

@Service 
public class StudentService { 

    @Autowired 
    StudentDaoInterface studentDao; 

    public Student getStudent(String id) throws Exception { 
     return studentDao.query(id); 
    } 

    public List<Student> getAll() throws Exception{ 
     return studentDao.queryAll(); 
    } 

    @Transactional 
    public void save(Student student)throws Exception{ 
     studentDao.save(student); 
    } 

    @Transactional 
    public void delete(Student student) throws Exception{ 
     studentDao.delete(student); 
    } 
} 

当然不要忘记申报您的DAO到接口的您的公共方法。

哦,并为您的Service类(接口,autowire接口,而不是类等)做同样的事情。

+0

感谢您的答案,我试过了你上面说的方式,但它没有改变。在我做了这些改变之前,我进行了修改,发现我有@Autowired的对象不为空。 – Aezio

0

你应该自动装配sessionfactory,让Spring完全控制你的交易。

+0

谢谢first.i曾经这样做,但它没有工作。所以我试过这种方式... – Aezio