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 .treat ;
8+
9+ import java .util .List ;
10+
11+ import org .hibernate .testing .orm .junit .EntityManagerFactoryScope ;
12+ import org .hibernate .testing .orm .junit .Jpa ;
13+ import org .junit .jupiter .api .BeforeEach ;
14+ import org .junit .jupiter .api .Test ;
15+
16+ import jakarta .persistence .DiscriminatorColumn ;
17+ import jakarta .persistence .DiscriminatorType ;
18+ import jakarta .persistence .DiscriminatorValue ;
19+ import jakarta .persistence .Entity ;
20+ import jakarta .persistence .Id ;
21+ import jakarta .persistence .Inheritance ;
22+ import jakarta .persistence .InheritanceType ;
23+ import jakarta .persistence .ManyToOne ;
24+ import jakarta .persistence .PrimaryKeyJoinColumn ;
25+ import jakarta .persistence .SecondaryTable ;
26+ import jakarta .persistence .SecondaryTables ;
27+ import jakarta .persistence .Table ;
28+
29+ import static org .junit .jupiter .api .Assertions .assertEquals ;
30+
31+
32+ @ Jpa (
33+ annotatedClasses = {
34+ HqlTreatTest .Product .class ,
35+ HqlTreatTest .SoftwareProduct .class ,
36+ HqlTreatTest .LineItem .class
37+ }
38+ )
39+ public class HqlTreatTest {
40+
41+
42+ @ BeforeEach
43+ public void setUp (EntityManagerFactoryScope scope ) {
44+ scope .inTransaction (
45+ entityManager -> {
46+ Product product1 = new Product ( 1 , "Monitor" );
47+ Product product2 = new SoftwareProduct ( 2 , "Linux" );
48+ LineItem lineItem1 = new LineItem ( 1 , 3 , product1 );
49+ LineItem lineItem2 = new LineItem ( 2 , 5 , product2 );
50+ entityManager .persist ( product1 );
51+ entityManager .persist ( product2 );
52+ entityManager .persist ( lineItem1 );
53+ entityManager .persist ( lineItem2 );
54+ }
55+ );
56+ }
57+
58+
59+ @ Test
60+ public void treatJoinClassTest (EntityManagerFactoryScope scope ) {
61+
62+ scope .inTransaction (
63+ entityManager -> {
64+ List <String > names = entityManager .createQuery (
65+ "SELECT s.name FROM LineItem l JOIN TREAT(l.product AS SoftwareProduct) s" )
66+ .getResultList ();
67+ assertEquals ( 1 , names .size () );
68+ assertEquals ( "Linux" , names .get ( 0 ) );
69+ }
70+ );
71+ }
72+
73+
74+ @ Entity (name = "LineItem" )
75+ @ Table (name = "LINEITEM_TABLE" )
76+ public static class LineItem {
77+
78+ @ Id
79+ private Integer id ;
80+
81+ private int quantity ;
82+
83+ @ ManyToOne
84+ private Product product ;
85+
86+ public LineItem () {
87+ }
88+
89+ public LineItem (Integer id , int quantity , Product product ) {
90+ this .id = id ;
91+ this .quantity = quantity ;
92+ this .product = product ;
93+ }
94+ }
95+
96+ @ Entity (name = "Product" )
97+ @ Table (name = "PRODUCT_TABLE" )
98+ @ SecondaryTables ({
99+ @ SecondaryTable (name = "PRODUCT_DETAILS" , pkJoinColumns = @ PrimaryKeyJoinColumn (name = "ID" ))
100+ })
101+ @ Inheritance (strategy = InheritanceType .SINGLE_TABLE )
102+ @ DiscriminatorColumn (name = "PRODUCT_TYPE" , discriminatorType = DiscriminatorType .STRING )
103+ @ DiscriminatorValue ("Product" )
104+ public static class Product {
105+
106+ @ Id
107+ private Integer id ;
108+
109+ private String name ;
110+
111+ public Product () {
112+ }
113+
114+ public Product (Integer id , String name ) {
115+ this .id = id ;
116+ this .name = name ;
117+ }
118+ }
119+
120+ @ Entity (name = "SoftwareProduct" )
121+ @ DiscriminatorValue ("SW" )
122+ public static class SoftwareProduct extends Product {
123+
124+ public SoftwareProduct () {
125+ }
126+
127+ public SoftwareProduct (Integer id , String name ) {
128+ super ( id , name );
129+ }
130+ }
131+
132+ }
0 commit comments