Struts2 获取对象对应的外键对象报错:ognl.NoConversionPossible

作者 做棵大树 日期 2018-10-28
Struts2 获取对象对应的外键对象报错:ognl.NoConversionPossible

在 SSH 项目中定义了两个有关联的类,分别是公司和项目,其中公司和项目是一对多的关系,一个公司可以存在多个项目,然后在获取相关属性时候,会报错了:ognl.NoConversionPossible。 那先把代码奉上,然后再说一下为什么会报这个错误: 项目实体类:

package com.test.beans;

/**
* BidProject entity. @author MyEclipse Persistence Tools
*/

public class Project implements java.io.Serializable {

// Fields
private String id;
private Company company ;

// Constructors

/\*\* default constructor */
public BidProject() {
}

// Property accessors

public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}
public Company getCompany () {
    return this.company ;
}

public void setCompany (Company company ) {
    this.company = company ;
}

}

}

公司实体类:

package com.rocky.beans;

/**
* Corporation entity. @author MyEclipse Persistence Tools
*/

public class Corporation implements java.io.Serializable {

private String id;    //ID生成器生成ID 2
private String name;    //前台自动填充1
    public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}
    public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

}

页面代码:

<s:iterator value=”#session.allBidList” id=”allBid”>
<s:property value=”#allBid.getCorporation().getName()”/>
</s:iterator>

这是为什么呢? 因为在Hibernate反向生成 .hbm.xml 文件时,其中的 lazy 属性的问题,它默认是打开的,所以就会出现这种情况。 至于如何解决,则是找到 .hbm.xml 文件下对应的属性映射,然后修改其属性值

<many-to-one name=”corporation” class=”com.rocky.beans.Corporation” lazy=”false” fetch=”select”>

应在标签中加入lazy=”false”属性,表示不使用懒加载方式即可解决问题。