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 5944737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:34:54+00:00 2026-05-22T16:34:54+00:00

@Entity public class User { @ManyToMany(cascade = { MERGE, PERSIST, CascadeType.REFRESH }, fetch =

  • 0
@Entity 
public class User {

    @ManyToMany(cascade = { MERGE, PERSIST, CascadeType.REFRESH }, fetch = LAZY)
    @Cache(usage = READ_WRITE)
    @UserRoles
    private List<Role> roles = new ArrayList<Role>(0);

}

Collections get loaded even when they are declared as Lazy, what could be the reason that it is getting loaded even when it is declared as Lazy

  • 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-22T16:34:55+00:00Added an answer on May 22, 2026 at 4:34 pm

    According to the JPA 2.0 specification lazy fetching is a hint to the persistence provider.

    Section 11.1.6 says:

    The EAGER strategy is a requirement on
    the persistence provider runtime that
    data must be eagerly fetched. The LAZY
    strategy is a hint to the persistence
    provider runtime that data should be
    fetched lazily when it is first
    accessed.The implementation is
    permitted to eagerly fetch data for
    which the LAZY strategy hint has been
    specified.

    There are cases in which the provider might determine that it makes no sense to lazily fetch something, or perhaps a provider may not even implement lazy fetching.

    The PersistenceUtil.isLoaded methods can be used to determine the load state of an entity
    and its attributes regardless of the persistence unit with which the entity is associated.

    To be sure you are facing this kind of problem you should use this method. Of course, if a entity got detached, a simple inspection of the lazily fetched field could help as well.

    Other than this, I cannot see a particular reason, based on the code posted of why your collections is not being lazily fetched. I think that perhaps the fact that you are using 2nd level cache could be affecting in this case, but this is something I have not verified, it is just a hunch.

    [EDIT-1]

    There is also an issue with lazy loading if you are using final classes. The Hibernate documentation says under persistent classes:

    A central feature of Hibernate,
    proxies, depends upon the persistent
    class being either non-final, or the
    implementation of an interface that
    declares all public methods.

    You can persist final classes that do
    not implement an interface with
    Hibernate. You will not, however, be
    able to use proxies for lazy
    association fetching which will
    ultimately limit your options for
    performance tuning.

    You should also avoid declaring public
    final methods on the non-final
    classes. If you want to use a class
    with a public final method, you must
    explicitly disable proxying by setting
    lazy=”false”.

    [EDIT-2]

    Besides this the JPA 2.0 Specification in section 2.1 specifies that entities must not be final:

    The entity class must not be final. No
    methods or persistent instance
    variables of the entity class may be
    final

    This same requirement is later specified for embeddables in section 2.5.

    I do not have access to the JPA 1.0 specification, but I am quite confident that this requirement applies to it as well. Try it and see if this is the reason and let us know.

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

Sidebar

Related Questions

These are my entities @Entity public class User { @ManyToMany(mappedBy=admins) private Set<Game> adminForGames =
I have this 2 classes: @Entity public class Student extends User { ... @ManyToMany(mappedBy
Let's say there is a entity class like this @Entity public class User {
I have an entity that has a ManyToMany relationship with itself: @Entity public class
Seu the following mapping @Entity public class User { private Integer id; @Id; private
Suppose a mapping like this one @Entity public class User { private Integer id
Assume I have the following two classes: public class User : Entity { public
I have the following java class: package domain; //imports @Entity public class User {
I have the following @OneToOne relation: @Entity @Table(name=USER) public class User implements Serializable{ private
I have 2 models. User: @Entity public class User implements Serializable { Long id;

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.