Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC domain objects and OpenSessionInViewInterceptor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC domain objects and OpenSessionInViewInterceptor

    Hello,
    I have been starting to use Spring MVC instead of Struts in my new web apps with OpenSessionInViewInterceptor and noticed something weird. Basically my command class is a hibernate domain object comming from a DAO and service layer respectively. I am manging my transactions at the service layer with a HibernateTransactionManager.

    I have a extended SimpleFormController for a User object which has a drop down of Status objects that I can change on submition. However if I try to change the status of my User on update, I get this error

    org.springframework.orm.hibernate.HibernateSystemE xception: identifier of an instance of com.example.domain.Status altered from -2 to 1; nested exception is net.sf.hibernate.HibernateException: identifier of an instance of com.example.Status altered from -2 to 1

    The status object is mapped to my User class as a many-to-one object. And I dont have this problem if I dont use OpenSessionInViewInterceptor or OpenSessionInViewFilter.

    I guess I dont understand why this issue is happening, the first hibernate session should be closed when I load the page to edit it. And when I hit submit on the form, shouldn't Spring MVC just be populating a new command class with request parameters , which I then send to my service layer to update

    One question I have is what is the lifecycle of a command object that is populated by hibernate when using OpenSessionInView*?

    I know I must be missing something, if anyone has suggestions that would be great.


    Thanks,
    Tyler


    Below is my example mapping. And I am using singleSession=true and my flush mode is auto.


    <?xml version="1.0"?>

    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

    <hibernate-mapping>
    <class
    name="com.example.domain.User"
    table="user"
    proxy="com.example.domain.User"
    dynamic-update="false"
    dynamic-insert="false"
    >

    <id
    name="id"
    column="user_id"
    type="java.lang.Long"
    >
    <generator class="identity">
    </generator>
    </id>
    ............

    <many-to-one
    name="status"
    class="com.example.domain.Status"
    cascade="none"
    outer-join="auto"
    update="true"
    insert="true"
    column="status_id"
    />

    ...................

    </class>

    the Select box in my jsp

    <spring:bind path="user.status.id">
    <select name="<c:out value="${status.expression}"/>">
    <c:forEach var="statusCode" items="${statusList}">
    <option <c:if test="${statusCode.id == status.value}">selected</c:if> value="<c:out value="${statusCode.id}"/>">
    <c:out value="${statusCode.status}"/></option>
    </c:forEach>
    </select>
    </spring:bind>

    The transaction manager mapping


    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager"><ref bean="transactionManager"/></property>
    <property name="transactionAttributeSource">
    <value>

    com.example.service.UserManager.get*=PROPAGATION_R EQUIRED,readOnly
    com.example.service.UserManager.save*=PROPAGATION_ REQUIRED,-DataAccessException
    </value>
    </property>
    </bean>

  • #2
    many-to-one

    Has anyone please solved this problem?

    Any suggestion would be verry apprecited.

    Comment

    Working...
    X