我正确使用EJB吗? [英] Am I using EJBs properly?

查看:100
本文介绍了我正确使用EJB吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JEE6堆栈,包括JPA 2.0,JSF 2.0,EJB 3.1等。

I am using a JEE6 stack including JPA 2.0, JSF 2.0, EJB 3.1 etc.

我的架构设置方式如下:

The way my architecture is setup is the following:

我使用hibernate作为JPA提供程序,使用JPA注释DAO。
我有JSF托管bean,它对应于我的facelet / xhtml页面。
我有处理所有数据库请求的EJB。

I have JPA annotated DAOs using hibernate as my JPA provider. I have JSF Managed beans which correspond to my facelet/xhtml pages. I have EJBs that handle all of my database requests.

我的XHTML页面有JSF EL,可以调用我的托管bean。我的托管bean包含对由EJB管理的DAO实体的引用。例如,我有一个映射到db表的用户实体。我有一个用户EJB,它处理所有返回用户的CRUD操作。我有一个编辑用户的页面。高级工作流程将是:导航到用户编辑页面 - > EL调用位于托管bean中的方法来加载用户。该方法从EJB调用userEJB.loadUser(user)以从数据库中获取用户。用户被编辑并提交 - >在托管bean中调用一个函数,该函数调用EJB中的函数来保存用户。等。

My XHTML pages have JSF EL which make calls to my Managed beans. My managed beans contain references to my DAO entities which are managed by EJBs. For example, I have a user entity which is mapped to a db table. I have a user EJB which handles all CRUD operations that return Users. I have a page that edits a user. The highlevel workflow would be: navigate to user edit page -> EL calls a method located in the managed bean that loads a user. The method calls userEJB.loadUser(user) from the EJB to get the user from the database. The user is edited and submit -> a function is called in the managed bean which calls a function in the EJB to save the user. etc.

我遇到了使用EJB访问我的JSF页面中的数据的问题。
我在延迟初始化错误方面遇到很多问题,我认为这是由于我已经设置好了。

I am running into issues accessing my data within my JSF pages using EJBs. I am having a lot of problems with lazy initialization errors, which I believe is due to how I have set things up.

例如,我有一个具有延迟加载的用户列表的客户端实体。为了获得
客户端,我在EJB中调用一个方法,该方法进入数据库,找到一个客户端并返回它。稍后在
我希望访问此客户端用户列表,为了这样做,我必须通过调用某种方法返回EJB以加载这些用户(因为它们被延迟加载)。这意味着我必须创建一个方法,例如

For example, I have a Client entity that has a List of users which are lazily loaded. In order to get a client I call a method in my EJB which goes to the database, finds a client and returns it. Later on i wish to access this clients list of users, in order to do so i have to go back to the EJB by calling some sort of method in order to load those users (since they are lazily loaded). This means that I have to create a method such as

public List<User> getUserListByClient(Client c)
{
    c = em.merge(c); return c.getUserList();
}

此方法的唯一目的是加载用户(我是甚至没有积极的这种方法是好的或有效的)。
如果我自己正在进行会话管理,我想让整个请求打开会话并直接访问该属性,这样会很好,因为会话无论如何会打开,似乎有一个额外的层这对EJB的间接性造成了困难。

The only purpose of this method is to load the users (and I'm not even positive this approach is good or works). If i was doing session management myself, I would like just leave the session open for the entire request and access the property directly, this would be fine as the session would be open anyway, there seems to be this one extra layer of indirection in EJBs which is making things difficult for me.

我喜欢EJB,因为我喜欢它们由容器控制,汇集,提供事务管理然而,我觉得我错误地使用它们,或者我错误地设置了我的JSF应用程序。

I do like EJBs as I like the fact that they are controlled by the container, pooled, offer transaction management for free etc. However, I get the feeling that I am using them incorrectly, or I have set up my JSF app incorrectly.

任何反馈都将不胜感激。

Any feedback would be greatly appreciated.

谢谢,

推荐答案

您的用法似乎很好。请记住,em.merge(c)可能会将对客户端c所做的更改保存到数据库中。如果您只想获取客户端c的UserList而不保存对客户端c所做的更改,那么您可以这样做:

Your usage seems good. Just remember that em.merge(c) may save the changes made to Client c into the database. If you just want to get the UserList of Client c without saving the changes made to the Client c, then you can do this:

public List<User> getUserListByClient(Client c)
{

     Client client = em.find(Client.class, c.clientId);
     return client.getUserList();

}

或者更好,只需将客户端ID发送到getUserListByClient传递一个完整的客户端对象,只是为了节省一点点内存:*

Or better ,just send the Client Id to the getUserListByClient instead of passing a full Client object , just to save a tinsy winsy bit of memory :)

这篇关于我正确使用EJB吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