简体   繁体   English

Java EE:EJB实体,Web Standalone

[英]Java EE: EJB Entity, Web Standalone

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.. 如果我只有接口,如何在Web应用程序上创建实体的新实例..

I have : 我有 :

  • EJB(3.1) Project EJB(3.1)项目
  • Web project 网络项目

I have an EJB Project like: 我有一个EJB项目,如:

User.java User.java

@Entity
public class User {
    //variables.. getters and setters...
}

UserManagerBean.java UserManagerBean.java

@Stateful
public class UserManagerBean implements UserManager {
    //getters and setters...
    //.......
    public void addUser(User user) {
        //implemented here
    }
    //.......
}

UserManager.java UserManager.java

@Local
@Remote
public interface UserManager {
    //methods here
}

In the web application(a standalone web application) I have this: 在Web应用程序(独立的Web应用程序)中,我有:

UserManager.java UserManager.java

public interface UserManager {
    //methods here
}

User.java 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: 现在..在一个bean ...我试图从远程上下文我的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?

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? 我知道你不能得到一个实体的实例,因为它不是一个ejb ...我是否必须创建一个普通的用户类,如同在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.. 先感谢您..

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). 实体bean不应该直接在Web层中创建 - 因为要使它们持久存在,您需要EntityManager ,这不能保证是线程安全的(这在servlet上下文中很重要)。

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. 您可能想要做的是编写一个DAO EJB,其方法是创建一个新用户,在servlet中注入/查找它,然后调用它。 Google for "GenericDAO" pattern to start with. 谷歌以“GenericDAO”模式开始。

To create the User object you just create a regular Java instance with the 'new' operator and then invoke your EJB call. 要创建User对象,只需使用'new'运算符创建常规Java实例,然后调用EJB调用。

User myNewUser = new User();
userManager.addUser(myNewUser);

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. 客户端(您的Web应用程序)和服务器端(EJB 3.1项目)都必须​​知道如何序列化和反序列化User对象,因此每一方都必须能够访问User类和EJB接口。

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. 在打包方面,您可以将EJB接口和User实体类全部捆绑到客户端和服务器端使用的单个JAR中。

An alternative deployment would be to bundle the web app and EJB into a single application deployed inside the same JVM. 另一种部署方式是将Web应用程序和EJB捆绑到同一JVM内部署的单个应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM