Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
...-core/src/test/java/org/hibernate/orm/test/entitygraph/EntityGraphAttributeNodesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.hibernate.orm.test.entitygraph; | ||
|
||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.hibernate.annotations.OnDelete; | ||
import org.hibernate.annotations.OnDeleteAction; | ||
|
||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.Jpa; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EntityGraph; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@Jpa( | ||
annotatedClasses = { | ||
EntityGraphAttributeNodesTest.Human.class, | ||
EntityGraphAttributeNodesTest.House.class, | ||
EntityGraphAttributeNodesTest.Address.class, | ||
} | ||
) | ||
@JiraKey("HHH-16885") | ||
public class EntityGraphAttributeNodesTest { | ||
|
||
private final static Integer HUMAN_ID = 1; | ||
|
||
@BeforeAll | ||
public void setUp(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
Human human = new Human( HUMAN_ID ); | ||
entityManager.persist( human ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testIt(EntityManagerFactoryScope scope) { | ||
|
||
scope.inEntityManager( | ||
entityManager -> { | ||
EntityGraph<?> entityGraph = entityManager.createEntityGraph( Human.class ); | ||
entityGraph.addSubgraph( "houses" ).addAttributeNodes( "address" ); | ||
|
||
List results = entityManager.createQuery( "SELECT h FROM Human h WHERE h.id = ?1" ) | ||
.setParameter( 1, HUMAN_ID ) | ||
.setHint( "javax.persistence.fetchgraph", entityGraph ) | ||
.getResultList(); | ||
|
||
assertThat( results.size() ).isEqualTo( 1 ); | ||
assertThat( results.get( 0 ) ).isNotNull(); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Human") | ||
public static class Human { | ||
@Id | ||
private Integer id; | ||
|
||
private String name; | ||
|
||
@OneToMany(mappedBy = "human", cascade = CascadeType.ALL) | ||
@OnDelete(action = OnDeleteAction.CASCADE) | ||
private Collection<House> houses; | ||
|
||
public Human() { | ||
} | ||
|
||
public Human(Integer id) { | ||
this.id = id; | ||
this.houses = new ArrayList<>(); | ||
} | ||
|
||
public Human(Integer id, String name, Collection<House> houses) { | ||
this.id = id; | ||
this.name = name; | ||
this.houses = houses; | ||
} | ||
} | ||
|
||
@Entity(name = "House") | ||
public static class House { | ||
@Id | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "human_fk", nullable = false, updatable = false) | ||
private Human human; | ||
|
||
@Id | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "address_fk", nullable = false, updatable = false) | ||
private Address address; | ||
|
||
private String name; | ||
} | ||
|
||
@Entity(name = "Address") | ||
public static class Address { | ||
|
||
@Id | ||
@GeneratedValue | ||
private BigInteger id; | ||
|
||
private String street; | ||
|
||
} | ||
} |