|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.hql.set; |
| 6 | + |
| 7 | +import java.time.LocalDate; |
| 8 | + |
| 9 | +import org.hibernate.query.Query; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 14 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.GeneratedValue; |
| 21 | +import jakarta.persistence.GenerationType; |
| 22 | +import jakarta.persistence.Id; |
| 23 | +import jakarta.persistence.ManyToOne; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Jan Schatteman |
| 27 | + */ |
| 28 | +@DomainModel ( |
| 29 | + annotatedClasses = { UnionOfPartitionResultsTest.Apple.class, UnionOfPartitionResultsTest.Pie.class } |
| 30 | +) |
| 31 | +@SessionFactory |
| 32 | +public class UnionOfPartitionResultsTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + @FailureExpected |
| 36 | + @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsUnion.class) |
| 37 | + public void testUnionOfPartitionResults(SessionFactoryScope scope) { |
| 38 | + scope.inTransaction( |
| 39 | + session -> { |
| 40 | + String q = |
| 41 | + "SELECT new CurrentApple(id, bakedPie.id, dir) " + |
| 42 | + "FROM (" + |
| 43 | + "(" + |
| 44 | + "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MAX(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, -1 dir " + |
| 45 | + "FROM Apple c " + |
| 46 | + "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn <= :now" + |
| 47 | + ") UNION ALL (" + |
| 48 | + "SELECT id id, bakedPie bakedPie, bakedOn bakedOn, MIN(bakedOn) OVER (PARTITION BY bakedPie.id) mbo, 1 dir " + |
| 49 | + "FROM Apple c " + |
| 50 | + "WHERE bakedPie.id IN (1,2,3,4) AND bakedOn > :now" + |
| 51 | + ")" + |
| 52 | + ") " + |
| 53 | + "WHERE bakedOn = mbo ORDER BY dir"; |
| 54 | + |
| 55 | + Query<CurrentApple> query = session.createQuery( q, CurrentApple.class ); |
| 56 | + query.setParameter( "now", LocalDate.now()); |
| 57 | + |
| 58 | + query.getSingleResult(); |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + public static class CurrentApple { |
| 64 | + private final int id; |
| 65 | + private final int pieId; |
| 66 | + private final int dir; |
| 67 | + |
| 68 | + public CurrentApple(int id, int pieId, int dir) { |
| 69 | + this.id = id; |
| 70 | + this.pieId = pieId; |
| 71 | + this.dir = dir; |
| 72 | + } |
| 73 | + public int getDir() { |
| 74 | + return dir; |
| 75 | + } |
| 76 | + public int getId() { |
| 77 | + return id; |
| 78 | + } |
| 79 | + public int getPieId() { |
| 80 | + return pieId; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Entity(name = "Apple") |
| 85 | + public static class Apple { |
| 86 | + @Id |
| 87 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 88 | + private Integer id; |
| 89 | + private LocalDate bakedOn; |
| 90 | + @ManyToOne |
| 91 | + private Pie bakedPie; |
| 92 | + |
| 93 | + public Integer getId() { |
| 94 | + return id; |
| 95 | + } |
| 96 | + public Apple setId(Integer id) { |
| 97 | + this.id = id; |
| 98 | + return this; |
| 99 | + } |
| 100 | + public LocalDate getBakedOn() { |
| 101 | + return bakedOn; |
| 102 | + } |
| 103 | + public Apple setBakedOn(LocalDate bakedOn) { |
| 104 | + this.bakedOn = bakedOn; |
| 105 | + return this; |
| 106 | + } |
| 107 | + public Pie getBakedPie() { |
| 108 | + return bakedPie; |
| 109 | + } |
| 110 | + public Apple setBakedPie(Pie bakedPie) { |
| 111 | + this.bakedPie = bakedPie; |
| 112 | + return this; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Entity(name = "Pie") |
| 117 | + public static class Pie { |
| 118 | + @Id |
| 119 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 120 | + private Integer id; |
| 121 | + private String taste; |
| 122 | + |
| 123 | + public Integer getId() { |
| 124 | + return id; |
| 125 | + } |
| 126 | + public Pie setId(Integer id) { |
| 127 | + this.id = id; |
| 128 | + return this; |
| 129 | + } |
| 130 | + public String getTaste() { |
| 131 | + return taste; |
| 132 | + } |
| 133 | + public Pie setTaste(String taste) { |
| 134 | + this.taste = taste; |
| 135 | + return this; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments