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