HHH-1134 - tests to verify this use case

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2022-09-28 00:25:43 +02:00 committed by Jan Schatteman
parent 8de46167f6
commit fafd894f3b
6 changed files with 407 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* 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.mapping.inheritance.discriminator;
import java.math.BigDecimal;
/**
* @author Jan Schatteman
*/
public abstract class AbstractAccount {
private Long id;
private AccountOwner owner;
private BigDecimal amount;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public AccountOwner getOwner() {
return owner;
}
public void setOwner(AccountOwner owner) {
this.owner = owner;
}
}

View File

@ -0,0 +1,54 @@
<?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.mapping.inheritance.discriminator">
<class name="AccountOwner" table="AccOwner">
<id name="id">
<generator class="native"/>
</id>
<property name="description"/>
<set name="creditAccounts"
cascade="persist">
<key column="ownerId"/>
<one-to-many class="CreditAccount"/>
</set>
<set name="debitAccounts"
cascade="persist">
<key column="ownerId"/>
<one-to-many class="DebitAccount"/>
</set>
</class>
<class name="AbstractAccount" table="Accounts" abstract="true" dynamic-update="true">
<id name="id">
<generator class="native"/>
</id>
<discriminator column="DISC" insert="true" type="java.lang.String" not-null="true"/>
<property name="amount"
scale="19"
precision="31" />
<many-to-one name="owner"
column="ownerId"
cascade="persist"/>
<subclass name="CreditAccount" discriminator-value="C" dynamic-insert="true" dynamic-update="true"/>
<subclass name="DebitAccount" discriminator-value="D" dynamic-insert="true" dynamic-update="true"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,54 @@
/*
* 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: Auction.java 5733 2005-02-14 15:56:06Z oneovthafew $
package org.hibernate.orm.test.mapping.inheritance.discriminator;
import java.util.HashSet;
import java.util.Set;
/**
* @author Jan Schatteman
*/
public class AccountOwner {
private Long id;
private String description;
private Set<AbstractAccount> creditAccounts = new HashSet<>();
private Set<AbstractAccount> debitAccounts = new HashSet<>();
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<AbstractAccount> getCreditAccounts() {
return creditAccounts;
}
public void setCreditAccounts(Set<AbstractAccount> creditAccounts) {
this.creditAccounts = creditAccounts;
}
public Set<AbstractAccount> getDebitAccounts() {
return debitAccounts;
}
public void setDebitAccounts(Set<AbstractAccount> debitAccounts) {
this.debitAccounts = debitAccounts;
}
}

View File

@ -0,0 +1,15 @@
/*
* 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.mapping.inheritance.discriminator;
/**
* @author Jan Schatteman
*/
public class CreditAccount extends AbstractAccount {
}

View File

@ -0,0 +1,15 @@
/*
* 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.mapping.inheritance.discriminator;
/**
* @author Jan Schatteman
*/
public class DebitAccount extends AbstractAccount {
}

View File

