1

我有一个具有这种关系的类 ContactAddress.java

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

Country.java 有这些东西

@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>();

在 ContactAddressAction.java 我调用 save 方法

    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;
}

调用此方法时出现上述异常。联系人地址列表保存在数据库中,但保存后不会立即显示。

JSP代码

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>

此外,如果我调试代码并通过 VARIABLES 查找 List 变量,它工作正常并在保存后显示列表。

我哪里不对?

4

2 回答 2

0

当您具有 LAZY 初始化类型并尝试更新列表时,JPA 中会发生这种错误

demoObject.getDemoList().add()

因为已经在没有 List 的情况下获取了对象 DemoObject,并且 JPA 数据库会话已经关闭。

一种解决方法是调用查询对象的服务

demoObject.getDemoList().size()

强制它检索列表。

于 2013-10-09T14:57:21.113 回答
0

你在 web.xml 中使用过 OpenSessionInViewFilter 吗?

<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

或创建自己的类并扩展 OpenSessionInViewFilter 例如

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

并在您的 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>
于 2013-10-09T17:33:01.563 回答