Announcement Announcement Module
Collapse
No announcement yet.
OpenSessionInViewPattern Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • OpenSessionInViewPattern

    Hello,

    I am trying my hands on "OpenSessionInViewPattern" in Spring & Hibernate but not able to do it successfully. Below the configurations.

    I added following in web.xml
    ---------------------------
    <code>
    <filter>
    <filter-name>sessionFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>sessionFilter</filter-name>
    <url-pattern>*.html</url-pattern>
    </filter-mapping>

    </code>

    Below is the applicationContext.xml
    ----------------------------------
    <code>
    <bean class="org.springframework.beans.factory.annotatio n.RequiredAnnotationBeanPostProcessor"/>
    <!--data source-->
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url"><value>jdbc:mysql://localhost:3306/lumieredb_preview</value></property>
    <property name="username"><value>test</value></property>
    <property name="password"><value>test</value></property>
    <property name="driverClassName"><value>com.mysql.jdbc.jdbc2 .optional.MysqlXADataSource</value></property>
    <property name="defaultReadOnly"><value>false</value></property>
    </bean>

    <!--previewSessionFactory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref local="dataSource"/>
    </property>
    <property name="configLocation">
    <value>WEB-INF/hibernate.cfg.xml</value>
    </property>
    </bean>

    <!--tx manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <!--dao-->
    <bean id="personDao" class="dao.PersonDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--Service-->
    <bean id="personService" class="service.PersonServiceImpl">
    <property name="personDao" ref="personDao"/>
    </bean>

    </beans>
    </code>

    The simple Dao is as follows
    --------------------------
    <code>
    @Transactional
    public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{


    public PersonDaoImpl(){
    }
    public void saveOrUpdatePerson(Person person){
    this.getHibernateTemplate().saveOrUpdate(person);
    }

    public Person getPerson(Integer id) throws Exception{
    return (Person)this.getHibernateTemplate().get(Person.cla ss, id);
    }

    public List<Person> getPersons() throws Exception{
    return (List<Person>)this.getHibernateTemplate().find("fr om Person p");
    }
    }

    </code>

    The application works fine with EAGER loading.
    Any help is appreciated
    Thanks!

  • #2
    OpenSessionInViewPattern

    Well, I could get OpenSessionInViewPattern working. The missing piece was -
    in the WebApplicationContext.xml, in simpleUrlMapping bean, we need to configure interceptor

    <code>

    <bean name="simpleUrlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="urlMap">
    <map>
    <entry key="/dashboard.html" value-ref="dashboardController"/>
    <entry key="/editPerson.html" value-ref="personController"/>
    </map>
    </property>

    <property name="interceptors">
    <list>
    <ref bean="openSessionInViewInterceptor"/>
    </list>
    </property>

    </bean>

    <bean name="openSessionInViewInterceptor"
    class="org.springframework.orm.hibernate3.support. OpenSessionInViewInterceptor">

    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    </code>

    This works fine for reading the data however when I try to save it, I get -

    <error>
    org.springframework.dao.InvalidDataAccessApiUsageE xception: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transactiondefinition.
    </error>


    Again, any help is greatly appreciated!

    Thanks

    Comment


    • #3
      OpenSessionInViewPattern

      So, I could finally resolve the above issue of saving too.
      What was missing is to set singleSession to false while configuring "openSessionInViewInterceptor" bean.
      <property name="singleSession" value="false"/>

      The application works smoothly now!

      Thanks!

      -Ritesh Biltheria

      Comment


      • #4
        OpenSessionInViewPattern

        Here are the files if anyone needs:

        1)web.xml
        --------
        <code>
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
        <web-app version="2.4"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >



        <display-name>Spring MVC - Person</display-name>

        <servlet>
        <servlet-name>person</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
        <servlet-name>person</servlet-name>
        <url-pattern>*.html</url-pattern>
        </servlet-mapping>

        <listener>
        <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
        </listener>

        <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/person-servlet.xml</param-value>
        </context-param>


        <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
        </filter>

        <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>*.html</url-pattern>
        </filter-mapping>

        </web-app>

        </code>

        2)applicationContext.xml
        -----------------------
        <code>
        <?xml version="1.0" encoding="ISO-8859-1"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schem...-beans-2.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schem...ing-tx-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

        <bean class="org.springframework.beans.factory.annotatio n.RequiredAnnotationBeanPostProcessor"/>
        <!--data source-->
        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url"><value>jdbc:mysql://localhost:3306/lumieredb_preview</value></property>
        <property name="username"><value>test</value></property>
        <property name="password"><value>test</value></property>
        <property name="driverClassName"><value>com.mysql.jdbc.jdbc2 .optional.MysqlXADataSource</value></property>
        <property name="defaultReadOnly"><value>false</value></property>
        </bean>

        <!--previewSessionFactory-->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
        <property name="dataSource">
        <ref local="dataSource"/>
        </property>
        <property name="configLocation">
        <value>WEB-INF/hibernate.cfg.xml</value>
        </property>
        </bean>

        <!--tx manager-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        <!--dao-->
        <bean id="personDao" class="dao.PersonDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--Service-->
        <bean id="personService" class="service.PersonServiceImpl">
        <property name="personDao" ref="personDao"/>
        </bean>


        <!--to leverage Hibernate Lazy Loading Feature-->
        <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support. OpenSessionInViewInterceptor">
        <property name="sessionFactory">
        <ref bean="sessionFactory"/>
        </property>
        <!--This is required else problem while saving a record(FlushMode.NEVER/MANUAL)-->
        <property name="singleSession" value="false"/>
        </bean>

        </beans>
        </code>

        3)WebApplicationContext.xml
        -----------------------------
        <code>
        <?xml version="1.0" encoding="ISO-8859-1"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schem...-beans-2.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schem...ing-tx-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

        <import resource="applicationContext.xml"/>

        <bean name="simpleUrlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
        <property name="urlMap">
        <map>
        <entry key="/dashboard.html" value-ref="dashboardController"/>
        <entry key="/editPerson.html" value-ref="personController"/>
        </map>
        </property>
        <!-- Interceptor required to leaverage Hibernate LAZY loading feature-->
        <property name="interceptors">
        <list>
        <ref bean="openSessionInViewInterceptor"/>
        </list>
        </property>
        </bean>


        <!-- Dashboard controller-->
        <bean name="dashboardController" class="controller.DashboardController">
        <property name="personService" ref="personService"/>
        </bean>

        <bean name="personController" class="controller.PersonController">
        <property name="personService" ref="personService"/>
        <property name="commandName" value="person"/>
        <property name="commandClass" value="model.Person"/>
        <property name="formView" value="person"/>
        <property name="successView" value="dashboard"/>
        <property name="sessionForm" value="true"/>
        </bean>


        <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew"/>
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
        </bean>

        </beans>
        </code>

        4)DaoImpl
        ----------
        <code>
        public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{

        public void saveOrUpdatePerson(Person person){
        this.getHibernateTemplate().saveOrUpdate(person);
        }

        public Person getPerson(Integer id) throws Exception{
        return (Person)this.getHibernateTemplate().get(Person.cla ss, id);
        }

        public List<Person> getPersons() throws Exception{
        return (List<Person>)this.getHibernateTemplate().find("fr om Person p");
        }
        }

        </code>

        5)Service Layer
        ----------------
        <code>
        public class PersonServiceImpl implements PersonService{

        private PersonDao personDao;

        public void setPersonDao(PersonDao personDao){
        this.personDao = personDao;
        }
        public Person getPerson(Integer id) throws Exception{
        return this.personDao.getPerson(id);
        }
        public List<Person> getPersons() throws Exception{
        return this.personDao.getPersons();
        }

        @Transactional(readOnly = false)
        public void saveOrUpdatePerson(Person person) throws Exception{
        this.personDao.saveOrUpdatePerson(person);
        }
        }
        </code>

        Comment

        Working...
        X