test for filtering @OneToMany associations by discriminator
see https://github.com/eclipse-ee4j/jpa-api/issues/39#issuecomment-830112571
This commit is contained in:
parent
3bc890dc48
commit
8ce2098deb
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package org.hibernate.orm.test.discriminatedcollections;
|
||||
|
||||
public enum AccountType {
|
||||
DEBIT,
|
||||
CREDIT
|
||||
}
|
|
@ -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<DebitAccount> debitAccounts = new HashSet<>();
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "client", fetch = FetchType.LAZY)
|
||||
private Set<CreditAccount> creditAccounts = new HashSet<>();
|
||||
|
||||
Client() {}
|
||||
|
||||
Client(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<CreditAccount> getCreditAccounts() {
|
||||
return creditAccounts;
|
||||
}
|
||||
|
||||
public Set<DebitAccount> getDebitAccounts() {
|
||||
return debitAccounts;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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<Client> 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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue