-
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-13058 fix issue left join root cannot be replaced by correlated p…
…arent in subquery
- Loading branch information
1 parent
efa7e66
commit 2662edf
Showing
7 changed files
with
322 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
133 changes: 133 additions & 0 deletions
133
...rnate-core/src/test/java/org/hibernate/query/criteria/internal/hhh13058/HHH13058Test.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,133 @@ | ||
package org.hibernate.query.criteria.internal.hhh13058; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.From; | ||
import javax.persistence.criteria.JoinType; | ||
import javax.persistence.criteria.Root; | ||
import javax.persistence.criteria.Subquery; | ||
|
||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* @author Archie Cobbs | ||
* @author Nathan Xu | ||
*/ | ||
@TestForIssue( jiraKey = "HHH-13058" ) | ||
public class HHH13058Test extends BaseEntityManagerFunctionalTestCase { | ||
|
||
private Set<Site> validSites; | ||
|
||
private Task taskWithoutPatient; | ||
private Task taskWithPatientWithoutSite; | ||
private Task taskWithPatient1WithValidSite1; | ||
private Task taskWithPatient2WithValidSite1; | ||
private Task taskWithPatient3WithValidSite2; | ||
private Task taskWithPatientWithInvalidSite; | ||
|
||
@Override | ||
public Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { | ||
Task.class, | ||
Patient.class, | ||
Site.class | ||
}; | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
final Site validSite1 = new Site(); | ||
final Site validSite2 = new Site(); | ||
final Site invalidSite = new Site(); | ||
|
||
entityManager.persist( validSite1 ); | ||
entityManager.persist( validSite2 ); | ||
entityManager.persist( invalidSite ); | ||
|
||
validSites = new HashSet<>( Arrays.asList( validSite1, validSite2 ) ); | ||
|
||
final Patient patientWithoutSite = new Patient(); | ||
final Patient patient1WithValidSite1 = new Patient( validSite1 ); | ||
final Patient patient2WithValidSite1 = new Patient( validSite1 ); | ||
final Patient patient3WithValidSite2 = new Patient( validSite2 ); | ||
final Patient patientWithInvalidSite = new Patient( invalidSite ); | ||
|
||
entityManager.persist( patientWithoutSite ); | ||
entityManager.persist( patient1WithValidSite1 ); | ||
entityManager.persist( patient2WithValidSite1 ); | ||
entityManager.persist( patient3WithValidSite2 ); | ||
entityManager.persist( patientWithInvalidSite ); | ||
|
||
taskWithoutPatient = new Task(); | ||
taskWithoutPatient.description = "taskWithoutPatient"; | ||
|
||
taskWithPatientWithoutSite = new Task( patientWithoutSite ); | ||
taskWithPatientWithoutSite.description = "taskWithPatientWithoutSite"; | ||
|
||
taskWithPatient1WithValidSite1 = new Task( patient1WithValidSite1 ); | ||
taskWithPatient1WithValidSite1.description = "taskWithPatient1WithValidSite1"; | ||
|
||
taskWithPatient2WithValidSite1 = new Task( patient2WithValidSite1 ); | ||
taskWithPatient2WithValidSite1.description = "taskWithPatient2WithValidSite1"; | ||
|
||
taskWithPatient3WithValidSite2 = new Task( patient3WithValidSite2 ); | ||
taskWithPatient3WithValidSite2.description = "taskWithPatient3WithValidSite2"; | ||
|
||
taskWithPatientWithInvalidSite = new Task( patientWithInvalidSite ); | ||
taskWithPatientWithInvalidSite.description = "taskWithPatientWithInvalidSite"; | ||
|
||
entityManager.persist( taskWithoutPatient ); | ||
entityManager.persist( taskWithPatientWithoutSite ); | ||
entityManager.persist( taskWithPatient1WithValidSite1 ); | ||
entityManager.persist( taskWithPatient2WithValidSite1 ); | ||
entityManager.persist( taskWithPatient3WithValidSite2 ); | ||
entityManager.persist( taskWithPatientWithInvalidSite ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testCorrelateSubQueryLeftJoin() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); | ||
final CriteriaQuery<Task> outerQuery = builder.createQuery( Task.class ); | ||
final Root<Task> outerTask = outerQuery.from( Task.class ); | ||
|
||
final Subquery<Task> subquery = outerQuery.subquery( Task.class ); | ||
final Root<Task> subtask = subquery.correlate( outerTask ); | ||
final From<Task, Patient> patient = subtask.join( Task_.patient, JoinType.LEFT ); | ||
final From<Patient, Site> site = patient.join( Patient_.site, JoinType.LEFT ); | ||
outerQuery.where( | ||
builder.exists( | ||
subquery.select( subtask ) | ||
.where( | ||
builder.or( | ||
patient.isNull(), | ||
site.in( validSites ) | ||
) | ||
) | ||
) | ||
); | ||
final List<Task> tasks = entityManager.createQuery( outerQuery ).getResultList(); | ||
assertThat( new HashSet<>( tasks ), is( new HashSet<>( Arrays.asList( | ||
taskWithoutPatient, | ||
taskWithPatient1WithValidSite1, | ||
taskWithPatient2WithValidSite1, | ||
taskWithPatient3WithValidSite2 | ||
) ) ) ); | ||
|
||
} ); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh13058/Patient.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,32 @@ | ||
package org.hibernate.query.criteria.internal.hhh13058; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.Table; | ||
|
||
/** | ||
* @author Archie Cobbs | ||
* @author Nathan Xu | ||
*/ | ||
@Entity(name = "Patient") | ||
@Table(name = "Patient") | ||
public class Patient { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
Site site; | ||
|
||
public Patient() { | ||
} | ||
|
||
public Patient(Site site) { | ||
this.site = site; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
hibernate-core/src/test/java/org/hibernate/query/criteria/internal/hhh13058/Site.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,20 @@ | ||
package org.hibernate.query.criteria.internal.hhh13058; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
|
||
/** | ||
* @author Archie Cobbs | ||
* @author Nathan Xu | ||
*/ | ||
@Entity(name = "Site") | ||
@Table(name = "Site") | ||
public class Site { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id; | ||
|
||
} |
Oops, something went wrong.