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.
Though this is an old post, please consider using @NamedEntityGraph (Javax Persistence) and @EntityGraph (Spring Data JPA). The combination works.
Example
and then the spring repo as below
You can do the same like this:
Just use faqQuestions.getFaqAnswers().size()nin your controller and you will get the size if lazily intialised list, without fetching the list itself.
You have some options
More work, best performance.
Less work, usually acceptable in web enviroments.
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:
I think you need OpenSessionInViewFilter to keep your session open during view rendering (but it is not too good practice).
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.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:
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.