无法使用hibernate和spring

时间:2017-04-07 19:25:01

标签: java spring hibernate spring-mvc orm

使用hibernate将新记录插入到数据库中它给出了如下错误 org.springframework.dao.InvalidDataAccessApiUsageException:在只读模式下不允许写入操作(FlushMode.MANUAL):将会话转换为FlushMode。 COMMIT / AUTO或删除' readOnly'来自交易定义的标记。

bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    id="dataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>
<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    id="sessionFactory">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <value>Student.hbm.xml</value>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>

        </props>
    </property>
</bean>
<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
    id="hibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>

</bean>
<bean class="com.sj.dao.StudentMasterDAOImpl" id="studentMasterDAOImpl">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>

错误消息

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

DAO实施

public class StudentMasterDAOImpl implements StudentMasterDAO {
    public StudentMasterDAOImpl() {
    System.out.println(getClass());
    }
    private HibernateTemplate hibernateTemplate;
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    System.out.println("set hibernate template");   
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    @Transactional(readOnly = false)
    public int createStudent(Student student) {
        System.out.println(student);
    //       hibernateTemplate.getSessionFactory().getCurrentSession().setFlushMode(FlushMo             de.AUTO);
        int id = (int) hibernateTemplate.save(student);
        return id;
    }
    @Override
    public List<Student> getStudent(String name) {
        String hql = "from Student";
        List list = hibernateTemplate.find(hql);
        return (List<Student>) list;
    }


    @Override
    public int deleteStudent(int id) {
        return 0;
    }
    @Override
    public Student getStudent(int id) {
        Student student = null;
        if (id > 0) {
            student = (Student) hibernateTemplate.get(Student.class, id);

        }
        return student;

    }
}

0 个答案:

没有答案