From fafd894f3b6dc8f1fa17abe7ca0f6c1deadcb839 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Wed, 28 Sep 2022 00:25:43 +0200 Subject: [PATCH] HHH-1134 - tests to verify this use case Signed-off-by: Jan Schatteman --- .../discriminator/AbstractAccount.java | 44 ++++ .../discriminator/AccountOwner.hbm.xml | 54 +++++ .../discriminator/AccountOwner.java | 54 +++++ .../discriminator/CreditAccount.java | 15 ++ .../discriminator/DebitAccount.java | 15 ++ .../MultiInheritanceDiscriminatorTest.java | 225 ++++++++++++++++++ 6 files changed, 407 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AbstractAccount.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/CreditAccount.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/DebitAccount.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/MultiInheritanceDiscriminatorTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AbstractAccount.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AbstractAccount.java new file mode 100644 index 0000000000..e552e01879 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AbstractAccount.java @@ -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 . + */ + +//$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; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.hbm.xml new file mode 100644 index 0000000000..30f63dc57c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.hbm.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.java new file mode 100644 index 0000000000..341b258718 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/AccountOwner.java @@ -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 . + */ + +//$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 creditAccounts = new HashSet<>(); + private Set 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 getCreditAccounts() { + return creditAccounts; + } + + public void setCreditAccounts(Set creditAccounts) { + this.creditAccounts = creditAccounts; + } + + public Set getDebitAccounts() { + return debitAccounts; + } + + public void setDebitAccounts(Set debitAccounts) { + this.debitAccounts = debitAccounts; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/CreditAccount.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/CreditAccount.java new file mode 100644 index 0000000000..cc269acc88 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/CreditAccount.java @@ -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 . + */ + +//$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 { +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/DebitAccount.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/DebitAccount.java new file mode 100644 index 0000000000..68764ac677 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/DebitAccount.java @@ -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 . + */ + +//$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 { +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/MultiInheritanceDiscriminatorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/MultiInheritanceDiscriminatorTest.java new file mode 100644 index 0000000000..791e17badd --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/inheritance/discriminator/MultiInheritanceDiscriminatorTest.java @@ -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 . + */ +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 creditAccs = new HashSet<>(); + + @OneToMany(targetEntity = DebitAcc.class, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "owner") + private Set 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 getCreditAccs() { + return creditAccs; + } + + public void setCreditAccs(Set creditAccs) { + this.creditAccs = creditAccs; + } + + public Set getDebitAccs() { + return debitAccs; + } + + public void setDebitAccs(Set 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 { + } + +}