-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-16680 Add test for treating correlated from node
- Loading branch information
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/TreatCorrelatedTest.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,114 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.orm.test.jpa.criteria; | ||
|
||
import java.util.List; | ||
|
||
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.Test; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.DiscriminatorValue; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
import jakarta.persistence.criteria.Join; | ||
import jakarta.persistence.criteria.Root; | ||
import jakarta.persistence.criteria.Subquery; | ||
|
||
@Jpa( | ||
annotatedClasses = { | ||
TreatCorrelatedTest.BaseRoot.class, | ||
TreatCorrelatedTest.DynamicRoot.class, | ||
TreatCorrelatedTest.VersionedRoot.class, | ||
TreatCorrelatedTest.Node.class, | ||
TreatCorrelatedTest.Version.class | ||
} | ||
) | ||
@JiraKey("HHH-16680") | ||
public class TreatCorrelatedTest { | ||
|
||
@Test | ||
public void testCriteriaQuery(EntityManagerFactoryScope scope) { | ||
scope.inTransaction( | ||
entityManager -> { | ||
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | ||
CriteriaQuery<BaseRoot> query = cb.createQuery( BaseRoot.class ); | ||
|
||
Root<BaseRoot> root = query.from( BaseRoot.class ); | ||
|
||
Subquery<Long> nodesQuery = query.subquery( Long.class ); | ||
Root<BaseRoot> subQueryRoot = nodesQuery.from( BaseRoot.class ); | ||
nodesQuery.select( subQueryRoot.get( "id" ) ); | ||
Root<VersionedRoot> treatedRoot = cb.treat( subQueryRoot, VersionedRoot.class ); | ||
Join<VersionedRoot, Version> joinTrees = treatedRoot.join( "versions" ); | ||
Join<Version, Node> joinNodes = joinTrees.join( "nodes" ); | ||
nodesQuery.where( | ||
cb.equal( joinNodes.get( "version" ).get( "owner" ).get( "id" ), root.get( "id" ) ) | ||
); | ||
|
||
query.where( | ||
cb.and( | ||
cb.equal( root.get( "type" ), "Dog" ), | ||
cb.exists( nodesQuery ) | ||
) | ||
); | ||
|
||
query.select( root ); | ||
|
||
List<BaseRoot> result = entityManager.createQuery( query ).getResultList(); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "BaseRoot") | ||
public static class BaseRoot { | ||
|
||
@Id | ||
private Long id; | ||
private String name; | ||
private String type; | ||
} | ||
|
||
@Entity | ||
@DiscriminatorValue(value = "1") | ||
public static class DynamicRoot extends BaseRoot { | ||
} | ||
|
||
@Entity(name = "Node") | ||
public static class Node { | ||
@Id | ||
private Long id; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Version version; | ||
} | ||
|
||
@Entity(name = "Version") | ||
public static class Version { | ||
@Id | ||
private Long id; | ||
private String name; | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
private VersionedRoot owner; | ||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "version", cascade = CascadeType.PERSIST) | ||
private List<Node> nodes; | ||
} | ||
|
||
@Entity | ||
@DiscriminatorValue(value = "2") | ||
public static class VersionedRoot extends BaseRoot { | ||
|
||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<Version> versions; | ||
} | ||
} |