Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package example.springdata.jpa.fetchgraph;

import jakarta.persistence.GenerationType;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.HashSet;
Expand All @@ -30,17 +32,19 @@
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.NamedEntityGraphs;
import lombok.Setter;

/**
* @author Thomas Darimont
*/
@Data
@NoArgsConstructor
@Entity
@NamedEntityGraphs(@NamedEntityGraph(name = "product-with-tags", attributeNodes = { @NamedAttributeNode("tags") }))
@Getter
@Setter
public class Product {

@Id @GeneratedValue //
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

String name;
Expand Down
12 changes: 10 additions & 2 deletions jpa/jpa21/src/main/java/example/springdata/jpa/fetchgraph/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,35 @@
*/
package example.springdata.jpa.fetchgraph;

import jakarta.persistence.GenerationType;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Setter;

/**
* @author Thomas Darimont
*/
@Data

@NoArgsConstructor
@Entity
@Getter
@Setter
public class Tag {

@Id @GeneratedValue //
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

String name;

public Tag(String name) {
this.name = name;
}
public static Tag createTestTag(int num){
return new Tag(String.format("Tag%d", num));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
*/
package example.springdata.jpa.fetchgraph;

import static org.assertj.core.api.Assertions.*;

import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import jakarta.persistence.EntityManager;

import java.util.Collections;
import java.util.stream.IntStream;
import org.hibernate.LazyInitializationException;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -47,7 +46,13 @@ class FetchGraphIntegrationTests {
void shouldFetchAssociationMarkedAsLazyViaNamedEntityFetchGraph() {

var xps = new Product("Dell XPS 15");
Collections.addAll(xps.getTags(), new Tag("cool"), new Tag("macbook-killer"), new Tag("speed"));
var expectedSize = 10;

Tag[] arr = IntStream.range(0, expectedSize)
.mapToObj(Tag::createTestTag)
.toArray(Tag[]::new);

Collections.addAll(xps.getTags(), arr);

xps = repository.save(xps);
repository.flush();
Expand All @@ -64,7 +69,7 @@ void shouldFetchAssociationMarkedAsLazyViaNamedEntityFetchGraph() {
// here we use the findWithNamedEntityGraphById that uses a NamedEntityGraph
var loadedXpsWithFetchGraph = repository.findWithNamedEntityGraphById(xps.getId());

assertThat(loadedXpsWithFetchGraph.getTags()).hasSize(3);
assertThat(loadedXpsWithFetchGraph.getTags()).hasSize(expectedSize);
}

@Test
Expand Down
Loading