Skip to content

Commit cc2749f

Browse files
committed
Various code cleanup
1 parent e29beef commit cc2749f

File tree

2 files changed

+45
-63
lines changed

2 files changed

+45
-63
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/joined/JoinedSubclassDuplicateFieldsWithTreatTest.java

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,67 @@
55
package org.hibernate.orm.test.mapping.inheritance.joined;
66

77
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;
138

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;
1417
import org.hibernate.testing.orm.junit.DomainModel;
1518
import org.hibernate.testing.orm.junit.JiraKey;
1619
import org.hibernate.testing.orm.junit.SessionFactory;
1720
import org.hibernate.testing.orm.junit.SessionFactoryScope;
21+
import org.junit.jupiter.api.AfterEach;
22+
import org.junit.jupiter.api.BeforeEach;
1823
import org.junit.jupiter.api.Test;
1924

2025
import static org.assertj.core.api.Assertions.assertThat;
2126

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")
3328
@JiraKey( "HHH-11686" )
29+
@DomainModel(standardModels = StandardDomainModel.RETAIL)
30+
@SessionFactory
3431
public class JoinedSubclassDuplicateFieldsWithTreatTest {
35-
3632
@Test
37-
public void queryConstrainedSubclass(SessionFactoryScope scope) {
33+
public void testRestrictedTreat(SessionFactoryScope scope) {
34+
// SINGLE_TABLE
3835
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();
5939
} );
6040

41+
// JOINED
6142
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 );
6847
} );
6948
}
7049

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 );
7958

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+
} );
8465
}
8566

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();
9370
}
9471
}

hibernate-testing/src/main/java/org/hibernate/testing/orm/domain/retail/CashPayment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.testing.orm.domain.retail;
66

7+
import javax.money.Monetary;
78
import javax.money.MonetaryAmount;
89
import jakarta.persistence.Entity;
910

@@ -18,4 +19,8 @@ public CashPayment() {
1819
public CashPayment(Integer id, MonetaryAmount amount) {
1920
super( id, amount );
2021
}
22+
23+
public CashPayment(Integer id, Long amount, String currencyCode) {
24+
super( id, Monetary.getDefaultAmountFactory().setNumber( amount ).setCurrency( currencyCode ).create() );
25+
}
2126
}

0 commit comments

Comments
 (0)