如有重复请见谅。
业务层是否可以或推荐使用对象而不是 id?
SELECT c
FROM Child AS c
WHERE c.parent = :parent
public List<Child> list(final Parent parent) {
// does parent must be managed?
// how can I know that?
// have parent even been persisted?
return em.createNamedQuery(...).
setParameter("parent", parent);
}
这就是我的工作方式。
SELECT c
FROM Child AS c
WHERE c.parent.id = :parent_id
public List<Child> list(final Parent parent) {
// wait! parent.id could be null!
// it may haven't been persisted yet!
return list(parent.getId());
}
public List<Child> list(final long parentId) {
return em.createNamedQuery(...).
setParameter("parent_id", parentId);
}
更新的问题 --------------------------------------
@EJB
是否可以在同一个 JTA 中说任何可以注入的 JAX-RS 或 JAX-WS 类?
我一直好奇的原始问题来了。
假设我们有两个 EJB。
@Stateless
class ParentBean {
public Parent find(...) {
}
}
@Stateless
class ChildBean {
public List<Child> list(final Parent parent) {
}
public List<Child> list(final long parentId) {
}
}
处理任何 EJB 客户端的正确方法是什么?
@Stateless // <<-- This is mandatory for being injected with @EJB, right?
@Path("/parents/{parent_id: \\d+}/children")
class ChildsResource {
@GET
@Path
public Response list(@PathParam("parent_id") final long parentId) {
// do i just have to stick to this approach?
final List<Child> children1 = childBean.list(parentId);
// is this parent managed?
// is it ok to pass to other EJB?
final Parent parent = parentBean.find(parentId);
// is this gonna work?
final List<Child> children2 = childBean.list(parent);
...
}
@EJB
private ParentBean parentBean;
@EJB
private ChildBean childBean;
}