Announcement Announcement Module
Collapse
No announcement yet.
How to use custom repository methods? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use custom repository methods?

    How can I use custom methods with spring data rest? Example:

    @RestResource(path = "productCategories", rel = "productCategories")
    @Transactional
    public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Long>, ProductCategoryRepositoryCustom {

    }

    public interface ProductCategoryRepositoryCustom {


    public Page<ProductCategory> findAllAvailable(@Param("available") Boolean available, Pageable pageable);

    }

    @Repository
    public class ProductCategoryRepositoryImpl implements ProductCategoryRepositoryCustom {

    @PersistenceContext
    private EntityManager manager;

    @Override
    public Page<ProductCategory> findAllAvailable(Boolean available, Pageable pageable) {
    ...
    }

    }

    The link http://localhost:8080/example/produc...available=true is not working...


    Thanks

  • #2
    Like this:

    @RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
    public interface FooRepository extends PagingAndSortingRepository<Foo, Long> {

    @RestResource(path = "nameLike", rel = "nameLike")
    List<Foo> findByNameLike(@Param(value = "name") String name);

    }

    Then to find all foos with name beginning with "A":

    http://localhost:8080/foos/search/nameLike?name=A%25

    Comment


    • #3
      Originally posted by dusura View Post
      Like this:

      @RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
      public interface FooRepository extends PagingAndSortingRepository<Foo, Long> {

      @RestResource(path = "nameLike", rel = "nameLike")
      List<Foo> findByNameLike(@Param(value = "name") String name);

      }

      Then to find all foos with name beginning with "A":

      http://localhost:8080/foos/search/nameLike?name=A%25
      Hi dusura,

      Using the repository interface (FooRepository) I know it works, but I was trying to use some method of customs classes, for example:

      @RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
      public interface FooRepository extends PagingAndSortingRepository<Foo, Long>, FooRepositoryCustom {

      @RestResource(path = "nameLike", rel = "nameLike")
      List<Foo> findByNameLike(@Param(value = "name") String name);

      }

      public interface FooRepositoryCustom {

      public Foo save2(Foo foo);
      }

      @Repository
      public class FooRepositoryImpl implements FooRepositoryCustom {

      @PersistenceContext
      private EntityManager manager;

      @Override
      public Foo save2(Foo foo){
      foo.setSomething();
      //do something
      manager.persist(foo);
      return manager;
      }

      }

      How can i invoke the method save2 with REST?

      Comment


      • jAddict
        jAddict commented
        Editing a comment
        If you find out the solution, please share. I think with spring data rest we cant use custom repository.
    Working...
    X