diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Account.java b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Account.java new file mode 100644 index 0000000000..46c3ac83d6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Account.java @@ -0,0 +1,92 @@ +package org.hibernate.orm.test.discriminatedcollections; + +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; + +@Entity +@DiscriminatorColumn(name = "account_type") +abstract class Account { + + Account() {} + + Account(Client client) { + this.client = client; + amount = 0.0; + rate = 12.0; + } + + @Id @GeneratedValue + private Long id; + + public Long getId() { + return id; + } + + @ManyToOne(fetch = FetchType.LAZY) + private Client client; + + public Client getClient() { + return client; + } + + private double amount; + + public double getAmount() { + return amount; + } + + public void setAmount(double amount) { + this.amount = amount; + } + + private Double rate; + + public Double getRate() { + return rate; + } + + public void setRate(Double rate) { + this.rate = rate; + } + + public abstract AccountType getType(); +} + +@Entity +@DiscriminatorValue("D") +class DebitAccount extends Account { + + public DebitAccount() { + } + + public DebitAccount(Client client) { + super(client); + } + + @Override + public AccountType getType() { + return AccountType.DEBIT; + } +} + +@Entity +@DiscriminatorValue("C") +class CreditAccount extends Account { + + public CreditAccount() { + } + + public CreditAccount(Client client) { + super(client); + } + + @Override + public AccountType getType() { + return AccountType.CREDIT; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/AccountType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/AccountType.java new file mode 100644 index 0000000000..b5e4a2e440 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/AccountType.java @@ -0,0 +1,6 @@ +package org.hibernate.orm.test.discriminatedcollections; + +public enum AccountType { + DEBIT, + CREDIT +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Client.java b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Client.java new file mode 100644 index 0000000000..ff742c8ac2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/Client.java @@ -0,0 +1,48 @@ +package org.hibernate.orm.test.discriminatedcollections; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import java.util.HashSet; +import java.util.Set; + +@Entity +class Client { + + @Id @GeneratedValue + private Long id; + + public Long getId() { + return id; + } + + private String name; + + public String getName() { + return name; + } + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "client", fetch = FetchType.LAZY) + private Set debitAccounts = new HashSet<>(); + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "client", fetch = FetchType.LAZY) + private Set creditAccounts = new HashSet<>(); + + Client() {} + + Client(String name) { + this.name = name; + } + + public Set getCreditAccounts() { + return creditAccounts; + } + + public Set getDebitAccounts() { + return debitAccounts; + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/TempTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/TempTest.java new file mode 100644 index 0000000000..d01874fa96 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/discriminatedcollections/TempTest.java @@ -0,0 +1,85 @@ +/* + * 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.discriminatedcollections; + +import org.hibernate.Session; +import org.hibernate.query.criteria.JpaCriteriaQuery; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import javax.persistence.criteria.JoinType; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * @author Gavin King + */ +public class TempTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Client.class, Account.class, DebitAccount.class, CreditAccount.class }; + } + + @Test + public void test() { + Client c = new Client("Gavin"); + DebitAccount da = new DebitAccount(c); + CreditAccount ca = new CreditAccount(c); + c.getDebitAccounts().add(da); + c.getCreditAccounts().add(ca); + Session session = openSession(); + session.beginTransaction(); + session.persist(c); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + Client client = session.find(Client.class, c.getId()); + assertEquals(1, client.getDebitAccounts().size()); + assertEquals(1, client.getCreditAccounts().size()); + assertNotEquals(client.getDebitAccounts().iterator().next().getId(), + client.getCreditAccounts().iterator().next().getId()); + session.close(); + + session = openSession(); + client = session.createQuery("from Client", Client.class).getSingleResult(); + assertEquals(1, client.getDebitAccounts().size()); + assertEquals(1, client.getCreditAccounts().size()); + assertNotEquals(client.getDebitAccounts().iterator().next().getId(), + client.getCreditAccounts().iterator().next().getId()); + session.close(); + + session = openSession(); + client = session.createQuery("from Client c left join fetch c.debitAccounts", Client.class).getSingleResult(); + assertEquals(1, client.getDebitAccounts().size()); + assertEquals(1, client.getCreditAccounts().size()); + assertNotEquals(client.getDebitAccounts().iterator().next().getId(), + client.getCreditAccounts().iterator().next().getId()); + session.close(); + + session = openSession(); + client = session.createQuery("from Client c left join fetch c.debitAccounts left join fetch c.creditAccounts", Client.class).getSingleResult(); + assertEquals(1, client.getDebitAccounts().size()); + assertEquals(1, client.getCreditAccounts().size()); + assertNotEquals(client.getDebitAccounts().iterator().next().getId(), + client.getCreditAccounts().iterator().next().getId()); + session.close(); + + session = openSession(); + JpaCriteriaQuery query = sessionFactory().getCriteriaBuilder().createQuery(Client.class); + query.from(Client.class).fetch(Client_.creditAccounts, JoinType.LEFT); + client = session.createQuery(query).getSingleResult(); + assertEquals(1, client.getDebitAccounts().size()); + assertEquals(1, client.getCreditAccounts().size()); + assertNotEquals(client.getDebitAccounts().iterator().next().getId(), + client.getCreditAccounts().iterator().next().getId()); + session.close(); + } +} +