当前位置: 技术问答>java相关
EJB初学者,遇到困难,求救各位!!!
来源: 互联网 发布时间:2017-04-13
本文导语: SessionBean 里面访问本地实体Bean,服务器报错如下: ------------ 服务器报错信息 ------------------- javax.ejb.EJBException: Attempt to pass a reference to an EJBLocalObject to a remote client. A local EJB component may only be access...
SessionBean 里面访问本地实体Bean,服务器报错如下:
------------ 服务器报错信息 -------------------
javax.ejb.EJBException: Attempt to pass a reference to an EJBLocalObject to a remote client. A local EJB component may only be accessed by clients co-located in the same ear or standalone jar file.
------------------------------------------------
SessionBean与实体Bean在同一包下面而且在同一个jar文件内,其代码如下:
**************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.sql.*;
public interface Zxadvice extends javax.ejb.EJBLocalObject {
public Integer getCid();
public void setTname(String tname);
public String getTname();
public void setCompanyname(String companyname);
public String getCompanyname();
public void setAddress(String address);
public String getAddress();
public void setMailcode(String mailcode);
public String getMailcode();
public void setTelephonecode(String telephonecode);
public String getTelephonecode();
public void setEmail(String email);
public String getEmail();
public void setAdvice(String advice);
public String getAdvice();
public void setTtime(Timestamp ttime);
public Timestamp getTtime();
}
*************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
public interface ZxadviceHome extends javax.ejb.EJBLocalHome {
public Zxadvice create(Integer cid) throws CreateException;
public Collection findBytname(String name) throws FinderException;
public Zxadvice findByPrimaryKey(Integer cid) throws FinderException;
}
**************
package j2eetest.zxadvice;
import javax.ejb.*;
abstract public class ZxadviceBean implements EntityBean {
EntityContext entityContext;
public java.lang.Integer ejbCreate(java.lang.Integer cid) throws CreateException {
setCid(cid);
return null;
}
public void ejbPostCreate(java.lang.Integer cid) throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() throws RemoveException {
/**@todo Complete this method*/
}
public abstract void setCid(java.lang.Integer cid);
public abstract void setTname(java.lang.String tname);
public abstract void setCompanyname(java.lang.String companyname);
public abstract void setAddress(java.lang.String address);
public abstract void setMailcode(java.lang.String mailcode);
public abstract void setTelephonecode(java.lang.String telephonecode);
public abstract void setEmail(java.lang.String email);
public abstract void setAdvice(java.lang.String advice);
public abstract void setTtime(java.sql.Timestamp ttime);
public abstract java.lang.Integer getCid();
public abstract java.lang.String getTname();
public abstract java.lang.String getCompanyname();
public abstract java.lang.String getAddress();
public abstract java.lang.String getMailcode();
public abstract java.lang.String getTelephonecode();
public abstract java.lang.String getEmail();
public abstract java.lang.String getAdvice();
public abstract java.sql.Timestamp getTtime();
public void ejbLoad() {
/**@todo Complete this method*/
}
public void ejbStore() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
}
********************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
import j2eetest.tools.*;
public interface ZxadviceSession extends javax.ejb.EJBObject {
public Collection getContent(String cid) throws RemoteException;
}
*******************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface ZxadviceSessionHome extends javax.ejb.EJBHome {
public ZxadviceSession create() throws CreateException, RemoteException;
}
******************
package j2eetest.zxadvice;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class ZxadviceSessionBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public java.util.Collection getContent(java.lang.String cid) {
ArrayList al = new ArrayList();
try {
// al = (ArrayList)this.getHome().findByPrimaryKey(Integer.getInteger(cid));
al = (ArrayList)this.getHome().findBytname(cid);
}catch(Exception e){
e.printStackTrace();
}
return al;
}
private ZxadviceHome getHome() throws Exception {
System.out.println("====== above InitialContext ======");
System.out.println("====== after InitialContext ======");
Context ic = new InitialContext();
Object ref = ic.lookup("Zxadvice");
System.out.println("====== after lookup ======");
System.out.println("obj = "+ref);
ZxadviceHome imh = (ZxadviceHome)PortableRemoteObject.narrow(ref , ZxadviceHome.class);
// ZxadviceHome imh = (ZxadviceHome) PortableRemoteObject.narrow(ref,ZxadviceHome.class);
return imh;
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
return null;
}
}
}
**************
------------ 服务器报错信息 -------------------
javax.ejb.EJBException: Attempt to pass a reference to an EJBLocalObject to a remote client. A local EJB component may only be accessed by clients co-located in the same ear or standalone jar file.
------------------------------------------------
SessionBean与实体Bean在同一包下面而且在同一个jar文件内,其代码如下:
**************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.sql.*;
public interface Zxadvice extends javax.ejb.EJBLocalObject {
public Integer getCid();
public void setTname(String tname);
public String getTname();
public void setCompanyname(String companyname);
public String getCompanyname();
public void setAddress(String address);
public String getAddress();
public void setMailcode(String mailcode);
public String getMailcode();
public void setTelephonecode(String telephonecode);
public String getTelephonecode();
public void setEmail(String email);
public String getEmail();
public void setAdvice(String advice);
public String getAdvice();
public void setTtime(Timestamp ttime);
public Timestamp getTtime();
}
*************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
public interface ZxadviceHome extends javax.ejb.EJBLocalHome {
public Zxadvice create(Integer cid) throws CreateException;
public Collection findBytname(String name) throws FinderException;
public Zxadvice findByPrimaryKey(Integer cid) throws FinderException;
}
**************
package j2eetest.zxadvice;
import javax.ejb.*;
abstract public class ZxadviceBean implements EntityBean {
EntityContext entityContext;
public java.lang.Integer ejbCreate(java.lang.Integer cid) throws CreateException {
setCid(cid);
return null;
}
public void ejbPostCreate(java.lang.Integer cid) throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() throws RemoveException {
/**@todo Complete this method*/
}
public abstract void setCid(java.lang.Integer cid);
public abstract void setTname(java.lang.String tname);
public abstract void setCompanyname(java.lang.String companyname);
public abstract void setAddress(java.lang.String address);
public abstract void setMailcode(java.lang.String mailcode);
public abstract void setTelephonecode(java.lang.String telephonecode);
public abstract void setEmail(java.lang.String email);
public abstract void setAdvice(java.lang.String advice);
public abstract void setTtime(java.sql.Timestamp ttime);
public abstract java.lang.Integer getCid();
public abstract java.lang.String getTname();
public abstract java.lang.String getCompanyname();
public abstract java.lang.String getAddress();
public abstract java.lang.String getMailcode();
public abstract java.lang.String getTelephonecode();
public abstract java.lang.String getEmail();
public abstract java.lang.String getAdvice();
public abstract java.sql.Timestamp getTtime();
public void ejbLoad() {
/**@todo Complete this method*/
}
public void ejbStore() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void unsetEntityContext() {
this.entityContext = null;
}
public void setEntityContext(EntityContext entityContext) {
this.entityContext = entityContext;
}
}
********************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
import j2eetest.tools.*;
public interface ZxadviceSession extends javax.ejb.EJBObject {
public Collection getContent(String cid) throws RemoteException;
}
*******************
package j2eetest.zxadvice;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface ZxadviceSessionHome extends javax.ejb.EJBHome {
public ZxadviceSession create() throws CreateException, RemoteException;
}
******************
package j2eetest.zxadvice;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
public class ZxadviceSessionBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public java.util.Collection getContent(java.lang.String cid) {
ArrayList al = new ArrayList();
try {
// al = (ArrayList)this.getHome().findByPrimaryKey(Integer.getInteger(cid));
al = (ArrayList)this.getHome().findBytname(cid);
}catch(Exception e){
e.printStackTrace();
}
return al;
}
private ZxadviceHome getHome() throws Exception {
System.out.println("====== above InitialContext ======");
System.out.println("====== after InitialContext ======");
Context ic = new InitialContext();
Object ref = ic.lookup("Zxadvice");
System.out.println("====== after lookup ======");
System.out.println("obj = "+ref);
ZxadviceHome imh = (ZxadviceHome)PortableRemoteObject.narrow(ref , ZxadviceHome.class);
// ZxadviceHome imh = (ZxadviceHome) PortableRemoteObject.narrow(ref,ZxadviceHome.class);
return imh;
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
return null;
}
}
}
**************
|
你的问题可能是想把CMP(Local)的引用传给Client,这是不行的,因为它
是本地的。
一般是通过一个SessionBean来包裹本地的实体Bean,client只能访问SessionBean,建议看看Facade模式。
是本地的。
一般是通过一个SessionBean来包裹本地的实体Bean,client只能访问SessionBean,建议看看Facade模式。
|
顶~!
|
检查一下Sessionbean,可能在调用Entitybean的Home接口时通过了远程调用,而不是 Local。