Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ productRepository.findOne(1L, EntityGraphUtils.fromName("Product.brand"));

Or any method you like.

You can also pass a dynamically built EntityGraph by using `DynamicEntityGraph` implementation.
You can also pass a dynamically built EntityGraph by using `DynamicEntityGraph`, it's also accessible through a helper method:

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

This is similar to [Spring's ad-hoc attribute paths](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions),
and equivalent to writing this in your repository's interface:
```java
@EntityGraph(attributePaths = { "brand", "maker" })
Product findOne(Long id);
```

## Default EntityGraph

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.cosium.spring.data.jpa.entity.graph.domain;

import com.mysema.commons.lang.Assert;

import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -38,6 +41,15 @@ public static EntityGraph fromName(String name, boolean optional){
return namedEntityGraph;
}

/**
* @param attributePaths The attribute paths to be present in the result
* @return A {@link DynamicEntityGraph} with the path attributes passed in as arguments.
*/
public static EntityGraph fromAttributePaths(String... attributePaths) {
Assert.notEmpty(attributePaths, "At least one attribute path is required.");
return new DynamicEntityGraph(Arrays.asList(attributePaths));
}

private static final class EmptyEntityGraph implements EntityGraph {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

import com.cosium.spring.data.jpa.entity.graph.BaseTest;
import com.cosium.spring.data.jpa.entity.graph.domain.EntityGraph;
import com.cosium.spring.data.jpa.entity.graph.domain.EntityGraphUtils;
import com.cosium.spring.data.jpa.entity.graph.repository.exception.InapplicableEntityGraphException;
import com.cosium.spring.data.jpa.entity.graph.repository.sample.*;
Expand Down Expand Up @@ -68,6 +69,25 @@ public void given_optional_brand_eg_when_findone_then_brand_should_be_loaded(){
assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();
}

@Transactional
@Test
public void given_brand_in_attribute_paths_eg_when_findone_then_brand_should_be_loaded() {
Product product = productRepository.findOne(1L, EntityGraphUtils.fromAttributePaths(Product.BRAND_PROP_NAME));
assertThat(product).isNotNull();
assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();
}

@Transactional
@Test
public void given_brand_and_maker_in_attribute_paths_eg_when_findone_then_brand_and_maker_should_be_loaded() {
EntityGraph entityGraph = EntityGraphUtils.fromAttributePaths(Product.BRAND_PROP_NAME, Product.MAKER_PROP_NAME);
Product product = productRepository.findOne(1L, entityGraph);

assertThat(product).isNotNull();
assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();
assertThat(Hibernate.isInitialized(product.getMaker())).isTrue();
}

@Transactional
@Test
public void given_brand_eg_when_findByName_then_brand_should_be_loaded(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class Product {
public static final String DEFAULT_EG = "Product.default";
public static final String BRAND_EG = "Product.brand";

public static final String BRAND_PROP_NAME = "brand";
public static final String MAKER_PROP_NAME = "maker";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Access(value = AccessType.PROPERTY)
Expand Down