hibernate无法保存数据

时间:2016-03-16 08:39:40

标签: java spring hibernate

当我使用hibernate保存实体时,发现会话无法将其保存在Transaction中。我必须刷新并清除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";
    }
}

2 个答案:

答案 0 :(得分: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类(接口,自动装配接口而不是类等)做同样的事情。

答案 1 :(得分:0)

你应该自动连接sessionfactory,让spring完全控制你的交易。