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

218,649

Solution 1

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

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

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

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

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.

Share:
218,649

Related videos on Youtube

Matsemann
Author by

Matsemann

Master of Technology: Computer Science at NTNU in Norway. Work for BEKK in Oslo.

Updated on May 13, 2020

Comments

  • Matsemann
    Matsemann almost 4 years

    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.

    • Rahul
      Rahul about 11 years
      Can you write a method, which will create a query to fetch a Person object given some parameter? In that Query, include the fetch clause and load the Roles too for the person.
  • Matsemann
    Matsemann about 11 years
    Since all my requests are simple REST calls with no rendering etc., the transaction is basically my whole request. Thanks for your input.
  • zagyi
    zagyi about 11 years
    Please note that this is an easy solution, 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, try writing a dedicated method that eagerly fetches the user and its associated roles in a single step using JPQL or the Criteria API as others suggested.
  • Matsemann
    Matsemann about 11 years
    How do I do the first one? I know how to write a query, but not how to do what you say. Could you please show an example? Would be very helpful.
  • Matsemann
    Matsemann about 11 years
    I now asked for an example to Jose's answer, have to admit I don't understand entirely.
  • zagyi
    zagyi about 11 years
    Please check a possible solution for the desired query method in my updated answer.
  • Matsemann
    Matsemann about 11 years
    zagyi provided an example in his answer, thanks for pointing me in the right direction anyway, though.
  • Matsemann
    Matsemann about 11 years
    As I'm not using JSP or anything, just making a REST-api, @Transactional will do for me. But will be useful at other times. Thanks.
  • FGreg
    FGreg over 10 years
    Interesting thing to note, if you simply join without the fetch, the set will be returned with initialized = false; therefore still issuing a second query once the set is accessed. fetch is key to making sure the relationship is completely loaded and avoiding the second query.
  • Vishwas Shashidhar
    Vishwas Shashidhar almost 10 years
    @Matsemann I know it's late now...but you can make use of OpenSessionInViewFilter even in a controller as well as the session will exist till a response is compiled...
  • desperateCoder
    desperateCoder over 9 years
    @Matsemann Thanks! Transactional-annotation did the trick for me! fyi: It even works if you just annotate the superclass of a rest-class.
  • K.Nicholas
    K.Nicholas over 9 years
    Seems that the problem with doing both and fetch and a join is that join predicate criteria is ignored and you end up getting everything in the list or map. If you want everything, then use a fetch, if you want something specific, then a join, but the, as was said, the join will be empty. This defeats the purpose of using .LAZY loading.
  • Shady Mohamed Sherif
    Shady Mohamed Sherif over 9 years
    I don't know how your class would be called! not completed solution waste others time
  • Shady Mohamed Sherif
    Shady Mohamed Sherif over 9 years
    Really thanks, I had the same problem and didn't imagine that I could annotate my controller as @Transactional
  • naXa stands with Ukraine
    naXa stands with Ukraine almost 9 years
    Note that @NamedEntityGraph is a part of JPA 2.1 API, which is not implemented in Hibernate before 4.3.0 version.
  • Yan Khonski
    Yan Khonski over 7 years
    Use OpenEntityManagerInViewFilter to keep session open for the entire request.- Bad idea. I would make an additional request to fetch all collections for my entities.
  • TomR
    TomR almost 7 years
    Code update with JOIN FETCH is nonsense (the repsonse is correct but the JPA API/JPQL design is nonsens in this case) from the OO view of point. E.g. assume that we have person Joe with 5 roles. Then this JOIN FETCH retrieves 5 Joe entities? Complete nonsense. Expected behavior is that this code should retrieve one Joe entity with initializes roles array which has 5 role entities. I have tested this code and it retrievied 5 Joe entities indeed and therefore I can not use it.
  • Marek Urbanowicz
    Marek Urbanowicz almost 7 years
    Could you help to adjust it to findByEmail ? Can't figure it out how to get it right
  • tryingHard
    tryingHard about 5 years
    findByIdAndFetchRolesEagerly solution works great. @MU for mails try sth like: @Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.email = (:email)") public Person findByEmailAndFetchRolesEagerly(@Param("email") String email);
  • Desislav Kamenov
    Desislav Kamenov over 4 years
    @EntityGraph(attributePaths = "employeeGroups") can be used directly in a Spring Data Repository to annotate a method without the need of a @NamedEntityGraph on your @Entity - less code, easy to understand when you open the repo.
  • Jeroen Vandevelde
    Jeroen Vandevelde almost 4 years
    Is there a reason why you wrote the query yourself and didn’t use a solution like the EntityGraph in @rakpan answer? Wouldn’t this produce the same result?
  • Vlad Mihalcea
    Vlad Mihalcea almost 4 years
    The overhead for using an EntityGraph is higher than a JPQL query. On the long run, you are better off writing the query.
  • Jeroen Vandevelde
    Jeroen Vandevelde almost 4 years
    Can you elaborate on the overhead (Where does it come from, is it noticeable, ...)? Cause I don’t understand why there is a higher overhead if they both generate the same query.
  • Vlad Mihalcea
    Vlad Mihalcea almost 4 years
    Because EntityGraphs plans are not cached like JPQL are. That can be a significant performance hit.
  • Jeroen Vandevelde
    Jeroen Vandevelde almost 4 years
    Just to be sure i understand you correctly. Are you talking about Hibernates QueryPlanCache mentioned in this great article? vladmihalcea.com/hibernate-query-plan-cache
  • Vlad Mihalcea
    Vlad Mihalcea almost 4 years
    Exactly. I'll have to write an article about it when I have some time.
  • DirtyMind
    DirtyMind almost 3 years
    Hi @vlad... In the same scenario if I fetch user using rest api in spring boot. It will fetch user as well as roles details? In that case I have to use jsonIgnore right? Will your query work in that case as well? If I use jsonignore
  • Vlad Mihalcea
    Vlad Mihalcea almost 3 years
    JsonIgnore is needed for bidirectional associations in the web layer, so it won't affect the way you're querying data in the data access layer.
  • Kaj Hejer
    Kaj Hejer almost 3 years
    @Vlad Will you get caching of the querty like you describe in your comment above if you use a @Query annotation like in one of the amswers above (stackoverflow.com/a/15360333/924036)?