Yavin
Yavin

Reputation: 455

Custom method in hibernate repository

I want to add custom method to hibernate CrudRepository. I'm using it with spring framework. Documentation said that it is posible, but example show interface not extending Repository base class.

I know that there is an annotation @Query that can define custom query, but what if i want to add for example Criteria query to that class? Also i want methods like findAll to stay in repository.

This is what i try. First i created repository for Post entity:

package foo.bar.repository;

import foo.bar.model.Post;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long> {

}

Then i created abstract implementation class that extends interface and add my custom method to it:

package foo.bar.repository;

public abstract class PostRepositoryImpl implements PostRepository {

    public List<Post> someCustomMethod() {
        //some logic here
    }

}

It is even posible to do that? Maybe there is other approach to do that in hibernate. I was using Doctrine for long time, which is based on hibernate. There you just extend base repository class and add methods that you want.

Upvotes: 3

Views: 3110

Answers (1)

Gabriel Ruiu
Gabriel Ruiu

Reputation: 2803

I'm going to add an example of a custom method that executes a custom query. I hope I understood what you meant with your question.

PersonRepository

public interface PostRepository extends CrudRepository<Post, Long>{
    List<Post> getPostsOlderThanDate(Date date);
}

PersonRepositoryImpl

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.List;

public class PostRepositoryImpl extends SimpleJpaRepository<Post, Long> implements PostRepository {

    private EntityManager entityManager;

    public PostRepositoryImpl(EntityManager em) {
        super(Post.class, em);
        this.entityManager = em;
    }

    @Override
    public List<Post> getPostsOlderThanDate(Date date) {
        String query = ""; //create query
        TypedQuery<Post> typedQuery = entityManager.createQuery(query, Post.class);
        return typedQuery.getResultList();
    }
}

You then create your EntityManagerFactory bean (which you build with a Hibernate persistance provider, or however you set it up, it's up to you), and inject it into the constructor of the PersonRepositoryImpl bean.

<bean id="personRepository" class="com.package.PersonRepositoryImpl">
    <constructor-arg ref="entityManagerFactory" />
</bean>

These would be the steps to provide some custom queries into your database.

NOTE: if you need help setting up your EntityManager just leave a comment addressed to me and I'll give you the spring config and dependencies.

Note, the suffix "Impl" is important. Or it can be tweaked. Find "repository-impl-postfix" at this article : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances.spring for more details.

repository-impl-postfix

Defines the postfix to autodetect custom repository implementations. Classes whose names end with the configured postfix are considered as candidates. Defaults to Impl.

Upvotes: 4

Related Questions