1

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?

Community
  • 1
  • 1
Chris Cooper
  • 869
  • 2
  • 18
  • 42
  • Have you tried to annotate your controller with RepositoryRestController instead of BasePathAwareController – Mathias Dpunkt Jun 28 '16 at 13:29
  • Instead of returning `ResponseEntity>>`, have you tried returning `Resources` by using `Resources.wrap()` to generate the resources from your entities? Also available for `PagedResources` – Will Faithfull Jul 04 '16 at 18:20

0 Answers0