org.springframework.dao.InvalidDataAccessApiUsageException:在只读模式下不允许写操作(FlushMode.MANUAL)

时间:2016-08-06 14:01:18

标签: java spring hibernate spring-data

在执行程序时,我收到了特定错误

**Exception in thread "main" 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.** 

每一次。请有人帮助我。在这里,我给出了包含一些错误的代码。在下面的代码中,我获取了一个员工数据,这些数据将存储在MSSQL数据库的employee表中。当时只使用hibernate,我可以保留我的数据。但是,她我无法坚持我的数据。

EmployeeHt.java

这是映射到MSSQL数据库的employee表的实体类

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Table;

 @Entity
 @Table(name="employee")
 public class EmployeeHt {

    @Id
    @Column(name="id")
    private int id;

    @Column(name="name")
    private String name;

    @Column(name="salary")
    private int salary;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
       public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    @Override
     public String toString() {
            return "EmployeeHt [id=" + id + ", name=" + name + ", salary=" +      salary + "]";
        }
}

EmployeeHtDao.java

这是包含hibernate模板的类

    import org.springframework.orm.hibernate5.HibernateTemplate;

    public class EmployeeHtDao {

        private HibernateTemplate ht;   
        public void setHt(HibernateTemplate ht) {
            this.ht = ht;
     }  
     public void saveEmployee(EmployeeHt e){
        ht.save(e);
     }  
   }

EmployeeHtTest.java

   **This is the main class**

   import org.springframework.context.ApplicationContext;
      import    org.springframework.context.support.ClassPathXmlApplicationContext;

        public class EmployeeHtTest {

        private static ApplicationContext context;
        public static void main(String[] args) {        
        context = new     ClassPathXmlApplicationContext("hibernateTemplate.xml");
         EmployeeHtDao dao=(EmployeeHtDao) context.getBean("edao");     
         EmployeeHt e=new EmployeeHt();
        e.setId(104);
        e.setName("Prangyan");
        e.setSalary(30000);     
        dao.saveEmployee(e);
    }
 }

hibernateTemplate.xml

   **This is the spring-xml file**

 <?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:p="http://www.springframework.org/schema/p"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

        <bean id="connpool"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"     value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="url"     value="jdbc:sqlserver://localhost:1433;databaseName=ananta"/>
            <property name="username" value="sa"/>
            <property name="password" value="pass123"/>
        </bean>

        <bean id="mysessionfactory"   class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="connpool"/>
        </bean>

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

        <bean id="edao" class="com.sdrc.hibernatetemplate.EmployeeHtDao">
            <property name="ht" ref="hibernateTemplate"/>
        </bean>
    </beans>

1 个答案:

答案 0 :(得分:0)

在您的DAO中检查@Transactional批注。 应该是

@Transactional(readOnly = false)