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.
Person
object given some parameter? In thatQuery
, include thefetch
clause and load theRoles
too for the person. – Detach