Giter Site home page Giter Site logo

seightday / spring-data-jpa-entity-graph Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cosium/spring-data-jpa-entity-graph

0.0 2.0 0.0 150 KB

Spring Data JPA extension allowing full dynamic usage of EntityGraph on repositories. https://cosium.github.io/spring-data-jpa-entity-graph

License: MIT License

Shell 0.15% Java 99.85%

spring-data-jpa-entity-graph's Introduction

Travis branch Codecov branch Maven Central

Spring Data JPA EntityGraph

Today, Spring Data JPA supports EntityGraph exlusively through annotations.
Thus, for a method, the choice of EntityGraph must be made before compilation.

This extension gives the ability to pass EntityGraph on any Spring Data JPA repository method as an argument, making the EntityGraph choice fully dynamic.

Example:

productRepository.findByName("foo", EntityGraphUtils.fromName("Product.brand"));

Compatibility

This library follows the Spring Data JPA versionning semantic.

spring-data-jpa-entity-graph spring-data-jpa
1.11.x 1.11.y
1.10.x 1.10.y

Quick start

  1. In addition to spring-data-jpa, add the library dependency :

    <dependency>
        <groupId>com.cosium.spring.data</groupId>
        <artifactId>spring-data-jpa-entity-graph</artifactId>
        <version>1.10.13</version>
    </dependency>
  2. In your Spring configuration, set the repository factory bean class to JpaEntityGraphRepositoryFactoryBean :

    @Configuration
    @EnableJpaRepositories(repositoryFactoryBeanClass = JpaEntityGraphRepositoryFactoryBean.class)
    public class DataRepositoryConfiguration {
        ...
    }
  3. Make sure your repositories extend JpaEntityGraphRepository, JpaEntityGraphSpecificationExecutor and/or JpaEntityGraphQueryDslPredicateExecutor

## Basic Usage

Let's consider the following entities and repository :

@Entity
public class Brand {
    @Id
    private long id = 0;
    private String name;
    //...
}
@NamedEntityGraphs(value = {
    @NamedEntityGraph(name = "Product.brand", attributeNodes = {
        @NamedAttributeNode("brand")
    })
})
@Entity
public class Product {
    @Id
    private long id = 0;
    private String name;
    @ManyToOne(fetch = FetchType.LAZY)
    private Brand brand;
    //...
}	
public interface ProductRepository extends JpaEntityGraphRepository<Product, Long> {
    List<Product> findByName(String name, EntityGraph entityGraph);
}

You can pass the entity graph to the findByName method :

// This will apply 'Product.brand' named EntityGraph to findByName
productRepository.findByName("MyProduct", EntityGraphUtils.fromName("Product.brand"));

Or to the findOne method :

// This will apply 'Product.brand' named EntityGraph to findOne
productRepository.findOne(1L, EntityGraphUtils.fromName("Product.brand"));

Or any method you like.

You can also pass a dynamically built EntityGraph by using DynamicEntityGraph implementation.

Default EntityGraph

For an Entity, you can define its default EntityGraph.
An Entity default EntityGraph will be used each time the Entity repository method is called without EntityGraph.

A default EntityGraph name must end with .default.

@NamedEntityGraphs(value = {
    @NamedEntityGraph(name = "Product.default", attributeNodes = {
        @NamedAttributeNode("brand")
    })
})
@Entity
public class Product {
    @Id
    private long id = 0;
    private String name;
    @ManyToOne(fetch = FetchType.LAZY)
    private Brand brand;
    //...
}	
// This call will make use of "Product.default" EntityGraph.
productRepository.findOne(1L);

spring-data-jpa-entity-graph's People

Contributors

reda-alaoui avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.