如何使用jax-rs子资源定位器处理持久化上下文(EntityManager)? [英] How to handle persistence context (EntityManager) with jax-rs sub-resource locators?

查看:168
本文介绍了如何使用jax-rs子资源定位器处理持久化上下文(EntityManager)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用jax-rs restful web服务和子资源定位器。但是,在将entityManager传递给子资源后,我无法在此子资源中保留任何新对象。

I am using jax-rs restful web service in my application with sub-resource locators. However after passing entityManager to sub-resource I cannot persist any new objects in this sub-resource.

然而,entityManager允许我查询数据。

The entityManager lets me however to query it for data.

这是我的主要资源:

@Path("/registrations")
@Stateless
public class RegistrationsResource {

    @Context
    private UriInfo context;

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

    public RegistrationsResource() {
    }

    //POST method ommited

    @Path("{regKey}")
    public RegistrationResource getRegistrationResource(@PathParam("regKey")
    String regKey) {
        return RegistrationResource.getInstance(regKey, em);
    }

}

这是我的子资源:

public class RegistrationResource {

    private String regKey;
    private EntityManager em;

    private RegistrationResource(String regKey, EntityManager em) {
        this.regKey = regKey;
        this.em = em;
    }

    @Path("securityQuestion")
    @GET
    public String getQuestion() {
        return "iamahuman"+regKey;
    }

    @Path("securityQuestion")
    @POST
    public void postSecurityAnswer(String answer) {
        if(!answer.equals("iamahuman"+regKey)){
            throw new WebApplicationException(Status.BAD_REQUEST);
        }

        //Getting this information works properly
        List<RegistrationEntity> result = em.createNamedQuery("getRegistrationByKey")
            .setParameter("regKey", regKey).getResultList();

        switch(result.size()){
            case 0 :
                throw new WebApplicationException(Status.NOT_FOUND);
            case 1:
                break;
            default:
                throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
            }

            RegistrationEntity reg = result.get(0);
            UserEntity newUser = new UserEntity();

            newUser.setHashedPassword(reg.getPwHash(), reg.getSalt());
            newUser.setUsername(reg.getUsername());
            newUser.setName(reg.getName());
            newUser.setSurname(reg.getSurname());

            //CRASHES HERE
            em.persist(newUser);
    }
}

正如您所看到的,它需要从数据库中注册对象,为注册创建新用户并尝试保留它。但是,em.persist(newUser)会抛出TransactionRequiredException。

As you can see it takes registration object from database, creates new user for registration and tries to persist it. However, em.persist(newUser) throws TransactionRequiredException.

我的问题是:我应该如何将EntityManager传递给子资源,以便它能够正确地保留新对象?

My question is: how should I pass EntityManager to sub-resource so it can properly persist new objects?

推荐答案

很抱歉再次挖掘这个,但我建议如下:

Sorry for digging this up again, but I suggest the following:


  • 将子资源注释为@Stateless EJB

  • 将@EJB注入成员字段放入父资源类中,如下所示:
  • Annotate also the sub-resource as a @Stateless EJB
  • Place @EJB injection member fields into the parent resource class, like so:

    @EJB private RegistrationResource registrationResource;



  • 在getRegistrationResource()中,不要调用子资源的构造函数,而是返回注入的EJB引用:

  • in "getRegistrationResource()", do not call the constructor of the sub-resource, but return the injected EJB reference:

    public RegistrationResource getRegistrationResource() {
        return this.registrationResource;
    }

  • 但是,要使其工作,您不能将@PathParam作为构造函数参数传递。您必须通过@Context或其他@Path声明在子资源中单独访问它。

    这使您能够以与父资源中完全相同的方式在子资源中注入EntityManager ,你不需要传递它。

    For this to work, however, you cannot pass the "@PathParam" as a constructor parameter. You would have to access it seperately in the subresource via the "@Context" or another @Path declaration.
    This enables you to inject the EntityManager in the sub resource in exactly the same way as in the parent resource, you don't need to pass it on.

    这篇关于如何使用jax-rs子资源定位器处理持久化上下文(EntityManager)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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