📄 dynamicfiltertest.java
字号:
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
}
public void testManyToManyOnCollectionLoadAfterHQL() {
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
// Force the categories to not get initialized here
List result = session.createQuery( "from Product as p where p.id = :id" )
.setLong( "id", testData.prod1Id.longValue() )
.list();
assertTrue( "No products returned from HQL", !result.isEmpty() );
Product prod = ( Product ) result.get( 0 );
assertNotNull( prod );
assertEquals( "Incorrect Product.categories count for filter on collection load", 1, prod.getCategories().size() );
session.close();
testData.release();
}
public void testManyToManyFilterOnQuery() {
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
session.enableFilter( "effectiveDate" ).setParameter( "asOfDate", new Date() );
List result = session.createQuery( "from Product p inner join fetch p.categories" ).list();
assertTrue( "No products returned from HQL many-to-many filter case", !result.isEmpty() );
Product prod = ( Product ) result.get( 0 );
assertNotNull( prod );
assertEquals( "Incorrect Product.categories count for filter with HQL", 1, prod.getCategories().size() );
session.close();
testData.release();
}
public void testManyToManyBase() {
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
Product prod = ( Product ) session.get( Product.class, testData.prod1Id );
long initLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long initFetchCount = getSessions().getStatistics().getCollectionFetchCount();
// should already have been initialized...
int size = prod.getCategories().size();
assertEquals( "Incorrect non-filtered collection count", 2, size );
long currLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long currFetchCount = getSessions().getStatistics().getCollectionFetchCount();
assertTrue(
"load with join fetch of many-to-many did not trigger join fetch",
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
);
// make sure we did not get back a collection of proxies
long initEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
Iterator itr = prod.getCategories().iterator();
while ( itr.hasNext() ) {
Category cat = ( Category ) itr.next();
System.out.println( " ===> " + cat.getName() );
}
long currEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
}
public void testManyToManyBaseThruCriteria() {
TestData testData = new TestData();
testData.prepare();
Session session = openSession();
List result = session.createCriteria( Product.class )
.add( Expression.eq( "id", testData.prod1Id ) )
.list();
Product prod = ( Product ) result.get( 0 );
long initLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long initFetchCount = getSessions().getStatistics().getCollectionFetchCount();
// should already have been initialized...
int size = prod.getCategories().size();
assertEquals( "Incorrect non-filtered collection count", 2, size );
long currLoadCount = getSessions().getStatistics().getCollectionLoadCount();
long currFetchCount = getSessions().getStatistics().getCollectionFetchCount();
assertTrue(
"load with join fetch of many-to-many did not trigger join fetch",
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
);
// make sure we did not get back a collection of proxies
long initEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
Iterator itr = prod.getCategories().iterator();
while ( itr.hasNext() ) {
Category cat = ( Category ) itr.next();
System.out.println( " ===> " + cat.getName() );
}
long currEntityLoadCount = getSessions().getStatistics().getEntityLoadCount();
assertTrue(
"load with join fetch of many-to-many did not trigger *complete* join fetch",
( initEntityLoadCount == currEntityLoadCount )
);
session.close();
testData.release();
}
/**
* Define the mappings needed for these tests.
*
* @return Mappings for these tests.
*/
protected String[] getMappings() {
return new String[]{
"filter/defs.hbm.xml",
"filter/LineItem.hbm.xml",
"filter/Order.hbm.xml",
"filter/Product.hbm.xml",
"filter/Salesperson.hbm.xml",
"filter/Department.hbm.xml",
"filter/Category.hbm.xml"
};
}
protected void configure(Configuration cfg) {
cfg.setProperty( Environment.MAX_FETCH_DEPTH, "1" );
// cfg.setProperty( Environment.MAX_FETCH_DEPTH, "2" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
}
public static Test suite() {
return new TestSuite( DynamicFilterTest.class );
}
private class TestData {
private Long steveId;
private Long deptId;
private Long prod1Id;
private Calendar lastMonth;
private Calendar nextMonth;
private Calendar sixMonthsAgo;
private Calendar fourMonthsAgo;
private List entitiesToCleanUp = new ArrayList();
private void prepare() {
Session session = openSession();
Transaction transaction = session.beginTransaction();
lastMonth = new GregorianCalendar();
lastMonth.add( Calendar.MONTH, -1 );
nextMonth = new GregorianCalendar();
nextMonth.add( Calendar.MONTH, 1 );
sixMonthsAgo = new GregorianCalendar();
sixMonthsAgo.add( Calendar.MONTH, -6 );
fourMonthsAgo = new GregorianCalendar();
fourMonthsAgo.add( Calendar.MONTH, -4 );
Department dept = new Department();
dept.setName( "Sales" );
session.save( dept );
deptId = dept.getId();
entitiesToCleanUp.add( dept );
Salesperson steve = new Salesperson();
steve.setName( "steve" );
steve.setRegion( "APAC" );
steve.setHireDate( sixMonthsAgo.getTime() );
steve.setDepartment( dept );
dept.getSalespersons().add( steve );
Salesperson max = new Salesperson();
max.setName( "max" );
max.setRegion( "EMEA" );
max.setHireDate( nextMonth.getTime() );
max.setDepartment( dept );
dept.getSalespersons().add( max );
session.save( steve );
session.save( max );
entitiesToCleanUp.add( steve );
entitiesToCleanUp.add( max );
steveId = steve.getId();
Category cat1 = new Category( "test cat 1", lastMonth.getTime(), nextMonth.getTime() );
Category cat2 = new Category( "test cat 2", sixMonthsAgo.getTime(), fourMonthsAgo.getTime() );
Product product1 = new Product();
product1.setName( "Acme Hair Gel" );
product1.setStockNumber( 123 );
product1.setEffectiveStartDate( lastMonth.getTime() );
product1.setEffectiveEndDate( nextMonth.getTime() );
product1.addCategory( cat1 );
product1.addCategory( cat2 );
session.save( product1 );
entitiesToCleanUp.add( product1 );
prod1Id = product1.getId();
Order order1 = new Order();
order1.setBuyer( "gavin" );
order1.setRegion( "APAC" );
order1.setPlacementDate( sixMonthsAgo.getTime() );
order1.setFulfillmentDate( fourMonthsAgo.getTime() );
order1.setSalesperson( steve );
order1.addLineItem( product1, 500 );
session.save( order1 );
entitiesToCleanUp.add( order1 );
Product product2 = new Product();
product2.setName( "Acme Super-Duper DTO Factory" );
product2.setStockNumber( 124 );
product2.setEffectiveStartDate( sixMonthsAgo.getTime() );
product2.setEffectiveEndDate( new Date() );
Category cat3 = new Category( "test cat 2", sixMonthsAgo.getTime(), new Date() );
product2.addCategory( cat3 );
session.save( product2 );
entitiesToCleanUp.add( product2 );
// An uncategorized product
Product product3 = new Product();
product3.setName( "Uncategorized product" );
session.save( product3 );
entitiesToCleanUp.add( product3 );
Order order2 = new Order();
order2.setBuyer( "christian" );
order2.setRegion( "EMEA" );
order2.setPlacementDate( lastMonth.getTime() );
order2.setSalesperson( steve );
order2.addLineItem( product2, -1 );
session.save( order2 );
entitiesToCleanUp.add( order2 );
transaction.commit();
session.close();
}
private void release() {
Session session = openSession();
Transaction transaction = session.beginTransaction();
Iterator itr = entitiesToCleanUp.iterator();
while ( itr.hasNext() ) {
session.delete( itr.next() );
}
transaction.commit();
session.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -