Added test for the issues mentioned in HHH-987 and HHH-992

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2022-09-22 23:54:01 +02:00 committed by Christian Beikov
parent 93992147c5
commit 063cb0ccd9
6 changed files with 180 additions and 28 deletions

View File

@ -18,7 +18,7 @@ import java.util.List;
public class Auction {
private Long id;
private String description;
private List bids = new ArrayList();
private List<Bid> bids = new ArrayList<Bid>();
private Bid successfulBid;
private Date end;
@ -30,11 +30,11 @@ public class Auction {
this.end = end;
}
public List getBids() {
public List<Bid> getBids() {
return bids;
}
public void setBids(List bids) {
public void setBids(List<Bid> bids) {
this.bids = bids;
}

View File

@ -0,0 +1,58 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.orm.test.bidi">
<class name="Auction" table="TAuction">
<id name="id">
<generator class="native"/>
</id>
<property name="description"/>
<property name="end" column="endDatetime"/>
<bag name="bids" inverse="true"
cascade="persist">
<key column="auctionId"/>
<one-to-many class="Bid"/>
</bag>
<one-to-one name="successfulBid"
property-ref="abc">
<formula>id</formula>
<formula>true</formula>
</one-to-one>
</class>
<class name="Bid" table="TBid" abstract="true">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="DISC"/>
<property name="amount"
scale="19"
precision="31" />
<property name="datetime"
column="createdDatetime"/>
<properties name="abc">
<many-to-one name="item"
column="auctionId"
cascade="persist"/>
<property name="successful"
column="success"/>
</properties>
<subclass name="SpecialBid" discriminator-value="SPECIAL">
<property name="isSpecial" access="field"/>
</subclass>
</class>
</hibernate-mapping>

View File

@ -7,6 +7,7 @@
package org.hibernate.orm.test.bidi;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
@ -15,11 +16,9 @@ import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -33,14 +32,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class AuctionTest {
@Test
@SuppressWarnings("unchecked")
public void testLazy(SessionFactoryScope scope) {
Auction auction = new Auction();
auction.setEnd( new Date() );
auction.setDescription( "an auction for something" );
Bid bid = new Bid();
bid.setAmount( new BigDecimal( 123.34 ).setScale( 19, BigDecimal.ROUND_DOWN ) );
bid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
bid.setSuccessful( true );
bid.setDatetime( new Date() );
bid.setItem( auction );
@ -64,22 +62,22 @@ public class AuctionTest {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Bid b = session.getReference( Bid.class, bidId );
assertFalse( Hibernate.isInitialized( b ) );
Bid initializedBid = session.get( Bid.class, bidId );
Assert.assertSame( initializedBid, b );
Assert.assertTrue( Hibernate.isInitialized( b ) );
assertSame( initializedBid, b );
assertTrue( Hibernate.isInitialized( b ) );
}
);
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Bid b = session.getReference( Bid.class, bidId );
assertFalse( Hibernate.isInitialized( b ) );
Auction a = session.get( Auction.class, aid );
List bids = a.getBids();
List<Bid> bids = a.getBids();
assertFalse( Hibernate.isInitialized( bids ) );
Bid successfulBid = a.getSuccessfulBid();
assertTrue( Hibernate.isInitialized( successfulBid ) );
@ -96,9 +94,9 @@ public class AuctionTest {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Bid b = session.getReference( Bid.class, bidId );
assertFalse( Hibernate.isInitialized( b ) );
Auction a = (Auction) session.createQuery( "from Auction a left join fetch a.bids" ).uniqueResult();
Auction a = session.createQuery( "from Auction a left join fetch a.bids", Auction.class ).uniqueResult();
assertTrue( Hibernate.isInitialized( b ) );
assertTrue( Hibernate.isInitialized( a.getBids() ) );
assertSame( b, a.getSuccessfulBid() );
@ -109,11 +107,11 @@ public class AuctionTest {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Auction a = session.load( Auction.class, aid );
Bid b = session.getReference( Bid.class, bidId );
Auction a = session.getReference( Auction.class, aid );
assertFalse( Hibernate.isInitialized( b ) );
assertFalse( Hibernate.isInitialized( a ) );
session.createQuery( "from Auction a left join fetch a.successfulBid" ).list();
session.createQuery( "from Auction a left join fetch a.successfulBid", Auction.class ).list();
assertTrue( Hibernate.isInitialized( b ) );
assertTrue( Hibernate.isInitialized( a ) );
assertSame( b, a.getSuccessfulBid() );
@ -125,8 +123,8 @@ public class AuctionTest {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Auction a = session.load( Auction.class, aid );
Bid b = session.getReference( Bid.class, bidId );
Auction a = session.getReference( Auction.class, aid );
assertFalse( Hibernate.isInitialized( b ) );
assertFalse( Hibernate.isInitialized( a ) );
assertSame( session.get( Bid.class, bidId ), b );

View File

@ -7,6 +7,7 @@
package org.hibernate.orm.test.bidi;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import org.hibernate.Hibernate;
@ -41,7 +42,7 @@ public class AuctionTest2 {
auction.setDescription( "an auction for something" );
auction.setEnd( new Date() );
Bid bid = new Bid();
bid.setAmount( new BigDecimal( 123.34 ).setScale( 19, BigDecimal.ROUND_DOWN ) );
bid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
bid.setSuccessful( true );
bid.setDatetime( new Date() );
bid.setItem( auction );
@ -57,7 +58,7 @@ public class AuctionTest2 {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Bid b = session.getReference( Bid.class, bidId );
assertFalse( Hibernate.isInitialized( b ) );
Auction a = session.get( Auction.class, aid );
assertFalse( Hibernate.isInitialized( a.getBids() ) );
@ -71,9 +72,9 @@ public class AuctionTest2 {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Bid b = session.getReference( Bid.class, bidId );
assertFalse( Hibernate.isInitialized( b ) );
Auction a = (Auction) session.createQuery( "from Auction a left join fetch a.bids" ).uniqueResult();
Auction a = session.createQuery( "from Auction a left join fetch a.bids", Auction.class ).uniqueResult();
assertTrue( Hibernate.isInitialized( b ) );
assertTrue( Hibernate.isInitialized( a.getBids() ) );
assertSame( b, a.getSuccessfulBid() );
@ -84,11 +85,11 @@ public class AuctionTest2 {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Auction a = session.load( Auction.class, aid );
Bid b = session.getReference( Bid.class, bidId );
Auction a = session.getReference( Auction.class, aid );
assertFalse( Hibernate.isInitialized( b ) );
assertFalse( Hibernate.isInitialized( a ) );
session.createQuery( "from Auction a left join fetch a.successfulBid" ).list();
session.createQuery( "from Auction a left join fetch a.successfulBid", Auction.class ).list();
assertTrue( Hibernate.isInitialized( b ) );
assertTrue( Hibernate.isInitialized( a ) );
assertSame( b, a.getSuccessfulBid() );
@ -100,8 +101,8 @@ public class AuctionTest2 {
scope.inTransaction(
session -> {
Bid b = session.load( Bid.class, bidId );
Auction a = session.load( Auction.class, aid );
Bid b = session.getReference( Bid.class, bidId );
Auction a = session.getReference( Auction.class, aid );
assertFalse( Hibernate.isInitialized( b ) );
assertFalse( Hibernate.isInitialized( a ) );
assertSame( session.get( Bid.class, bidId ), b );

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.bidi;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Jan Schatteman
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/bidi/Auction3.hbm.xml"
)
@SessionFactory
@TestForIssue( jiraKey = {"HHH-987", "HHH-992"} )
public class AuctionWithAbstractBidClassTest {
@Test
public void testAbstractSuperClassMapping(SessionFactoryScope scope) {
Auction auction = new Auction();
auction.setEnd( new Date() );
auction.setDescription( "an auction for something" );
SpecialBid ssbid = new SpecialBid();
ssbid.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
ssbid.setSuccessful( true );
ssbid.setSpecial( false );
ssbid.setDatetime( new Date() );
ssbid.setItem( auction );
auction.getBids().add( ssbid );
auction.setSuccessfulBid( ssbid );
SpecialBid sbid = new SpecialBid();
sbid.setAmount( new BigDecimal( "321.43" ).setScale( 19, RoundingMode.DOWN ) );
sbid.setSuccessful( false );
sbid.setSpecial( true );
sbid.setDatetime( new Date() );
sbid.setItem( auction );
auction.getBids().add( sbid );
scope.inTransaction(
session ->
session.persist( auction )
);
Long auctionId = auction.getId();
Long ssbidId = ssbid.getId();
scope.inTransaction(
session -> {
Auction auc = session.get( Auction.class, auctionId );
SpecialBid successfulBid = (SpecialBid) auc.getSuccessfulBid();
assertTrue( successfulBid.isSuccessful() );
assertEquals( successfulBid.getId(), ssbidId );
}
);
}
}

View File

@ -0,0 +1,23 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
//$Id: Bid.java 5733 2005-02-14 15:56:06Z oneovthafew $
package org.hibernate.orm.test.bidi;
/**
*/
public class SpecialBid extends Bid {
private boolean isSpecial;
public boolean isSpecial() {
return isSpecial;
}
public void setSpecial(boolean isSpecial) {
this.isSpecial = isSpecial;
}
}