SLYAK

spring-data-jpa-extra

use spring data jpa more comfortable

I love spring-data-jpa, she set my hands free, crud methods are boring! However she is not perfect on dynamic native query and her return type must be an entity, although she provide us a specification solution, but i think it's heavy and not easy to use.

spring-data-jpa-extra comes to solve three problem:

Example

by java bean

@ComponentScan({"com.slyak","your.base.package"})

by xml

<context:component-scan base-package="com.slyak,your.base.package"/>
    public interface SampleRepository extends GenericJpaRepository<Sample, Long> {
        @TemplateQuery
        Page<Sample> findByContent(@Param("content")String content, Pageable pageable);
        @TemplateQuery
        CustomVO findCustomVO(@Param("id")Long id);
    }
SLYAK SLYAK
--findByContent
SELECT * FROM t_sample WHERE 1=1
<#if content??>
AND content LIKE :content
</#if>

--findCustomVO
SELECT id,name as viewName FROM t_sample WHERE id=:id

How to use?

you can use it by using source code or adding a maven dependency

notice: if the spring-data-jpa version >= 1.11.x (spring-boot version >= 1.5.x) please use 2.1.x.RELEASE .

notice: if you are using spring-boot 2.0+ or spring5 , please use the latest version 3.0.0.RELEASE

spring version spring-data-jpa-extra version
spring 3 (spring-boot 1.5.x) 2.1.x.RELEASE
spring 4 (spring-boot 2.0.x) 3.0.0.RELEASE
    <dependency>
        <groupId>com.slyak</groupId>
        <artifactId>spring-data-jpa-extra</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

config with annotation

@EnableJpaRepositories(basePackages = "your.packages", repositoryBaseClass = GenericJpaRepositoryImpl.class, repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class)

or with xml

<jpa:repositories base-package="your.packages" repository-base-class="com.slyak.spring.jpa.GenericJpaRepositoryImpl" repository-factory-bean-class="com.slyak.spring.jpa.GenericJpaRepositoryFactoryBean"/>

Notice: if you are using spring-boot, the springboot-starter-jpa-extra should be the best choice!

2 Minute Tutorial

Template Query

Methods annotated with @TemplateQuery tells QueryLookupStrategy to look up query by content,this is often used by dynamic query.

Template Query Object

Object annotated with @TemplateQueryObject tells content process engine render params provided by object properties.

Entity Assemblers

Entity assemblers can assembler entity with other entities, such as one to many relation or one to one relation.

More Useful Methods (eg: mget togglestatus fakedelete)

    //batch get items and put the result into a map
    Map<ID, T> mget(Collection<ID> ids);

    //get items one by one for cache
    Map<ID, T> mgetOneByOne(Collection<ID> ids);

    //get items one by one for cache
    List<T> findAllOneByOne(Collection<ID> ids);

    //toggle entity status if it has a Status property
    void toggleStatus(ID id);

    //set entity status to Status.DELETED if it has a Status property
    void fakeDelete(ID... id);

TODO List

Change Log

CHANGELOG

How to use in old version ( <2.0 )

old version guide