@ -0,0 +1,225 @@
/*
* 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.mapping.inheritance.discriminator;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.Set;
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 jakarta.persistence.CascadeType;
import jakarta.persistence.DiscriminatorColumn;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Jan Schatteman
*/
@TestForIssue( jiraKey = {"HHH-1134"} )
public class MultiInheritanceDiscriminatorTest {
@DomainModel(
xmlMappings = "org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.hbm.xml"
)
@SessionFactory
@Test
public void testAbstractSuperClassMappingHbm(SessionFactoryScope scope) {
AccountOwner owner = new AccountOwner();
owner.setDescription( "Some account" );
CreditAccount cAcc1 = new CreditAccount();
cAcc1.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
cAcc1.setOwner( owner );
owner.getCreditAccounts().add( cAcc1 );
CreditAccount cAcc2 = new CreditAccount();
cAcc2.setAmount( new BigDecimal( "321.43" ).setScale( 19, RoundingMode.DOWN ) );
cAcc2.setOwner( owner );
owner.getCreditAccounts().add( cAcc2 );
DebitAccount dAcc = new DebitAccount();
dAcc.setAmount( new BigDecimal( "654.99" ).setScale( 19, RoundingMode.DOWN ) );
dAcc.setOwner( owner );
owner.getDebitAccounts().add( dAcc );
scope.inTransaction(
session ->
session.persist( owner )
);
Long ownerId = owner.getId();
scope.inTransaction(
session -> {
AccountOwner _owner = session.get( AccountOwner.class, ownerId );
assertEquals( 2, _owner.getCreditAccounts().size() );
assertEquals( "CreditAccount", _owner.getCreditAccounts().iterator().next().getClass().getSimpleName() );
assertEquals( 1, _owner.getDebitAccounts().size() );
assertEquals( "DebitAccount", _owner.getDebitAccounts().iterator().next().getClass().getSimpleName() );
}
);
}
@DomainModel(
annotatedClasses = {
AbstractAcc.class, CreditAcc.class, DebitAcc.class, AccOwner.class
}
)
@SessionFactory
@Test
public void testAbstractSuperClassMappingAnn(SessionFactoryScope scope) {
AccOwner owner = new AccOwner();
owner.setName( "Some account" );
CreditAcc cAcc1 = new CreditAcc();
cAcc1.setAmount( new BigDecimal( "123.34" ).setScale( 19, RoundingMode.DOWN ) );
cAcc1.setOwner( owner );
owner.getCreditAccs().add( cAcc1 );
CreditAcc cAcc2 = new CreditAcc();
cAcc2.setAmount( new BigDecimal( "321.43" ).setScale( 19, RoundingMode.DOWN ) );
cAcc2.setOwner( owner );
owner.getCreditAccs().add( cAcc2 );
DebitAcc dAcc = new DebitAcc();
dAcc.setAmount( new BigDecimal( "654.99" ).setScale( 19, RoundingMode.DOWN ) );
dAcc.setOwner( owner );
owner.getDebitAccs().add( dAcc );
scope.inTransaction(
session ->
session.persist( owner )
);
Long ownerId = owner.getId();
scope.inTransaction(
session -> {
AccOwner _owner = session.get( AccOwner.class, ownerId );
assertEquals( 2, _owner.getCreditAccs().size() );
assertEquals( "CreditAcc", _owner.getCreditAccs().iterator().next().getClass().getSimpleName() );
assertEquals( 1, _owner.getDebitAccs().size() );
assertEquals( "DebitAcc", _owner.getDebitAccs().iterator().next().getClass().getSimpleName() );
}
);
}
@Entity
@Table(name = "AccOwner")
public static class AccOwner {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany(targetEntity = CreditAcc.class, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "owner")
private Set<AbstractAcc> creditAccs = new HashSet<>();
@OneToMany(targetEntity = DebitAcc.class, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "owner")
private Set<AbstractAcc> debitAccs = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<AbstractAcc> getCreditAccs() {
return creditAccs;
}
public void setCreditAccs(Set<AbstractAcc> creditAccs) {
this.creditAccs = creditAccs;
}
public Set<AbstractAcc> getDebitAccs() {
return debitAccs;
}
public void setDebitAccs(Set<AbstractAcc> debitAccs) {
this.debitAccs = debitAccs;
}
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISC")
@Table(name = "Accounts")
public static abstract class AbstractAcc {
@Id
@GeneratedValue
private Long id;
private BigDecimal amount;
@ManyToOne
private AccOwner owner;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public AccOwner getOwner() {
return owner;
}
public void setOwner(AccOwner owner) {
this.owner = owner;
}
}
@Entity
@DiscriminatorValue("CA")
public static class CreditAcc extends AbstractAcc {
}
@Entity
@DiscriminatorValue("DA")
public static class DebitAcc extends AbstractAcc {
}
}