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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:29:34+00:00 2026-05-17T18:29:34+00:00

I have a problem with Hibernate and the LazyInitializationException. I searched and find a

  • 0

I have a problem with Hibernate and the LazyInitializationException. I searched and find a lot of answers, but I can not use them to solve my problem, because I have to say, I am new to Hibernate.

I run JUnit tests, in case of the error this one:

@Test
public void testAddPerson() {
    Set<Person> persons = service.getAllPersons();

    // create new person
    Person person = new Person();
    person.setEmail("john@doe.com");
    Project testProject = serviceProj.findProjectById(1);

    HashSet<Project> lister = new HashSet<Project>();
    lister.add(testProject);
    person.setProjects(lister);
    service.addPerson(person);

    testProject.getPersons().add(person);
    ...
}

The last shown line:

testProject.getPersons().add(person);

throws this error:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.doe.john.domain.Project.persons, no session or session was closed

Person and Project are bidirectional n:m:

Person.java:

@ManyToMany(mappedBy="persons")
private Set<Project> projects = new HashSet<Project>();

Project.java:

@ManyToMany
@JoinTable(name = "Project_Person",
    joinColumns = {@JoinColumn(name="project_id", referencedColumnName="id")},
    inverseJoinColumns = {@JoinColumn(name="person_id", referencedColumnName="id")}
)
private Set<Person> persons = new HashSet<Person>();

So what’s the problem?

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

    The problem is that by default the collection is lazily loaded. This means that it wont actually be loaded from the database until it is being accessed. In order to load it you will need a active session/transaction.

    The easy way out is to change it to FethType.EAGER which makes sure that the collection is populated straight away.

    –update–

    I’ve recently had the very same problem and I ended up modifying my actual service to deal with this sort of thing. Declare a addPerson method in your ProjectService class.

    public void addPersonTo(Project project, Person person)
    {
      project = em.merge(project); //EntityManager, not sure what you are using but you get the idea hopefully
      project.addPerson(person);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do not have problem as such but I am quite new to Ruby.
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a problem with Hibernate Native Query. I have one SELECT that selects
This is a real beginner Hibernate problem. I have a problem with the mappings
i am using the following approach to solve lazy initialization problem in hibernate. Please
have a peculiar problem with hibernate. I have a hibernate function like this. @SuppressWarnings(unchecked)
I have a problem with my hibernate mapping using EmbeddedId. My code looks like
i am coming back because i still have problem using JodaTime. After the previous
I have a problem with changing my spring/hibernate application from MySql to SQL Server.
I have a very weird problem: sometimes when I call nHibernate update to an

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.