1

I have a class ContactAddress.java which has this relationship

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "CNTRY_ID")
private Country country;

Country.java has these things

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CNTRY_ID")
private Integer id;

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

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

@Column(name = "SRT_ORDR")
private Integer sortOrder;

@OneToMany(mappedBy="country")
private List<State> states = new ArrayList<State>();

In ContactAddressAction.java I call the save method

    public String save() throws Exception {
    // Add the relationship
    contactAddress.setAccount(getAccount());

    // Is this an update?
    if (index != null) {
        getAccount().getContactAddresses().set(index, contactAddress);
    } else {

        List<ContactAddress> contactAddressList = getAccount().getContactAddresses();
        contactAddressList.add(contactAddress);
        //getAccount().getContactAddresses().add(contactAddress);
        getAccount().setContactAddresses(contactAddressList);



    }

    return SUCCESS;
}

While calling this method the above exception comes.The list of contact adrreses are saved in the database but are not showing up immidiately after save.

JSP code

c:forEach items="${addresses}" var="address" varStatus="stat">
                <s:url id="updateAddressUrl" action="update" namespace="/account/address">
                    <s:param name="index" value="${stat.index}" />
                </s:url>
                <s:url id="deleteAddressUrl" action="delete" namespace="/account/address">
                    <s:param name="index" value="${stat.index}" />
                </s:url>
                <tr class="address deletable">
                    <td>${address.type}</td>
                    <td>${address.primary ? 'Yes' : 'No'}</td>
                    <td>${address.active ? 'Yes' : 'No'}</td>
                    <td>${address.companyName}</td>
                    <td>
                        <v:address value="${address}"/>
                        <hr>
                        <table class="info">
                            <tr>
                                <th>Best Contact Time:</th>
                                <td>${address.bestContactTime}</td>
                            </tr>
                            <tr>
                                <th>Office Hours:</th>
                                <td>
                                    Monday: <span class="value">${address.officeHourMonday}</span>&nbsp;
                                    Tuesday:<span class="value">${address.officeHourTuesday}</span>&nbsp;
                                    Wednesday:<span class="value">${address.officeHourWednesday}</span>&nbsp;
                                    Thursday:<span class="value">${address.officeHourThursday}</span>&nbsp;
                                    Friday:<span class="value">${address.officeHourFriday}</span>
                                </td>
                            </tr>
                            <tr>
                                <th>Lunch Time:</th>
                                <td>${address.officeLunchTime}</td>
                            </tr>
                        </table>
                    </td>
                    <td class="actions">
                        <a href="${updateAddressUrl}" class="popup-link button-link"><strong>Edit</strong></a>
                        <c:if test="${user.primaryAddressManageable}">
                            <a href="${deleteAddressUrl}" class="deletelink button-link"><strong>Delete</strong></a>
                        </c:if>             
                    </td>
                </tr>
            </c:forEach>

Also, If I debug the code and look up the List variable through VARIABLES ,it's working fine and shows the list after saving.

Where am I not correct?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2820678
  • 33
  • 1
  • 6

2 Answers2

0

This kind of errors occurs in JPA when you have a LAZY initialization type and try to update a list like

demoObject.getDemoList().add()

because the object DemoObject was already fetched without the List, and the JPA database session was already closed.

A workaround for this is calling in the service that queries the object

demoObject.getDemoList().size()

to force it to retrieve an the list.

Carlos487
  • 1,459
  • 13
  • 17
  • But not change the save method, change the method in which you retrieve the object. Or you can test changing the relationship to EAGER – Carlos487 Oct 09 '13 at 16:14
0

Have you used OpenSessionInViewFilter in you web.xml?

<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class>
 <init-param>
    <param-name>sessionFactoryBeanName</param-name>
    <param-value>sessionFactory</param-value>
  </init-param>

</filter>

<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/orm/hibernate3/support/OpenSessionInViewFilter.html

or create your own class and extend OpenSessionInViewFilter for example

public class MyFilter extends OpenSessionInViewFilter {
public void closeSession(Session session, SessionFactory sessionFactory){
session.flush();
super.closeSession(session,sessionFactory);
}

and set filter in your web.xml

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>your class package.MyFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>

</filter>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
S.M.V.A
  • 470
  • 1
  • 5
  • 14
  • Servlet.service() for servlet default threw exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387) – user2820678 Oct 09 '13 at 17:59
  • for this new error refer this http://stackoverflow.com/questions/14390823/getting-no-bean-named-sessionfactory-error-when-using-opensessioninviewfilter it will work i think – S.M.V.A Oct 09 '13 at 18:09
  • Fixed the beandefination error but I tried adding the listener mentioned above but it dint work. – user2820678 Oct 09 '13 at 20:20