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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:22:34+00:00 2026-06-08T18:22:34+00:00

I am getting org.hibernate.LazyInitializationException: illegal access to loading collection in my JPA code –

  • 0

I am getting org.hibernate.LazyInitializationException: illegal access to loading collection in my JPA code – all collections are EAGER fetch – when the collection entity also has a collection.

Could somebody please help me to fix this?

I have isolated a problem in my JPA code to the following @Entity definitions:

(note, I’m skipping the package and import statements to shorten the code. Some Lombok annotations are used, such as @Data to mean that the field has a getter/setter and @Cleanup to do the usual try/catch close() dance)

@Entity
@Data
public class MyEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

//    @ElementCollection(fetch = FetchType.EAGER)
//    private Set<String> tags = Sets.newTreeSet();
}

@Entity
@Data
public class MyOtherEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToMany(fetch = FetchType.EAGER)
    private Set<MyEntity> entities = Sets.newHashSet();
}

(I also get the same problem if I explicitly do a full @JoinTable, but Hibernate seems to generate everything fine without it – I’m happy leaving it out).

The problem is that if I uncomment the “tags” field in @MyEntity, then I always get the following PersistenceException

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.LazyInitializationException: illegal access to loading collection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1377)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:828)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:781)

The following is a short application which exemplifies the problem:

public class JpaQuestion {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.put("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver");
        properties.put("hibernate.connection.url", "jdbc:derby:playground;create=true");
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PlaygroundPU", properties);

        populate(emf);

        @Cleanup("close") EntityManager em = emf.createEntityManager();
        MyOtherEntity other = em.find(MyOtherEntity.class, 1L);
        System.out.println(other != null ? other.toString() : "null");
    }

    public static void populate(EntityManagerFactory emf) {
        @Cleanup("close") EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        MyEntity a = new MyEntity();
        em.persist(a);
        MyOtherEntity other = new MyOtherEntity();
        other.getEntities().add(a);
        em.persist(other);
        em.getTransaction().commit();
    }
}

UPDATE: I know about LazyInitializationException when field is eager but that seems to be because load() grabs a lazy version of the entity. I’m using “find” here. I’ve noticed that if I issue a JPA query (instead of find), then this problem goes away.

UPDATE: This really does work fine if instead of find(), I use a Query like "SELECT b FROM MyOtherEntity b WHERE b.id = :id". Maybe find() really does ignore EAGER loading! Hence this is likely a bug in Hibernate.

UPDATE: I’ve logged this as a bug report with Hibernate at https://hibernate.onjira.com/browse/HHH-7476

  • 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-06-08T18:22:36+00:00Added an answer on June 8, 2026 at 6:22 pm

    First of all it is useful to reference the full stack trace: http://pastie.org/4358203

    The problem is being caused by your call to tags.hashCode() within the hashCode() implementation of MyEntity.

    When you use Hibernate to load the MyOtherEntity instance, it’s collection of MyEntity is initialised. When a MyEntity is added to the Set implementation within MyOtherEntity, it’s hashCode() method is naturally called (sets cannot contain duplicates remember and hashCode() is part of how Java checks for object equality). The hashCode() method of MyEntity then attempts to invoke hashCode() on the tags collection. The tags collection is in the process of being initialised, which leads you to this error.

    I think it is worth thinking about your hashCode() implementation for MyEntity. Does your use-case really require you to compare the value of the all elements within the tags collection to ensure object equality?

    For more information on managing object equality in Hibernate, the following is a useful resource:

    https://community.jboss.org/wiki/EqualsAndHashCode

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

Sidebar

Related Questions

I'm trying to use HibernateDaoSupport but I'm getting stuck with a org.hibernate.LazyInitializationException problem. This
Hello all I'm getting this very strange error: java.lang.NoSuchMethodError: org.hibernate.SessionFactory.getCurrentSession()Lor g/hibernate/classic/Session; at org.cometd.hibernate.util.HibernateUtil.getSessionFactory(HibernateUt il.java:29)
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: pojo.Person.address, no session or session
Error: org.hibernate.LazyInitializationException SEVERE: could not initialize proxy - the owning Session was closed Code:
I keep on getting this error Exception in thread main org.hibernate.LazyInitializationException: could not initialize
I'm getting this Hibernate error: org.hibernate.MappingException: Could not determine type for: a.b.c.Results$BusinessDate, for columns:
I am getting the following exception while adding data into database: org.hibernate.HibernateException: The database
I'm trying to get http://www.gelens.org/code/gevent-websocket/ running and keep getting the following error. socket_id=1 already
I am using Hibernate and getting Exception in thread main org.hibernate.ObjectNotFoundException: No row with
Im getting following error while running the query. org.hibernate.hql.ast.QuerySyntaxException: expecting CLOSE, found 'LIMIT' near

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.