I am using jax-rs restful web service in my application with subresource locators. However, after passing the entityManager to a sub-resource, I cannot save any new objects in this sub-resource.
EntityManager allows me, however, to query it for data.
This is my main resource:
@Path("/registrations") @Stateless public class RegistrationsResource { @Context private UriInfo context; @PersistenceContext(unitName="pctx") private EntityManager em; public RegistrationsResource() { }
}
And this is my sub-resource:
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); }
As you can see, it takes the registration object from the database, creates a new user for registration and tries to save it. However, em.persist (newUser) throws a TransactionRequiredException.
My question is: how do I pass the EntityManager to a sub-resource so that it can correctly save new objects?
Mikom source share