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

  • Home
  • SEARCH
  • 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 1060129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:16:46+00:00 2026-05-16T18:16:46+00:00

There are two JPA entities: User and Order with one-to-many relationship. /** * User

  • 0

There are two JPA entities: User and Order with one-to-many relationship.

/**
 * User DTO
 */
@Entity
@Table(name="user")
public class User implements Serializable {
    private static final long serialVersionUID = 8372128484215085291L;

    private Long id;
    private Set<Order> orders;

    public User() {}

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceUser")
    public Long getId() {
        return this.id;
    }
    private void setId(Long id) {
        this.id = id;
    }

    @OneToMany(mappedBy="user", cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
    @LazyCollection(LazyCollectionOption.EXTRA)
    public Set<Order> getOrders() {
        return orders;
    }
    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }
 }


/**
 * Order DTO
 */
@Entity
@Table(name="order")
public class Order implements Serializable {
    private static final long serialVersionUID = 84504362507297200L;

    private Long id;
    private User user;

    public Order() {
    }

    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceOrder")
    public Long getId() {
        return this.id;
    }
    private void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name="user_id")
    public User getUser(){
        return user;
    }
    public void setUser(User user){
        this.user = user;
    }
}

I use these entities in my service layer classes where every method runs in transaction. Everything is fine except cases when methods of service layer classes must return these entities.

@Transactional(readOnly=true)
public Set<Order> getOrders() {
    Set<Order> orders = user.getOrders();

    return orders;
}

This method returns data well. But when I try access to received collection elements I catch exception: “org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.User.orders, no session or session was closed”.

So, it was excepted. I thought that detaching result will solve my problem, but trick like this

@Transactional(readOnly=true)
public Set<Order> getOrders() {
    Set<Order> orders = user.getOrders();

    for(Order order: orders)
        entityManager.detach(order);
    return orders;
}

didn’t change anything 🙁

It doesn’t matter for me will info about users attend in set of orders or not. I just want to work with this set and not going to modify it.

Can anybody help me? 🙂

  • 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-16T18:16:47+00:00Added an answer on May 16, 2026 at 6:16 pm

    This method returns data well. But when I try access to received collection elements I catch exception: “org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: package.User.orders, no session or session was closed”.

    The error is self explaining: you are trying to load a (lazy) loaded collection but there is no active session anymore because the User instance is detached.

    So, it was excepted. I thought that detaching result will solve my problem, but trick like this

    This won’t change anything. The EntityManager#detach(Object) method doesn’t load anything, it removes the passed entity from the persistence context, making it detached.

    It doesn’t matter for me will info about users attend in set of orders or not. I just want to work with this set and not going to modify it.

    You need to either make the association EAGER (so that Orders will be loaded when retrieving a User) or to use a FETCH JOIN when retrieving a user in your service:

    SELECT u
    FROM User u LEFT JOIN FETCH u.orders
    WHERE u.id = :id
    

    Do I understand correctly that hibernate has no standard mechanism for force load all lazy associations of current object

    Hibernate has a Hibernate.initialize method, but this is obviously not standard JPA (prefer fetch join for portable code).

    I have no way to make hibernate ignore lazy associations of current object (set them as null)

    What? What do you mean by ignore? Why would you do that anyway?

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

Sidebar

Related Questions

I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I am trying to generate a unidirectional one-to-many mapping between two JPA entities. In
I need to join two JPA entities on a property when there is no
I have two tables: User and Event in a many-to-many relationship. For this I
Consider that there are two entities, Department and Employee, where in one department there
I would like to write a JPA entity class that has a one to
There are two ways to overload operators for a C++ class: Inside class class
There are two types of query parameters binding in the Hibernate Query. One is
There are two tables. One is users info users, one is comments info comments.
I have two entities: User and Event. Each user has a list of events

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.