|
5 | 5 | package org.hibernate.orm.test.mapping.inheritance.joined;
|
6 | 6 |
|
7 | 7 | import java.util.List;
|
8 |
| -import jakarta.persistence.Column; |
9 |
| -import jakarta.persistence.Entity; |
10 |
| -import jakarta.persistence.Id; |
11 |
| -import jakarta.persistence.Inheritance; |
12 |
| -import jakarta.persistence.InheritanceType; |
13 | 8 |
|
| 9 | + |
| 10 | +import org.hibernate.testing.orm.domain.StandardDomainModel; |
| 11 | +import org.hibernate.testing.orm.domain.retail.CardPayment; |
| 12 | +import org.hibernate.testing.orm.domain.retail.CashPayment; |
| 13 | +import org.hibernate.testing.orm.domain.retail.DomesticVendor; |
| 14 | +import org.hibernate.testing.orm.domain.retail.ForeignVendor; |
| 15 | +import org.hibernate.testing.orm.domain.retail.Payment; |
| 16 | +import org.hibernate.testing.orm.domain.retail.Vendor; |
14 | 17 | import org.hibernate.testing.orm.junit.DomainModel;
|
15 | 18 | import org.hibernate.testing.orm.junit.JiraKey;
|
16 | 19 | import org.hibernate.testing.orm.junit.SessionFactory;
|
17 | 20 | import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
18 | 23 | import org.junit.jupiter.api.Test;
|
19 | 24 |
|
20 | 25 | import static org.assertj.core.api.Assertions.assertThat;
|
21 | 26 |
|
22 |
| -/** |
23 |
| - * @author pholvs |
24 |
| - */ |
25 |
| -@DomainModel( |
26 |
| - annotatedClasses = { |
27 |
| - JoinedSubclassDuplicateFieldsWithTreatTest.Account.class, |
28 |
| - JoinedSubclassDuplicateFieldsWithTreatTest.Deposit.class, |
29 |
| - JoinedSubclassDuplicateFieldsWithTreatTest.Loan.class |
30 |
| - } |
31 |
| -) |
32 |
| -@SessionFactory |
| 27 | +@SuppressWarnings("JUnitMalformedDeclaration") |
33 | 28 | @JiraKey( "HHH-11686" )
|
| 29 | +@DomainModel(standardModels = StandardDomainModel.RETAIL) |
| 30 | +@SessionFactory |
34 | 31 | public class JoinedSubclassDuplicateFieldsWithTreatTest {
|
35 |
| - |
36 | 32 | @Test
|
37 |
| - public void queryConstrainedSubclass(SessionFactoryScope scope) { |
| 33 | + public void testRestrictedTreat(SessionFactoryScope scope) { |
| 34 | + // SINGLE_TABLE |
38 | 35 | scope.inTransaction( (session) -> {
|
39 |
| - Deposit deposit1 = new Deposit(); |
40 |
| - deposit1.id = 1L; |
41 |
| - deposit1.interest = 10; |
42 |
| - |
43 |
| - Loan loan1 = new Loan(); |
44 |
| - loan1.id = 2L; |
45 |
| - loan1.interest = 10; |
46 |
| - |
47 |
| - Deposit deposit2 = new Deposit(); |
48 |
| - deposit2.id = 3L; |
49 |
| - deposit2.interest = 20; |
50 |
| - |
51 |
| - Loan loan2 = new Loan(); |
52 |
| - loan2.id = 4L; |
53 |
| - loan2.interest = 30; |
54 |
| - |
55 |
| - session.persist(deposit1); |
56 |
| - session.persist(loan1); |
57 |
| - session.persist(deposit2); |
58 |
| - session.persist(loan2); |
| 36 | + final String qry = "from Vendor v where treat(v as DomesticVendor).name = 'Spacely'"; |
| 37 | + final List<Vendor> vendors = session.createQuery( qry, Vendor.class ).getResultList(); |
| 38 | + assertThat( vendors ).isEmpty(); |
59 | 39 | } );
|
60 | 40 |
|
| 41 | + // JOINED |
61 | 42 | scope.inTransaction( (session) -> {
|
62 |
| - List<Account> accounts = session |
63 |
| - .createQuery( |
64 |
| - "select a from Account a where treat(a as Loan).interest = 10", |
65 |
| - Account.class |
66 |
| - ).getResultList(); |
67 |
| - assertThat( accounts ).hasSize( 1 ); |
| 43 | + final String qry = "from Payment p where treat(p as CardPayment).transactionId = 123"; |
| 44 | + final List<Payment> payments = session.createQuery( qry, Payment.class ).getResultList(); |
| 45 | + assertThat( payments ).hasSize( 1 ); |
| 46 | + assertThat( payments.get( 0 ) ).isInstanceOf( CardPayment.class ); |
68 | 47 | } );
|
69 | 48 | }
|
70 | 49 |
|
71 |
| - @Entity(name = "Account") |
72 |
| - @Inheritance(strategy = InheritanceType.JOINED) |
73 |
| - public static class Account |
74 |
| - { |
75 |
| - @Id |
76 |
| - public Long id; |
77 |
| - } |
78 |
| - |
| 50 | + @BeforeEach |
| 51 | + void createTestData(SessionFactoryScope sessions) { |
| 52 | + sessions.inTransaction( (session) -> { |
| 53 | + // SINGLE_TABLE |
| 54 | + final DomesticVendor acme = new DomesticVendor( 1, "Acme", "Acme, LLC" ); |
| 55 | + final ForeignVendor spacely = new ForeignVendor( 2, "Spacely", "Spacely Space Sprockets, Inc" ); |
| 56 | + session.persist( acme ); |
| 57 | + session.persist( spacely ); |
79 | 58 |
|
80 |
| - @Entity(name = "Deposit") |
81 |
| - public static class Deposit extends Account { |
82 |
| - @Column |
83 |
| - public Integer interest; |
| 59 | + // JOINED |
| 60 | + final CardPayment cardPayment = new CardPayment( 1, 123, 123L, "USD" ); |
| 61 | + final CashPayment cashPayment = new CashPayment( 2, 789L, "USD" ); |
| 62 | + session.persist( cardPayment ); |
| 63 | + session.persist( cashPayment ); |
| 64 | + } ); |
84 | 65 | }
|
85 | 66 |
|
86 |
| - @Entity(name = "Loan") |
87 |
| - public static class Loan extends Account { |
88 |
| - @Column |
89 |
| - public Integer interest; |
90 |
| - |
91 |
| - @Column |
92 |
| - public Integer rate; |
| 67 | + @AfterEach |
| 68 | + void dropTestData(SessionFactoryScope sessions) { |
| 69 | + sessions.dropData(); |
93 | 70 | }
|
94 | 71 | }
|
0 commit comments