hibernate subclass map 父类到子类时转换出错(大家帮下忙)(3)

07-10-26 dxadnwfn

实体对象:

主表:

public class CreditInquiry implements Serializable {

private static final long serialVersionUID = 1L;

public static String REF = "CreditInquiry";

public static String PROP_TYPE = "type";

public static String PROP_TYPE_NAME = "typeName";

public static String PROP_NO = "no";

public static String PROP_CREATE_TIME = "createTime";

public static String PROP_UPDATE_TIME = "updateTime";

public static String PROP_ORGANIZATION_NO = "organizationNo";

public static String PROP_UPDATE_ORGANIZATION_NO = "updateOrganizationNo";

public static String PROP_STAFFER_NO = "stafferNo";

public static String PROP_UPDATE_STAFFER_NO = "updateStafferNo";

public static String PROP_ID = "Id";

private int hashCode = Integer.MIN_VALUE;

// primary key

private java.lang.Long id;

// fields

private java.lang.String type;

private java.lang.String typeName;

private java.lang.String no;

private java.util.Date createTime;

private java.util.Date updateTime;

private java.lang.String organizationNo;

private java.lang.String updateOrganizationNo;

private java.lang.String stafferNo;

private java.lang.String updateStafferNo;

...........

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

}

子类..

public class CreditInquiryIndividualCustomer extends CreditInquiry implements Serializable {

private static final long serialVersionUID = 1L;

public static String REF = "CreditInquiryIndividualCustomer";

public static String PROP_NAME = "name";

public static String PROP_VALUE = "value";

public static String PROP_CREDIT_INQUIRY_ID = "CreditInquiryId";

private int hashCode = Integer.MIN_VALUE;

// collections

private java.util.Map properties;

......

......

}

我也问过同事,说这个表就不合理,这里没有父子关系,这里没有子类,这里我把它看作子类的[类],其实是一个属性而已..

请大家帮我分析一下..

banq
2007-10-27 17:28

没有必要贴这么多代码,只要日志就可以了。

>java.lang.ClassCastException: com.skyon.mica.frontage.entity.CreditInquiry$$EnhancerByCGLIB$$67a4c7a6

建议研究一下Hibernate的懒加载,缺省lazy=true,也就是说从Hibernate获得父对象时,其子对象可能都没有真正加载,而是一个Proxy类型,所以,这时如果你对其子对象进行类型切换,很容易出现ClassCastException,试验加入Lazy="false"看看。

懒加载正常运行时是必须的,提高性能,减少数据库不必要访问,但是需要Open Session In View或者Jdon框架的Close Session In View,具体用法可见:

http://www.jdon.com/article/32694.html