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?

2
  • Have you tried to annotate your controller with RepositoryRestController instead of BasePathAwareController Commented Jun 28, 2016 at 13:29
  • Instead of returning ResponseEntity<PagedResources<Resource<T>>>, have you tried returning Resources<T> by using Resources.wrap() to generate the resources from your entities? Also available for PagedResources<T> Commented Jul 4, 2016 at 18:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.