测试FetchType

一个部门树的实体Bean

 

@Entity
@NamedQuery(name 
=   " Department.findAll " , query  =   " select o from Department o " )
@Table(name 
=   " DEPARTMENTS " )
public   class  Department  implements  Serializable  {
    @Column(nullable 
= false)
    
private String name;//部门名称
    @Id
    @Column(nullable 
= false)
    
private String no;//部门编号
    @ManyToOne
    @JoinColumn(name 
= "PARENT_NO", referencedColumnName = "NO")
    
private Department department;//父部门
    @OneToMany(mappedBy = "department")
    
private List<Department> departmentList;//子部门列表

 

对于一对多关系默认的FetchType=Lazy,这个时候如果你要在客户端通过远程接口获取一对多属性就有些麻烦了.

象这样一个返回所有部门的Sesion方法:

 

     /** <code>select o from Department o</code> */
    
public  List < Department >  queryDepartmentFindAll()  {
       
return em.createNamedQuery("Department.findAll").getResultList();

    }

是否需要这样手工的遍历部门的属性?才能够在客户端调用获取部门的departmentList 属性.

     /** <code>select o from Department o</code> */
    
public  List < Department >  queryDepartmentFindAll()  {
        List li
=em.createNamedQuery("Department.findAll").getResultList();
        
if(li.size()>0){
            
for(int i=0;i<li.size();i++){
                ((Department)li.get(i)).getDepartmentList().size();
            }

        }

        
return li;
    }

 

对于多对一关系,我认为默认的FetchType应该等于EAGER,当设为Lazy时如下:

在department属性使用Lazy Load时:    @ManyToOne(fetch=FetchType.LAZY)

public   class  User  implements  Serializable  {
    @Id
    @Column(nullable 
= false)
    
private Long id;
    @Column(nullable 
= false)
    
private String name;
    @Column(nullable 
= false)

    @ManyToOne(fetch
=FetchType.LAZY)
    @JoinColumn(name 
= "DEPARTMENT_NO", referencedColumnName = "NO")
    
private Department department;    

...........

系统报错:

D:/jdevelepor/jdk/bin/javaw.exe -client -classpath E:/ADF/XinEJB/EJBTest/classes;E:/ADF/XinEJB/Model/classes;D:/jdevelepor/toplink/jlib/toplink.jar;D:/jdevelepor/toplink/jlib/toplink-oc4j.jar;D:/jdevelepor/toplink/jlib/antlr.jar;D:/jdevelepor/lib/xmlparserv2.jar;D:/jdevelepor/lib/xml.jar;D:/jdevelepor/j2ee/home/lib/ejb30.jar;D:/jdevelepor/toplink/jlib/toplink-essentials.jar;D:/jdevelepor/j2ee/home/lib/activation.jar;D:/jdevelepor/j2ee/home/lib/ejb.jar;D:/jdevelepor/j2ee/home/lib/jms.jar;D:/jdevelepor/j2ee/home/lib/jta.jar;D:/jdevelepor/j2ee/home/lib/mail.jar;D:/jdevelepor/j2ee/home/lib/servlet.jar;D:/jdevelepor/BC4J/lib/adfshare.jar;D:/jdevelepor/BC4J/lib/adfm.jar;D:/jdevelepor/BC4J/lib/collections.jar;D:/jdevelepor/BC4J/jlib/adfui.jar;D:/jdevelepor/BC4J/lib/adfbinding.jar;D:/jdevelepor/BC4J/jlib/adfmtl.jar;D:/jdevelepor/BC4J/lib/bc4jmt.jar;D:/jdevelepor/BC4J/lib/bc4jct.jar;D:/jdevelepor/jlib/jdev-cm.jar;D:/jdevelepor/jlib/ojmisc.jar;D:/jdevelepor/jlib/commons-el.jar;D:/jdevelepor/jlib/jsp-el-api.jar;D:/jdevelepor/jlib/oracle-el.jar;D:/jdevelepor/BC4J/jlib/bc4jtester.jar;D:/jdevelepor/jlib/help4.jar;D:/jdevelepor/jlib/share.jar;D:/jdevelepor/jlib/jewt4.jar;D:/jdevelepor/jlib/oracle_ice.jar;D:/jdevelepor/jdbc/lib/ojdbc14dms.jar;D:/jdevelepor/jdbc/lib/orai18n.jar;D:/jdevelepor/jdbc/lib/ocrs12.jar;D:/jdevelepor/diagnostics/lib/ojdl.jar;D:/jdevelepor/lib/dms.jar;D:/jdevelepor/BC4J/lib/bc4jdomorcl.jar;D:/jdevelepor/BC4J/jlib/bc4jdatum.jar;D:/jdevelepor/jdev/system/oracle.j2ee.10.1.3.39.14/embedded-oc4j/.client;D:/jdevelepor/j2ee/home/oc4j.jar;D:/jdevelepor/j2ee/home/lib/oc4j-internal.jar;D:/jdevelepor/opmn/lib/optic.jar ejbtest.BaseFacadeBeanClient
2007-4-24 17:14:12 oracle.j2ee.rmi.RMIMessages EXCEPTION_ORIGINATES_FROM_THE_REMOTE_SERVER
警告: 远程服务器 {0} 返回异常错误
java.rmi.UnmarshalException: Error deserializing return-value: java.io.InvalidClassException: com.zspost.xin.model.entities.User; local class incompatible: stream classdesc serialVersionUID = -175996434274999754, local class serialVersionUID = 7770971911119058238
 at com.evermind.server.rmi.RMIClientConnection.handleMethodInvocationResponse(RMIClientConnection.java:806)
 at com.evermind.server.rmi.RMIClientConnection.handleOrmiCommandResponse(RMIClientConnection.java:250)
 at com.evermind.server.rmi.RMIClientConnection.dispatchResponse(RMIClientConnection.java:205)
 at com.evermind.server.rmi.RMIClientConnection.processReceivedCommand(RMIClientConnection.java:187)
 at com.evermind.server.rmi.RMIConnection.handleCommand(RMIConnection.java:152)
 at com.evermind.server.rmi.RMIConnection.listenForOrmiCommands(RMIConnection.java:127)
 at com.evermind.server.rmi.RMIConnection.run(RMIConnection.java:107)
 at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:814)
 at java.lang.Thread.run(Thread.java:595)
com.evermind.reflect.UndeclaredExceptionTypeException: oracle.oc4j.rmi.OracleRemoteException
 at __Proxy1.queryUserFindById(Unknown Source)
 at ejbtest.BaseFacadeBeanClient.main(BaseFacadeBeanClient.java:38)
oracle.oc4j.rmi.OracleRemoteException: Invocation error: java.rmi.UnmarshalException: Error deserializing return-value: java.io.InvalidClassException: com.zspost.xin.model.entities.User; local class incompatible: stream classdesc serialVersionUID = -175996434274999754, local class serialVersionUID = 7770971911119058238
 at com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:139)
 at com.evermind.server.rmi.RMIClientConnection.obtainRemoteMethodResponse(RMIClientConnection.java:480)
 at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:424)
 at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
 at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
 at com.evermind.server.ejb.StatelessSessionRemoteInvocationHandler.invoke(StatelessSessionRemoteInvocationHandler.java:43)
 at __Proxy1.queryUserFindById(Unknown Source)
 at ejbtest.BaseFacadeBeanClient.main(BaseFacadeBeanClient.java:38)

 Nested exception is:
