Question

Let us say I am trying to list all products using a restful interface.

    @XmlRootElement
    public class Product implements Serializable {

    //products attributes defined here

    @OneToMany(cascade = CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="product")
    private Set<ProductUser> productUsers= new HashSet<ProductUser>();
}

When I try to call find.All on products, it is trying to fetch even ProductUsers and throwing an exception

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role, productUsers, no session or session was closed

I don't want to include the product users in my rest xml output. How do I selectively choose what gets included in the xml?

I am using REST-Easy, JBoss AS7, JPA with Hibernate as the implementation. My EJB 3.1 methods are exposed as Restful services.

Appreciate your help!

Was it helpful?

Solution

I would not suggest to mix JAXB and JPA in one class.

JPA Entities have there own special lifecycle. If you return a JPA Entity from a public EJB method it is detached in most cases. I assume that your REST Resource classes are EJBs. The JPA Entity is detached because, per default, your transaction will start and end with a call to a public EJB method. For more information look at the JavaDoc for TransactionAttribute. If a JPA Entity is detached, lazy loading will no longer work. That is your problem.

I would suggest to have separate classes for JPA Entities and JAXB Entities. Furthermore I would suggest to never return a JPA Entity out of its transaction context. So you will never have to deal with detaching/merging of JPA Entities. All your lazy loading problems would be history. To achieve this, you have to manually convert your JPA Entity into your JAXB Entity inside the transaction (inside the public EJB method) and return the JAXB entity.

Your GET method could look like this:

@GET
@Produces("application/xml")
public JaxbProduct get(@PathParam("id") int id) {
    JpaProduct product = entityManager.find(JpaProduct.class, id);
    JaxbProduct result = new JaxbProduct();
    result.setPropertyX(product.getPropertyX());
    return result;
}

By manually converting your JPA entity into the JAXB entity you have full control about what gets into your output and what not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top