Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot extending CrudRepository

I'm using Hibernate in a Spring Boot app. I'm making a new CrudRepository for all my Model objects, to do basic CRUD tasks. They look like this:

@Repository
public interface FoobarCrudRepo extends CrudRepository<Foobar, Long> {
}

But then I always need to do some additional things, like custom search queries with inequalities and such. I follow a pattern like this:

@Repository
public class FoobarDao {

    @PersistenceContext
    EntityManager em;

    public List<Foobar> findFoobarsByDate(Date date) {
        String sql = "select fb from Foobar fb where createdDate > :date";
        ...
        return query.getResultList();
    }
}

My question is, can I combine these two concepts into a single class? I tried making it an abstract class, like so:

@Repository
public abstract class FoobarCrudRepo extends CrudRepository<Foobar, Long> {

    @PersistenceContext
    EntityManager em;

    public List<Foobar> findFoobarsByDate(Date date) {
        String sql = "select fb from Foobar fb where createdDate > :date";
        ...
        return query.getResultList();
    }

}

But then Spring didn't create a bean for it.

How can I accomplish this?

Thanks!

like image 559
James Watkins Avatar asked Jun 17 '15 01:06

James Watkins


People also ask

Does JPA repository extend CrudRepository?

It extends both CrudRepository and PagingAndSortingRepository. To perform CRUD operations, define repository extending CrudRepository. To perform CRUD as well as batch operations, define repository extends JpaRepository.

When you extend the CrudRepository in your repo What functionality are you giving your project?

extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction. This provides two methods. Page findAll(Pageable pageable) – returns a Page of entities meeting the paging restriction provided in the Pageable object.

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is Spring boot CrudRepository?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.


2 Answers

There are lots of ways you could probably accomplish this. If you really need absolute control try this

interface FoobarRepositoryCustom{
    List<Foobar> findFoobarsByDate(Date date);
}

interface FoobarRepository extends CrudRepository<Foobar, Long>, FoobarRepositoryCustom

public class FoobarRespoitoryImpl implements FoobarRepositoryCustom{
    @PersistenceContext private EntityManager em;


    public List<Foobar> findFoobarsByDate(Date date) {
    String sql = "select fb from Foobar fb where createdDate > :date";
    ...
    return query.getResultList();
    }
}

There is also the possibility to go a simpler route and the query can be auto generated for you based on the method name. In your example you could just add this to your FoobarCrudRepo and Spring should do the rest assuming Foobar has a property named CreatedDate

List<Foobar> findByCreatedDateGreaterThan(Date date);

For reference on how Spring can generate queries based on the method name see this http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

like image 183
jst Avatar answered Oct 09 '22 22:10

jst


Completely new to Spring Data, but having searched a bit it is my impression that you do not have to leave the interface to create custom logic - rather you would create either an annotated interface method, an interface method that follows a special naming scheme or a default interface method with custom logic:

enter image description here Screenshot from Baeldung: Introduction to Spring.

Here is a link to the documentation. Notice "table 4. Supported keywords inside method names" which can be used to create interface methods, whose name conveys information to the code generator about which query to create (See part of table below).

enter image description here

like image 30
Hervian Avatar answered Oct 09 '22 22:10

Hervian