简体   繁体   English

缺少上下文的EntityManager调用了无状态会话Bean

[英]Stateless session bean called out of context missing EntityManager

I am new to Java EE so my question may be very basic. 我是Java EE的新手,所以我的问题可能很基础。 I have built following REST web service with Stateless session bean (simplyfied): 我使用无状态会话bean(简化)构建了以下REST Web服务:

@Path("/list/")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Stateless
public class ListResource {

    @PersistenceContext(unitName = "unitName")
    private EntityManager em;

    @GET
    @Path("/")
    public List<Compound> getCompounds() {

    List<Compound> list = tq.getResultList();

    if (list.isEmpty()) {
        throw new WebApplicationException(Response.status(404).entity("There are no compounds in database.").build());
    }

    return list;
    }
}

It works like charm. 它像魅力一样工作。 Its accessible via URL and return JSON. 可通过URL访问并返回JSON。 Problem is that I have another part of the program written in plain Java that needs to use this Session bean as some kind of model to get all Compounds. 问题是我还有用普通Java编写的程序的另一部分,需要使用此Session bean作为某种模型来获取所有化合物。

Problem is that when I initialize this Session bean somewhere it is outside of persistence context and therefore doesnt know EntityManager to access database. 问题是,当我在某个持久性上下文之外的某个地方初始化此Session Bean时,因此不知道EntityManager来访问数据库。 I believe. 我相信。

I dont know what to do. 我不知道该怎么办。 Can I initialize class ListResource in distant part of code and have Dependency injection of EntityManager working? 我可以在代码的较远部分初始化类ListResource并使EntityManager的依赖项注入正常工作吗? Or somehow to get persistence context and then initialize this session bean? 还是以某种方式获取持久化上下文,然后初始化此会话Bean?

I hope it makes sense. 我希望这是有道理的。 Its complicated problem for me to describe. 我要描述它的复杂问题。

(I cannot write comments yet, so I'll update this answer later) (我尚无法发表评论,因此稍后将更新此答案)

Please describe what you mean by "I have another part of the program written in plain Java". 请描述“我还有用普通Java编写的程序的另一部分”的意思。 Is this a standalone Java program which has a main method? 这是具有主要方法的独立Java程序吗?

Dependency injection will work for "managed classes" like a Servlet, JSF ManagedBean or CDI bean. 依赖注入将适用于“托管类”,例如Servlet,JSF ManagedBean或CDI bean。

You can still call this session bean remotely. 您仍然可以远程调用此会话bean。 What is your environment (Java EE version, app server)? 您的环境是什么(Java EE版本,应用服务器)?

If you have a web service and a standalone app calling the same bean, I would recommend you to move the functionality in a separate stateless bean and create remote and local interfaces to it. 如果您有一个Web服务和一个调用同一个bean的独立应用程序,我建议您将功能移到一个单独的无状态bean中并为其创建远程和本地接口。 This way you can inject local bean into you web service bean, and call the remote one with jndi. 这样,您可以将本地bean注入Web服务bean,并使用jndi调用远程bean。

More about accessing Java EE beans here . 有关在此处访问Java EE bean的更多信息。

Alternatively, your client java code can call the web service to get all the data. 或者,您的客户端Java代码可以调用Web服务以获取所有数据。 Refer to this question about ways to connect to a RESTful service. 有关连接到RESTful服务的方法,请参阅此问题

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

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