需要同时访问多个包内的实体bean, EJB3.0或者EJB2.1能不能解决?怎么解决?
具体的说,就是查询模块需要关联查询其他各个模块的记录,比如说:查询用户的业务办理记录,而用户和业务是两个模块,所以用户实体Bean和业务实体Bean就部署在不同的包里。所以需要同时访问这两个包,请问该怎么处理呢? 问题点数:50、回复次数:2Top
1 楼yeshucheng(叶澍成★七哥)回复于 2006-03-04 18:19:17 得分 5
我不知道楼主所说的不在同一个包是指不在同一个ear还是同一个包的含义
其实只要在同一个jvm中就可以调用,跨ear稍微麻烦点,但是EJB同样可以做到你的要求。
通过访问localhome来定位到你相关要的类就可以Top
2 楼mirroryuri(蜜仁)回复于 2006-03-09 16:18:15 得分 45
可以在ejb2.0中,利用sessionbean直接访问数据库。
下面我曾经做过的代码,楼主可以看看的:
/**
* @return JDBC 连接
*/
public Connection getConnection() throws Exception {
Context ctx = null;
try {
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/BOSSOracle9i");
return ds.getConnection();
} catch (Exception e) {
System.err.println("Couldn't get datasource!");
e.printStackTrace();
throw e;
}
finally {
ctx.close();
}
}
/**
* Business method
* @ejb.interface-method view-type = "both"
*/
public List findByQueryStr(String queryString, String version
) throws VersionException, FinderException, QueryException {
if (this.version.compareToIgnoreCase(version) > 0) {
throw new VersionException(this.getClass().getName());
}// 版本低。
PreparedStatement pstmt = null;
Connection conn = null;
List result = new ArrayList();
try {
System.out.println("ejbFindByQueryStr(" + queryString + ") called.");
conn = getConnection();
pstmt = conn.prepareStatement(queryString);
ResultSet rs = pstmt.executeQuery();
int count = rs.getMetaData().getColumnCount();
while (rs.next()) {
Object[] oneResult = new Object[count];
for (int colIndex = 0; colIndex < count; colIndex ++) {
oneResult[colIndex] = rs.getObject(colIndex + 1);
}
result.add(oneResult);
}
}catch (Exception e) {
e.printStackTrace();
throw new FinderException(e.toString());
}finally {
try {
if (pstmt != null)
pstmt.close();
} catch (Exception e) {
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
return result;
} Top




