HHH-5233 add test on @FetchProfile and multiple @FetchOverride
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19528 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
84f0b00bc6
commit
3015c64556
|
@ -0,0 +1,22 @@
|
|||
package org.hibernate.test.annotations.fetchprofile;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="Order_Country")
|
||||
public class Country {
|
||||
@Id @GeneratedValue
|
||||
public Integer getId() { return id; }
|
||||
public void setId(Integer id) { this.id = id; }
|
||||
private Integer id;
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
private String name;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
// $Id:$
|
||||
// $Id$
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
|
@ -25,21 +25,34 @@
|
|||
*/
|
||||
package org.hibernate.test.annotations.fetchprofile;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import org.hibernate.annotations.FetchProfile;
|
||||
import org.hibernate.annotations.FetchProfiles;
|
||||
|
||||
/**
|
||||
* @author Hardy Ferentschik
|
||||
*/
|
||||
@Entity
|
||||
@FetchProfile(name = "customer-with-orders", fetchOverrides = {
|
||||
@FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN)
|
||||
@FetchProfiles( {
|
||||
@FetchProfile(name = "customer-with-orders", fetchOverrides = {
|
||||
@FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN)
|
||||
}),
|
||||
@FetchProfile(name = "customer-with-orders-and-country",
|
||||
fetchOverrides = {
|
||||
@FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN),
|
||||
//The following does not work
|
||||
//@FetchProfile.FetchOverride(entity = Customer.class, association = "orders.country", mode = FetchMode.JOIN),
|
||||
@FetchProfile.FetchOverride(entity = Customer.class, association = "lastOrder", mode = FetchMode.JOIN)
|
||||
})
|
||||
})
|
||||
public class Customer {
|
||||
@Id
|
||||
|
@ -51,7 +64,26 @@ public class Customer {
|
|||
private long customerNumber;
|
||||
|
||||
@OneToMany
|
||||
private Set<Order> orders;
|
||||
private Set<Order> orders = new HashSet<Order>();
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Order lastOrder;
|
||||
|
||||
public Order getLastOrder() {
|
||||
return lastOrder;
|
||||
}
|
||||
|
||||
public void setLastOrder(Order lastOrder) {
|
||||
this.lastOrder = lastOrder;
|
||||
}
|
||||
|
||||
public Set<SupportTickets> getTickets() {
|
||||
return tickets;
|
||||
}
|
||||
|
||||
public void setTickets(Set<SupportTickets> tickets) {
|
||||
this.tickets = tickets;
|
||||
}
|
||||
|
||||
@OneToMany
|
||||
private Set<SupportTickets> tickets;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// $Id:$
|
||||
// $Id$
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
|
@ -49,6 +49,7 @@ public class FetchProfileTest extends TestCase {
|
|||
config.addAnnotatedClass( Customer.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( SupportTickets.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
SessionFactoryImplementor sessionImpl = ( SessionFactoryImplementor ) config.buildSessionFactory();
|
||||
|
||||
assertTrue(
|
||||
|
@ -65,6 +66,7 @@ public class FetchProfileTest extends TestCase {
|
|||
AnnotationConfiguration config = new AnnotationConfiguration();
|
||||
config.addAnnotatedClass( Customer2.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
|
||||
try {
|
||||
config.buildSessionFactory();
|
||||
|
@ -79,6 +81,7 @@ public class FetchProfileTest extends TestCase {
|
|||
AnnotationConfiguration config = new AnnotationConfiguration();
|
||||
config.addAnnotatedClass( Customer3.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
|
||||
try {
|
||||
config.buildSessionFactory();
|
||||
|
@ -93,6 +96,7 @@ public class FetchProfileTest extends TestCase {
|
|||
AnnotationConfiguration config = new AnnotationConfiguration();
|
||||
config.addAnnotatedClass( Customer4.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
|
||||
try {
|
||||
config.buildSessionFactory();
|
||||
|
@ -107,6 +111,7 @@ public class FetchProfileTest extends TestCase {
|
|||
AnnotationConfiguration config = new AnnotationConfiguration();
|
||||
config.addAnnotatedClass( Customer5.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
InputStream is = Thread.currentThread()
|
||||
.getContextClassLoader()
|
||||
.getResourceAsStream( "org/hibernate/test/annotations/fetchprofile/mappings.hbm.xml" );
|
||||
|
@ -122,6 +127,7 @@ public class FetchProfileTest extends TestCase {
|
|||
config = new AnnotationConfiguration();
|
||||
config.addAnnotatedClass( Customer5.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
try {
|
||||
config.buildSessionFactory();
|
||||
fail();
|
||||
|
@ -136,6 +142,7 @@ public class FetchProfileTest extends TestCase {
|
|||
config.addAnnotatedClass( Customer.class );
|
||||
config.addAnnotatedClass( Order.class );
|
||||
config.addAnnotatedClass( SupportTickets.class );
|
||||
config.addAnnotatedClass( Country.class );
|
||||
config.addPackage( Customer.class.getPackage().getName() );
|
||||
SessionFactoryImplementor sessionImpl = ( SessionFactoryImplementor ) config.buildSessionFactory();
|
||||
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package org.hibernate.test.annotations.fetchprofile;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.junit.FailureExpected;
|
||||
import org.hibernate.test.annotations.TestCase;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
public class MoreFetchProfileTest extends TestCase{
|
||||
|
||||
@FailureExpected( jiraKey = "HHH-5233")
|
||||
public void testFetchWithTwoOverrides() throws Exception {
|
||||
Session s = openSession( );
|
||||
s.enableFetchProfile( "customer-with-orders-and-country" );
|
||||
final Transaction transaction = s.beginTransaction();
|
||||
Country ctry = new Country();
|
||||
ctry.setName( "France" );
|
||||
Order o = new Order();
|
||||
o.setCountry( ctry );
|
||||
o.setDeliveryDate( new Date() );
|
||||
o.setOrderNumber( 1 );
|
||||
Order o2 = new Order();
|
||||
o2.setCountry( ctry );
|
||||
o2.setDeliveryDate( new Date() );
|
||||
o2.setOrderNumber( 2 );
|
||||
Customer c = new Customer();
|
||||
c.setCustomerNumber( 1 );
|
||||
c.setName( "Emmanuel" );
|
||||
c.getOrders().add( o );
|
||||
c.setLastOrder( o2 );
|
||||
|
||||
s.persist( ctry );
|
||||
s.persist( o );
|
||||
s.persist( o2 );
|
||||
s.persist( c );
|
||||
|
||||
s.flush();
|
||||
|
||||
s.clear();
|
||||
|
||||
c = (Customer) s.get( Customer.class, c.getId() );
|
||||
assertTrue( Hibernate.isInitialized( c.getLastOrder() ) );
|
||||
assertTrue( Hibernate.isInitialized( c.getOrders() ) );
|
||||
for(Order so : c.getOrders() ) {
|
||||
//assertTrue( Hibernate.isInitialized( so.getCountry() ) );
|
||||
}
|
||||
final Order order = c.getOrders().iterator().next();
|
||||
c.getOrders().remove( order );
|
||||
s.delete( c );
|
||||
final Order lastOrder = c.getLastOrder();
|
||||
c.setLastOrder( null );
|
||||
s.delete( order.getCountry() );
|
||||
s.delete( lastOrder );
|
||||
s.delete( order );
|
||||
|
||||
transaction.commit();
|
||||
s.close();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Order.class,
|
||||
Country.class,
|
||||
Customer.class,
|
||||
SupportTickets.class
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
// $Id:$
|
||||
// $Id$
|
||||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
|
@ -27,13 +27,17 @@ package org.hibernate.test.annotations.fetchprofile;
|
|||
|
||||
import java.util.Date;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
* @author Hardy Ferentschik
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="C_ORDER")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
|
@ -43,6 +47,17 @@ public class Order {
|
|||
|
||||
private Date deliveryDate;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Country country;
|
||||
|
||||
public Country getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(Country country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public Date getDeliveryDate() {
|
||||
return deliveryDate;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue