Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7631493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:22:04+00:00 2026-05-31T06:22:04+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:22:05+00:00Added an answer on May 31, 2026 at 6:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's say I'm trying to get a list of all my users who
Let's say we have the following html: <ul id=portfolio-list class=clearfix span-24> <li class=2012#foo bar
Let's say we have a List<Product> and each product item in the list has
I'm trying to do some async stuff in a webservice method. Let say I
Let's say i have a Domain Model that im trying to make compatible with
I am trying to animate an object, let's say its a car. I want
I'm trying to foresee how big my database will be. Let's say I have
I'm trying to add two folders to my eclipse project's classpath, let's say Folder
Trying to avoid the SomethingManager trap here... Let's say I'm going to write a
I have a 2D bitmap-like array of let's say 500*500 values. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.