Skip to content

Latest commit

 

History

History
115 lines (101 loc) · 3.52 KB

File metadata and controls

115 lines (101 loc) · 3.52 KB

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.11.03</version>
    </dependency>
  2. In your Spring configuration, set the repository factory bean class to EntityGraphJpaRepositoryFactoryBean :

    @Configuration
    @EnableJpaRepositories(repositoryFactoryBeanClass = EntityGraphJpaRepositoryFactoryBean.class)
    public class DataRepositoryConfiguration {
        //...
    }
  3. Make sure your repositories extend the Spring Data standard ones or the library provided repositories:

  • EntityGraphJpaRepository which is equivalent to standard JpaRepository
  • EntityGraphJpaSpecificationExecutor which is equivalent to standard JpaSpecificationExecutor
  • EntityGraphQueryDslPredicateExecutor which is equivalent to standard QueryDslPredicateExecutor
  • EntityGraphCrudRepository which is equivalent to standard CrudRepository
  • EntityGraphPagingAndSortingRepository which is equivalent to standard PagingAndSortingRepository
  • EntityGraphQueryByExampleExecutor which is equivalent to standard QueryByExampleExecutor

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 EntityGraphJpaRepository<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, it's also accessible through a helper method:

productRepository.findOne(1L, EntityGraphUtils.fromAttributePaths("brand", "maker"));

This is similar to Spring's ad-hoc attribute paths, and equivalent to writing this in your repository's interface:

@EntityGraph(attributePaths = { "brand", "maker" })
Product findOne(Long id);

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);