Java EE:EJB 实体、Web 独立
你能告诉我怎么做最好吗? 如果我只有接口,如何在 Web 应用程序上创建实体的新实例..
我有:
- EJB(3.1) Project
- Web 项目
我有一个 EJB 项目,例如:
User.java
@Entity
public class User {
//variables.. getters and setters...
}
UserManagerBean.java
@Stateful
public class UserManagerBean implements UserManager {
//getters and setters...
//.......
public void addUser(User user) {
//implemented here
}
//.......
}
UserManager.java
@Local
@Remote
public interface UserManager {
//methods here
}
在 Web 应用程序(独立的 Web 应用程序)中,我有以下内容:
UserManager.java
public interface UserManager {
//methods here
}
User.java
public interface User {
//methods here
}
现在..在一粒豆子里...我我正在尝试从远程上下文获取我的 bean 并使用它们:
//ctx is created with the specific properties to access remote context..
InitialContext ctx = new InitialContext();
userManager = (UserManager)ctx.lookup("java:global/project/projectEJB/UserManagerBean");
User user = userManager.getUserById(1); //working
User new_user = (User)ctx.lookup("java:global/project/projectEJB/User"); //not working
//I know this is not supposed to work.. but how can I do this?
我知道您无法获取实体的实例,因为它不是 ejb... 我是否必须创建一个普通的用户类别,其所有内容都与projectEJB 上的一样?难道就没有更好的解决办法吗? 还有哪些其他解决方案?我搜索了谷歌,似乎每个人都知道如何做到这一点..只有我不...
提前谢谢你..
Can you please tell me how is best to do?
How can I create a new instance of an entity on web application if I have only the interface..
I have :
- EJB(3.1) Project
- Web project
I have an EJB Project like:
User.java
@Entity
public class User {
//variables.. getters and setters...
}
UserManagerBean.java
@Stateful
public class UserManagerBean implements UserManager {
//getters and setters...
//.......
public void addUser(User user) {
//implemented here
}
//.......
}
UserManager.java
@Local
@Remote
public interface UserManager {
//methods here
}
In the web application(a standalone web application) I have this:
UserManager.java
public interface UserManager {
//methods here
}
User.java
public interface User {
//methods here
}
Now.. in a bean... I am trying to get from the remote context my beans and use them:
//ctx is created with the specific properties to access remote context..
InitialContext ctx = new InitialContext();
userManager = (UserManager)ctx.lookup("java:global/project/projectEJB/UserManagerBean");
User user = userManager.getUserById(1); //working
User new_user = (User)ctx.lookup("java:global/project/projectEJB/User"); //not working
//I know this is not supposed to work.. but how can I do this?
I know you can't get an instance of an entity because it's not an ejb...
Do I have to create a normal class of user having everying as on projectEJB? isn't there a better solution?
What other solutions are there? I searched for google and it seems like everybody knows how to do this.. only I don't...
Thank you in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要创建 User 对象,您只需使用“new”运算符创建一个常规 Java 实例,然后调用您的 EJB 调用。
客户端(您的 Web 应用程序)和服务器端(EJB 3.1 项目)都必须知道如何序列化和反序列化 User 对象,因此每一方都必须有权访问 User 类和 EJB 接口。
在打包方面,您可以将 EJB 接口和用户实体类全部捆绑到客户端和服务器端都使用的单个 JAR 中。
另一种部署是将 Web 应用程序和 EJB 捆绑到部署在同一 JVM 内的单个应用程序中。
To create the User object you just create a regular Java instance with the 'new' operator and then invoke your EJB call.
Both the client side (your web application) and the server side (EJB 3.1 project) must know how to serialize and deserialize the User object therefore each side must have access to the User class and the EJB interfaces.
In terms of packaging you could bundle the EJB interface and User entity class all into a single JAR that is used by both the client and server sides.
An alternative deployment would be to bundle the web app and EJB into a single application deployed inside the same JVM.
实体 bean 不应该直接在 Web 层中创建 - 因为为了使它们持久化,您需要 EntityManager,它不能保证线程安全(这在 servlet 上下文中很重要)。
您可能想要做的是编写一个 DAO EJB,其中包含一个方法来创建新用户、在 servlet 中注入/查找它并调用它。首先谷歌搜索“GenericDAO”模式。
Entity beans are not supposed to be created directly in web layer — because for making them persistent them you need the
EntityManager
, which is not guaranteed to be thread safe (and that's important in a servlet context).What you probably want to do is writing a DAO EJB with a method to create a new user, inject/look it up in the servlet, and call it. Google for "GenericDAO" pattern to start with.