Skip to content

Add optinal DynamicEntityGraph configuration mode #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.cosium.spring.data</groupId>
<artifactId>spring-data-jpa-entity-graph-parent</artifactId>
<version>3.2.3-SNAPSHOT</version>
<version>3.3-SNAPSHOT</version>
</parent>

<name>Spring Data JPA EntityGraph</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ void test() {
assertThat(Hibernate.isInitialized(product.getMaker().getCountry())).isTrue();
}

@Transactional
@Test
void test_() {
Product product =
productRepository
.findById(
1L,
ProductEntityGraph.inic_(
productEG ->
productEG.brand_().category_().maker_(makerEG -> makerEG.country_())))
.orElseThrow(RuntimeException::new);

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

@Test
@DisplayName("EntityGraph with embedded part is well generated")
void test2() {
Expand Down
2 changes: 1 addition & 1 deletion generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.cosium.spring.data</groupId>
<artifactId>spring-data-jpa-entity-graph-parent</artifactId>
<version>3.2.3-SNAPSHOT</version>
<version>3.3-SNAPSHOT</version>
</parent>

<name>Spring Data JPA EntityGraph Generator</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.processing.Filer;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -64,6 +65,20 @@ public EntityGraph(
.addStatement("return new $N()", rootComposer.simpleName())
.build();

MethodSpec inicMethod =
MethodSpec.methodBuilder("inic_")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(Constants.ENTITY_GRAPH_CLASS_NAME)
.addParameter(
ParameterizedTypeName.get(
ClassName.get(Function.class), rootComposerClassName, rootComposerClassName),
"initializer")
.addStatement(
"return initializer.apply($N()).$N()",
Constants.PATH_SEPARATOR,
Constants.PATH_SEPARATOR)
.build();

MethodSpec rootStaticMethodWithEntityGraphType =
MethodSpec.methodBuilder(Constants.PATH_SEPARATOR)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
Expand Down Expand Up @@ -96,6 +111,7 @@ public EntityGraph(
.addMethod(emptyConstructor)
.addMethod(constructor)
.addMethod(rootStaticMethod)
.addMethod(inicMethod)
.addMethod(rootStaticMethodWithEntityGraphType)
.addMethod(getEntityGraphTypeMethod)
.addType(rootComposer.toTypeSpec())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.List;
import java.util.function.Function;
import javax.lang.model.element.Modifier;
import javax.lang.model.util.Elements;

Expand Down Expand Up @@ -88,6 +89,24 @@ private void addPathToEntity(Elements elements, MetamodelAttributeTarget target)
.addStatement("path.add($S)", target.attributeName())
.addStatement("return new $T($N, path)", targetNodeComposer, Constants.PATH_SEPARATOR)
.build());

typeSpecBuilder.addMethod(
MethodSpec.methodBuilder(target.attributeName() + "_")
.addModifiers(Modifier.PUBLIC)
.returns(rootType)
.addStatement("return this.$N().$N", target.attributeName(), Constants.PATH_SEPARATOR)
.build());

typeSpecBuilder.addMethod(
MethodSpec.methodBuilder(target.attributeName() + "_")
.addModifiers(Modifier.PUBLIC)
.returns(rootType)
.addParameter(
ParameterizedTypeName.get(
ClassName.get(Function.class), targetNodeComposer, rootType),
"initializer")
.addStatement("return initializer.apply(this.$N())", target.attributeName())
.build());
}

private void addPathToLeafComposer(MetamodelAttributeTarget target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.squareup.javapoet.TypeSpec;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.lang.model.element.Modifier;
import javax.lang.model.util.Elements;

Expand Down Expand Up @@ -113,6 +114,26 @@ private void addPathToEntity(Elements elements, MetamodelAttributeTarget target)
.addStatement("entityGraphAttributePaths.add(path)")
.addStatement("return new $T(this, path)", targetNodeComposer)
.build());

typeSpecBuilder.addMethod(
MethodSpec.methodBuilder(target.attributeName() + "_")
.addModifiers(Modifier.PUBLIC)
.returns(entityGraphClassName.nestedClass(SIMPLE_NAME))
.addStatement("return this.$N().$N", target.attributeName(), Constants.PATH_SEPARATOR)
.build());

typeSpecBuilder.addMethod(
MethodSpec.methodBuilder(target.attributeName() + "_")
.addModifiers(Modifier.PUBLIC)
.returns(entityGraphClassName.nestedClass(SIMPLE_NAME))
.addParameter(
ParameterizedTypeName.get(
ClassName.get(Function.class),
targetNodeComposer,
entityGraphClassName.nestedClass(SIMPLE_NAME)),
"initializer")
.addStatement("return initializer.apply(this.$N())", target.attributeName())
.build());
}

private void addPathToLeafComposer(MetamodelAttributeTarget target) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>com.cosium.spring.data</groupId>
<artifactId>spring-data-jpa-entity-graph-parent</artifactId>
<version>3.2.3-SNAPSHOT</version>
<version>3.3-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
Expand Down
Loading