Following some of the recommendations in How to correctly use PagedResourcesAssembler from Spring Data? I've created a controller to handle some custom query requests for an entity. The classes look like:
@BasePathAwareController
public class FooController implements ResourceProcessor<RepositorySearchesResource> {
@Autowired
FooRepo repo;
@RequestMapping("/foo/search/findFooByAttribute")
public ResponseEntity<PagedResources<Resource<Foo>>> findByAttribute(
PersistentEntityResourceAssembler entityAssembler) {
Page<Foo> results = repo.findAll(
Specifications.where(FooSpec.isOpen()),
new PageRequest(1, 20));
return new ResponseEntity<PagedResources<Resource<Foo>>>(
resourceAssembler.toResource(results),
HttpStatus.OK);
}
@Override
public RepositorySearchesResource process(
RepositorySearchesResource resource) {
...
}
}
where FooRepo
is a repository interface annotated with @RestResource
. If do a GET to /foo
(the default findAll) the endpoint is able to retrieve a @OneToMany
collection mapping on the Foo entity during the serialization process. But in my custom repository, I get the famous could not initialize proxy - no Session
exception. How can I make my custom controller behave in the same way as the generated controller that @RestResource
seems to create with regards to keeping the Hibernate session open?