How to fetch FetchType.LAZY associations with JPA and Hibernate in a Spring Controller

JavaSpringHibernateJpaSpring Data

Java Problem Overview


I have a Person class:

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(fetch = FetchType.LAZY)
    private List<Role> roles;
    // etc
}

With a many-to-many relation that is lazy.

In my controller I have

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonRepository personRepository;

    @RequestMapping("/get")
    public @ResponseBody Person getPerson() {
        Person person = personRepository.findOne(1L);
        return person;
    }
}

And the PersonRepository is just this code, written according to this guide

public interface PersonRepository extends JpaRepository<Person, Long> {
}

However, in this controller I actually need the lazy-data. How can I trigger its loading?

Trying to access it will fail with

> failed to lazily initialize a collection of role: > no.dusken.momus.model.Person.roles, could not initialize proxy - no > Session

or other exceptions depending on what I try.

My xml-description, in case needed.

Thanks.

Java Solutions


Solution 1 - Java

You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size() for this purpose). In Hibernate there is a dedicated method for this (Hibernate.initialize()), but JPA has no equivalent of that. Of course you will have to make sure that the invocation is done, when the session is still available, so annotate your controller method with @Transactional. An alternative is to create an intermediate Service layer between the Controller and the Repository that could expose methods which initialize lazy collections.

Update:

Please note that the above solution is easy, but results in two distinct queries to the database (one for the user, another one for its roles). If you want to achieve better performace add the following method to your Spring Data JPA repository interface:

public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")
    public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);

}

This method will use JPQL's fetch join clause to eagerly load the roles association in a single round-trip to the database, and will therefore mitigate the performance penalty incurred by the two distinct queries in the above solution.

Solution 2 - Java

Though this is an old post, please consider using @NamedEntityGraph (Javax Persistence) and @EntityGraph (Spring Data JPA). The combination works.

Example

@Entity
@Table(name = "Employee", schema = "dbo", catalog = "ARCHO")
@NamedEntityGraph(name = "employeeAuthorities",
			attributeNodes = @NamedAttributeNode("employeeGroups"))
public class EmployeeEntity implements Serializable, UserDetails {
// your props
}

and then the spring repo as below

@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")
public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String>           {

	@EntityGraph(value = "employeeAuthorities", type = EntityGraphType.LOAD)
	EmployeeEntity getByUsername(String userName);

}

Solution 3 - Java

You have some options

  • Write a method on repository that return a initialized entity as R.J suggested.

More work, best performance.

  • Use OpenEntityManagerInViewFilter to keep session open for the entire request.

Less work, usually acceptable in web enviroments.

  • Use a helper class to initialize entities when required.

Less work, useful when OEMIV is not at option, for example in a Swing application, but may be useful too on repository implementations to initialize any entity in one shot.

For the last option, I wrote a utility class, JpaUtils to initilize entities at some deph.

For example:

@Transactional
public class RepositoryHelper {
	
	@PersistenceContext
	private EntityManager em;
	
	public void intialize(Object entity, int depth) {
		JpaUtils.initialize(em, entity, depth);
	}
}

Solution 4 - Java

Spring Data JpaRepository

The Spring Data JpaRepository defines the following two methods:

  • getOne, which returns an entity proxy that is suitable for setting a @ManyToOne or @OneToOne parent association when persisting a child entity.
  • findById, which returns the entity POJO after running the SELECT statement that loads the entity from the associated table

However, in your case, you didn't call either getOne or findById:

Person person = personRepository.findOne(1L);

So, I assume the findOne method is a method you defined in the PersonRepository. However, the findOne method is not very useful in your case. Since you need to fetch the Person along with is roles collection, it's better to use a findOneWithRoles method instead.

Custom Spring Data methods

You can define a PersonRepositoryCustom interface, as follows:

public interface PersonRepository
    extends JpaRepository<Person, Long>, PersonRepositoryCustom { 

}

public interface PersonRepositoryCustom {
    Person findOneWithRoles(Long id);
}

And define its implementation like this:

public class PersonRepositoryImpl implements PersonRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Person findOneWithRoles(Long id)() {
        return entityManager.createQuery("""
            select p 
            from Person p
            left join fetch p.roles
            where p.id = :id 
            """, Person.class)
        .setParameter("id", id)
        .getSingleResult();
    }
}

That's it!

Solution 5 - Java

it can only be lazily loaded whilst within a transaction. So you could access the collection in your repository, which has a transaction - or what I normally do is a get with association, or set fetchmode to eager.

Solution 6 - Java

I think you need OpenSessionInViewFilter to keep your session open during view rendering (but it is not too good practice).

Solution 7 - Java

You can do the same like this:

@Override
public FaqQuestions getFaqQuestionById(Long questionId) {
	session = sessionFactory.openSession();
	tx = session.beginTransaction();
	FaqQuestions faqQuestions = null;
	try {
		faqQuestions = (FaqQuestions) session.get(FaqQuestions.class,
				questionId);
		Hibernate.initialize(faqQuestions.getFaqAnswers());

		tx.commit();
		faqQuestions.getFaqAnswers().size();
	} finally {
		session.close();
	}
	return faqQuestions;
}

Just use faqQuestions.getFaqAnswers().size()nin your controller and you will get the size if lazily intialised list, without fetching the list itself.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatsemannView Question on Stackoverflow
Solution 1 - JavazagyiView Answer on Stackoverflow
Solution 2 - JavarakpanView Answer on Stackoverflow
Solution 3 - JavaJose Luis MartinView Answer on Stackoverflow
Solution 4 - JavaVlad MihalceaView Answer on Stackoverflow
Solution 5 - JavaNimChimpskyView Answer on Stackoverflow
Solution 6 - JavaMichail NikolaevView Answer on Stackoverflow
Solution 7 - Javaneel4softView Answer on Stackoverflow