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

  • rodrigo.aiello
    started a topic How to use custom repository methods?

    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

  • jAddict
    commented on 's reply
    If you find out the solution, please share. I think with spring data rest we cant use custom repository.

  • rodrigo.aiello
    replied
    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?

    Leave a comment:


  • dusura
    replied
    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

    Leave a comment:

Working...
X