Skip to content

Commit 063cb0c

Browse files
jrenaatbeikov
authored andcommitted
Added test for the issues mentioned in HHH-987 and HHH-992
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
1 parent 9399214 commit 063cb0c

File tree

6 files changed

+180
-28
lines changed

6 files changed

+180
-28
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/bidi/Auction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
public class Auction {
1919
private Long id;
2020
private String description;
21-
private List bids = new ArrayList();
21+
private List<Bid> bids = new ArrayList<Bid>();
2222
private Bid successfulBid;
2323
private Date end;
2424

@@ -30,11 +30,11 @@ public void setEnd(Date end) {
3030
this.end = end;
3131
}
3232

33-
public List getBids() {
33+
public List<Bid> getBids() {
3434
return bids;
3535
}
3636

37-
public void setBids(List bids) {
37+
public void setBids(List<Bid> bids) {
3838
this.bids = bids;
3939
}
4040

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!DOCTYPE hibernate-mapping PUBLIC
9+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
10+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
11+
12+
<hibernate-mapping
13+
package="org.hibernate.orm.test.bidi">
14+
15+
<class name="Auction" table="TAuction">
16+
<id name="id">
17+
<generator class="native"/>
18+
</id>
19+
<property name="description"/>
20+
<property name="end" column="endDatetime"/>
21+
<bag name="bids" inverse="true"
22+
cascade="persist">
23+
<key column="auctionId"/>
24+
<one-to-many class="Bid"/>
25+
</bag>
26+
<one-to-one name="successfulBid"
27+
property-ref="abc">
28+
<formula>id</formula>
29+
<formula>true</formula>
30+
</one-to-one>
31+
</class>
32+
33+
<class name="Bid" table="TBid" abstract="true">
34+
<id name="id">
35+
<generator class="native"/>
36+
</id>
37+
38+
<discriminator column="DISC"/>
39+
40+
<property name="amount"
41+
scale="19"
42+
precision="31" />
43+
<property name="datetime"
44+
column="createdDatetime"/>
45+
<properties name="abc">
46+
<many-to-one name="item"
47+
column="auctionId"
48+
cascade="persist"/>
49+
<property name="successful"
50+
column="success"/>
51+
</properties>
52+
53+
<subclass name="SpecialBid" discriminator-value="SPECIAL">
54+
<property name="isSpecial" access="field"/>
55+
</subclass>
56+
</class>
57+
58+
</hibernate-mapping>

hibernate-core/src/test/java/org/hibernate/orm/test/bidi/AuctionTest.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.orm.test.bidi;
88

99
import java.math.BigDecimal;
10+
import java.math.RoundingMode;
1011
import java.util.Date;
1112
import java.util.List;
1213

@@ -15,11 +16,9 @@
1516
import org.hibernate.testing.orm.junit.DomainModel;
1617
import org.hibernate.testing.orm.junit.SessionFactory;
1718
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18-
import org.junit.Assert;
1919
import org.junit.jupiter.api.Test;
2020

2121
import static org.junit.jupiter.api.Assertions.assertFalse;
22-
import static org.junit.jupiter.api.Assertions.assertNotNull;
2322
import static org.junit.jupiter.api.Assertions.assertSame;
2423
import static org.junit.jupiter.api.Assertions.assertTrue;
2524

@@ -33,14 +32,13 @@
3332
public class AuctionTest {
3433

3534
@Test
36-
@SuppressWarnings("unchecked")
3735
public void testLazy(SessionFactoryScope scope) {
3836
Auction auction = new Auction();
3937
auction.setEnd( new Date() );
4038
auction.setDescription( "an auction for something" );
4139

4240
Bid bid = new Bid();
43-
bid.setAmount( new BigDecimal( 123.34 ).setScale( 19, BigDecimal.ROUND_DOWN ) );
41+
bid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
4442
bid.setSuccessful( true );
4543
bid.setDatetime( new Date() );
4644
bid.setItem( auction );
@@ -64,22 +62,22 @@ public void testLazy(SessionFactoryScope scope) {
6462

6563
scope.inTransaction(
6664
session -> {
67-
Bid b = session.load( Bid.class, bidId );
65+
Bid b = session.getReference( Bid.class, bidId );
6866
assertFalse( Hibernate.isInitialized( b ) );
6967

7068
Bid initializedBid = session.get( Bid.class, bidId );
71-
Assert.assertSame( initializedBid, b );
72-
Assert.assertTrue( Hibernate.isInitialized( b ) );
69+
assertSame( initializedBid, b );
70+
assertTrue( Hibernate.isInitialized( b ) );
7371
}
7472
);
7573

7674
scope.inTransaction(
7775
session -> {
78-
Bid b = session.load( Bid.class, bidId );
76+
Bid b = session.getReference( Bid.class, bidId );
7977
assertFalse( Hibernate.isInitialized( b ) );
8078
Auction a = session.get( Auction.class, aid );
8179

82-
List bids = a.getBids();
80+
List<Bid> bids = a.getBids();
8381
assertFalse( Hibernate.isInitialized( bids ) );
8482
Bid successfulBid = a.getSuccessfulBid();
8583
assertTrue( Hibernate.isInitialized( successfulBid ) );
@@ -96,9 +94,9 @@ public void testLazy(SessionFactoryScope scope) {
9694

9795
scope.inTransaction(
9896
session -> {
99-
Bid b = session.load( Bid.class, bidId );
97+
Bid b = session.getReference( Bid.class, bidId );
10098
assertFalse( Hibernate.isInitialized( b ) );
101-
Auction a = (Auction) session.createQuery( "from Auction a left join fetch a.bids" ).uniqueResult();
99+
Auction a = session.createQuery( "from Auction a left join fetch a.bids", Auction.class ).uniqueResult();
102100
assertTrue( Hibernate.isInitialized( b ) );
103101
assertTrue( Hibernate.isInitialized( a.getBids() ) );
104102
assertSame( b, a.getSuccessfulBid() );
@@ -109,11 +107,11 @@ public void testLazy(SessionFactoryScope scope) {
109107

110108
scope.inTransaction(
111109
session -> {
112-
Bid b = session.load( Bid.class, bidId );
113-
Auction a = session.load( Auction.class, aid );
110+
Bid b = session.getReference( Bid.class, bidId );
111+
Auction a = session.getReference( Auction.class, aid );
114112
assertFalse( Hibernate.isInitialized( b ) );
115113
assertFalse( Hibernate.isInitialized( a ) );
116-
session.createQuery( "from Auction a left join fetch a.successfulBid" ).list();
114+
session.createQuery( "from Auction a left join fetch a.successfulBid", Auction.class ).list();
117115
assertTrue( Hibernate.isInitialized( b ) );
118116
assertTrue( Hibernate.isInitialized( a ) );
119117
assertSame( b, a.getSuccessfulBid() );
@@ -125,8 +123,8 @@ public void testLazy(SessionFactoryScope scope) {
125123

126124
scope.inTransaction(
127125
session -> {
128-
Bid b = session.load( Bid.class, bidId );
129-
Auction a = session.load( Auction.class, aid );
126+
Bid b = session.getReference( Bid.class, bidId );
127+
Auction a = session.getReference( Auction.class, aid );
130128
assertFalse( Hibernate.isInitialized( b ) );
131129
assertFalse( Hibernate.isInitialized( a ) );
132130
assertSame( session.get( Bid.class, bidId ), b );

hibernate-core/src/test/java/org/hibernate/orm/test/bidi/AuctionTest2.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.orm.test.bidi;
88

99
import java.math.BigDecimal;
10+
import java.math.RoundingMode;
1011
import java.util.Date;
1112

1213
import org.hibernate.Hibernate;
@@ -41,7 +42,7 @@ public void testLazy(SessionFactoryScope scope) {
4142
auction.setDescription( "an auction for something" );
4243
auction.setEnd( new Date() );
4344
Bid bid = new Bid();
44-
bid.setAmount( new BigDecimal( 123.34 ).setScale( 19, BigDecimal.ROUND_DOWN ) );
45+
bid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
4546
bid.setSuccessful( true );
4647
bid.setDatetime( new Date() );
4748
bid.setItem( auction );
@@ -57,7 +58,7 @@ public void testLazy(SessionFactoryScope scope) {
5758

5859
scope.inTransaction(
5960
session -> {
60-
Bid b = session.load( Bid.class, bidId );
61+
Bid b = session.getReference( Bid.class, bidId );
6162
assertFalse( Hibernate.isInitialized( b ) );
6263
Auction a = session.get( Auction.class, aid );
6364
assertFalse( Hibernate.isInitialized( a.getBids() ) );
@@ -71,9 +72,9 @@ public void testLazy(SessionFactoryScope scope) {
7172

7273
scope.inTransaction(
7374
session -> {
74-
Bid b = session.load( Bid.class, bidId );
75+
Bid b = session.getReference( Bid.class, bidId );
7576
assertFalse( Hibernate.isInitialized( b ) );
76-
Auction a = (Auction) session.createQuery( "from Auction a left join fetch a.bids" ).uniqueResult();
77+
Auction a = session.createQuery( "from Auction a left join fetch a.bids", Auction.class ).uniqueResult();
7778
assertTrue( Hibernate.isInitialized( b ) );
7879
assertTrue( Hibernate.isInitialized( a.getBids() ) );
7980
assertSame( b, a.getSuccessfulBid() );
@@ -84,11 +85,11 @@ public void testLazy(SessionFactoryScope scope) {
8485

8586
scope.inTransaction(
8687
session -> {
87-
Bid b = session.load( Bid.class, bidId );
88-
Auction a = session.load( Auction.class, aid );
88+
Bid b = session.getReference( Bid.class, bidId );
89+
Auction a = session.getReference( Auction.class, aid );
8990
assertFalse( Hibernate.isInitialized( b ) );
9091
assertFalse( Hibernate.isInitialized( a ) );
91-
session.createQuery( "from Auction a left join fetch a.successfulBid" ).list();
92+
session.createQuery( "from Auction a left join fetch a.successfulBid", Auction.class ).list();
9293
assertTrue( Hibernate.isInitialized( b ) );
9394
assertTrue( Hibernate.isInitialized( a ) );
9495
assertSame( b, a.getSuccessfulBid() );
@@ -100,8 +101,8 @@ public void testLazy(SessionFactoryScope scope) {
100101

101102
scope.inTransaction(
102103
session -> {
103-
Bid b = session.load( Bid.class, bidId );
104-
Auction a = session.load( Auction.class, aid );
104+
Bid b = session.getReference( Bid.class, bidId );
105+
Auction a = session.getReference( Auction.class, aid );
105106
assertFalse( Hibernate.isInitialized( b ) );
106107
assertFalse( Hibernate.isInitialized( a ) );
107108
assertSame( session.get( Bid.class, bidId ), b );
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.bidi;
8+
9+
import java.math.BigDecimal;
10+
import java.math.RoundingMode;
11+
import java.util.Date;
12+
13+
import org.hibernate.testing.TestForIssue;
14+
import org.hibernate.testing.orm.junit.DomainModel;
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 static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
/**
23+
* @author Jan Schatteman
24+
*/
25+
@DomainModel(
26+
xmlMappings = "org/hibernate/orm/test/bidi/Auction3.hbm.xml"
27+
)
28+
@SessionFactory
29+
@TestForIssue( jiraKey = {"HHH-987", "HHH-992"} )
30+
public class AuctionWithAbstractBidClassTest {
31+
32+
@Test
33+
public void testAbstractSuperClassMapping(SessionFactoryScope scope) {
34+
Auction auction = new Auction();
35+
auction.setEnd( new Date() );
36+
auction.setDescription( "an auction for something" );
37+
38+
SpecialBid ssbid = new SpecialBid();
39+
ssbid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
40+
ssbid.setSuccessful( true );
41+
ssbid.setSpecial( false );
42+
ssbid.setDatetime( new Date() );
43+
ssbid.setItem( auction );
44+
auction.getBids().add( ssbid );
45+
auction.setSuccessfulBid( ssbid );
46+
47+
SpecialBid sbid = new SpecialBid();
48+
sbid.setAmount( new BigDecimal( "321.43" ).setScale( 19, RoundingMode.DOWN ) );
49+
sbid.setSuccessful( false );
50+
sbid.setSpecial( true );
51+
sbid.setDatetime( new Date() );
52+
sbid.setItem( auction );
53+
auction.getBids().add( sbid );
54+
55+
scope.inTransaction(
56+
session ->
57+
session.persist( auction )
58+
);
59+
60+
Long auctionId = auction.getId();
61+
Long ssbidId = ssbid.getId();
62+
63+
scope.inTransaction(
64+
session -> {
65+
Auction auc = session.get( Auction.class, auctionId );
66+
SpecialBid successfulBid = (SpecialBid) auc.getSuccessfulBid();
67+
assertTrue( successfulBid.isSuccessful() );
68+
assertEquals( successfulBid.getId(), ssbidId );
69+
}
70+
);
71+
}
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
8+
//$Id: Bid.java 5733 2005-02-14 15:56:06Z oneovthafew $
9+
package org.hibernate.orm.test.bidi;
10+
11+
/**
12+
*/
13+
public class SpecialBid extends Bid {
14+
private boolean isSpecial;
15+
16+
public boolean isSpecial() {
17+
return isSpecial;
18+
}
19+
20+
public void setSpecial(boolean isSpecial) {
21+
this.isSpecial = isSpecial;
22+
}
23+
}

0 commit comments

Comments
 (0)