1

While Executing the program, I'm getting the particular error

**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.** 

every time. please someone help me. Here I am giving my codes which contains some error. In the Below code I have taken one employee data which will be stored in the employee table of MSSQL database. While working with only hibernate at that time i can able to persist my data. But, her i am unable to persist my data.

EmployeeHt.java

This is the entity class which is mapped to the employee table of MSSQL database

 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

This is the class which contains hibernate template

    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>
lenach87
  • 949
  • 3
  • 11
  • 18
Ananta Chandra Das
  • 1,865
  • 2
  • 14
  • 18
  • Check the answers to this question - it might help http://stackoverflow.com/questions/6810158/java-hibernate-write-operations-are-not-allowed-in-read-only-mode – lenach87 Aug 06 '16 at 20:14

1 Answers1

1

Check for @Transactional annotation in you DAO. It should be

@Transactional(readOnly = false)