java.rmi.UnmarshalException: Error deserializing return-value: java.io.InvalidClassException: com.zspost.xin.model.entities.User; local class incompatible: stream classdesc serialVersionUID = -175996434274999754, local class serialVersionUID = 7770971911119058238
 at com.evermind.server.rmi.RMICall.EXCEPTION_ORIGINATES_FROM_THE_REMOTE_SERVER(RMICall.java:109)
 at com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:125)
 at com.evermind.server.rmi.RMIClientConnection.obtainRemoteMethodResponse(RMIClientConnection.java:480)
 at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:424)
 at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
 at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
 at com.evermind.server.ejb.StatelessSessionRemoteInvocationHandler.invoke(StatelessSessionRemoteInvocationHandler.java:43)
 at __Proxy1.queryUserFindById(Unknown Source)
 at ejbtest.BaseFacadeBeanClient.main(BaseFacadeBeanClient.java:38)
oracle.oc4j.rmi.OracleRemoteException: Invocation error: java.rmi.UnmarshalException: Error deserializing return-value: java.io.InvalidClassException: com.zspost.xin.model.entities.User; local class incompatible: stream classdesc serialVersionUID = -175996434274999754, local class serialVersionUID = 7770971911119058238
 at com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:139)
 at com.evermind.server.rmi.RMIClientConnection.obtainRemoteMethodResponse(RMIClientConnection.java:480)
 at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:424)
 at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
 at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
 at com.evermind.server.ejb.StatelessSessionRemoteInvocationHandler.invoke(StatelessSessionRemoteInvocationHandler.java:43)
 at __Proxy1.queryUserFindById(Unknown Source)
 at ejbtest.BaseFacadeBeanClient.main(BaseFacadeBeanClient.java:38)

 Nested exception is:
java.rmi.UnmarshalException: Error deserializing return-value: java.io.InvalidClassException: com.zspost.xin.model.entities.User; local class incompatible: stream classdesc serialVersionUID = -175996434274999754, local class serialVersionUID = 7770971911119058238
 at com.evermind.server.rmi.RMICall.EXCEPTION_ORIGINATES_FROM_THE_REMOTE_SERVER(RMICall.java:109)
 at com.evermind.server.rmi.RMICall.throwRecordedException(RMICall.java:125)
 at com.evermind.server.rmi.RMIClientConnection.obtainRemoteMethodResponse(RMIClientConnection.java:480)
 at com.evermind.server.rmi.RMIClientConnection.invokeMethod(RMIClientConnection.java:424)
 at com.evermind.server.rmi.RemoteInvocationHandler.invoke(RemoteInvocationHandler.java:63)
 at com.evermind.server.rmi.RecoverableRemoteInvocationHandler.invoke(RecoverableRemoteInvocationHandler.java:28)
 at com.evermind.server.ejb.StatelessSessionRemoteInvocationHandler.invoke(StatelessSessionRemoteInvocationHandler.java:43)
 at __Proxy1.queryUserFindById(Unknown Source)
 at ejbtest.BaseFacadeBeanClient.main(BaseFacadeBeanClient.java:38)
Process exited with exit code 0.

 

估计这个错误是由于FetchType 类型与nullable有冲突,当这个值不为空时,你又lazy load 会有问题吗?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值