astparserloadingtest.java
来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 1,536 行 · 第 1/4 页
JAVA
1,536 行
// $Id: ASTParserLoadingTest.java 10138 2006-07-24 10:52:04Z max.andersen@jboss.com $
package org.hibernate.test.hql;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.QueryException;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.TypeMismatchException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.Oracle9Dialect;
import org.hibernate.stat.QueryStatistics;
import org.hibernate.test.TestCase;
import org.hibernate.test.any.PropertyValue;
import org.hibernate.test.any.StringPropertyValue;
import org.hibernate.test.any.IntegerPropertyValue;
import org.hibernate.test.any.PropertySet;
import org.hibernate.test.cid.Customer;
import org.hibernate.test.cid.LineItem;
import org.hibernate.test.cid.Order;
import org.hibernate.test.cid.Product;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.Type;
import org.hibernate.util.StringHelper;
/**
* Tests the integration of the new AST parser into the loading of query results using
* the Hibernate persisters and loaders.
* <p/>
* Also used to test the syntax of the resulting sql against the underlying
* database, specifically for functionality not supported by the classic
* parser.
*
* @author Steve
*/
public class ASTParserLoadingTest extends TestCase {
public ASTParserLoadingTest(String name) {
super( name );
}
private List createdAnimalIds = new ArrayList();
public static Test suite() {
return new TestSuite( ASTParserLoadingTest.class );
}
protected String[] getMappings() {
// Make sure we are using the new AST parser translator...
System.setProperty( Environment.QUERY_TRANSLATOR, "org.hibernate.hql.ast.ASTQueryTranslatorFactory" );
return new String[] {
"hql/Animal.hbm.xml",
"hql/FooBarCopy.hbm.xml",
"batchfetch/ProductLine.hbm.xml",
"cid/Customer.hbm.xml",
"cid/Order.hbm.xml",
"cid/LineItem.hbm.xml",
"cid/Product.hbm.xml",
"any/Properties.hbm.xml"
};
}
protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
public void testJPAPositionalParameterList() {
Session s = openSession();
s.beginTransaction();
ArrayList params = new ArrayList();
params.add( "Doe" );
params.add( "Public" );
s.createQuery( "from Human where name.last in (?1)" )
.setParameterList( "1", params )
.list();
s.getTransaction().commit();
s.close();
}
public void testComponentQueries() {
Session s = openSession();
s.beginTransaction();
// Test the ability to perform comparisions between component values
s.createQuery( "from Human h where h.name = h.name" ).list();
s.createQuery( "from Human h where h.name = :name" ).setParameter( "name", new Name() ).list();
s.createQuery( "from Human where name = :name" ).setParameter( "name", new Name() ).list();
s.createQuery( "from Human h where :name = h.name" ).setParameter( "name", new Name() ).list();
s.createQuery( "from Human h where :name <> h.name" ).setParameter( "name", new Name() ).list();
// Test the ability to perform comparisions between a component and an explicit row-value
s.createQuery( "from Human h where h.name = ('John', 'X', 'Doe')" ).list();
s.createQuery( "from Human h where ('John', 'X', 'Doe') = h.name" ).list();
s.createQuery( "from Human h where ('John', 'X', 'Doe') <> h.name" ).list();
s.createQuery( "from Human h where ('John', 'X', 'Doe') >= h.name" ).list();
s.getTransaction().commit();
s.close();
}
public void testAnyMappingReference() {
Session s = openSession();
s.beginTransaction();
PropertyValue redValue = new StringPropertyValue( "red" );
PropertyValue lonliestNumberValue = new IntegerPropertyValue( 1 );
Long id;
PropertySet ps = new PropertySet( "my properties" );
ps.setSomeSpecificProperty( redValue );
ps.getGeneralProperties().put( "the lonliest number", lonliestNumberValue );
ps.getGeneralProperties().put( "i like", new StringPropertyValue( "pina coladas" ) );
ps.getGeneralProperties().put( "i also like", new StringPropertyValue( "getting caught in the rain" ) );
s.save( ps );
s.getTransaction().commit();
id = ps.getId();
s.clear();
s.beginTransaction();
// TODO : setEntity() currently will not work here, but that would be *very* nice
// does not work because the corresponding EntityType is then used as the "bind type" rather
// than the "discovered" AnyType...
s.createQuery( "from PropertySet p where p.someSpecificProperty = :ssp" ).setParameter( "ssp", redValue ).list();
s.createQuery( "from PropertySet p where p.someSpecificProperty.id is not null" ).list();
s.createQuery( "from PropertySet p join p.generalProperties gp where gp.id is not null" ).list();
s.delete( s.load( PropertySet.class, id ) );
s.getTransaction().commit();
s.close();
}
public void testJdkEnumStyleEnumConstant() throws Exception {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Zoo z where z.classification = org.hibernate.test.hql.Classification.LAME" ).list();
s.getTransaction().commit();
s.close();
}
public void testParameterTypeMismatchFailureExpected() {
Session s = openSession();
s.beginTransaction();
Query query = s.createQuery( "from Animal a where a.description = :nonstring" )
.setParameter( "nonstring", new Integer(1) );
try {
query.list();
fail( "query execution should have failed" );
}
catch( TypeMismatchException tme ) {
// expected behavior
}
s.getTransaction().commit();
s.close();
}
public void testMultipleBagFetchesFail() {
Session s = openSession();
s.beginTransaction();
try {
s.createQuery( "from Human h join fetch h.friends f join fetch f.friends fof" ).list();
fail( "failure expected" );
}
catch( HibernateException e ) {
assertTrue( "unexpected failure reason : " + e, e.getMessage().indexOf( "multiple bags" ) > 0 );
}
s.getTransaction().commit();
s.close();
}
public void testCollectionJoinsInSubselect() {
// HHH-1248 : initially FromElementFactory treated any explicit join
// as an implied join so that theta-style joins would always be used.
// This was because correlated subqueries cannot use ANSI-style joins
// for the correlation. However, this special treatment was not limited
// to only correlated subqueries; it was applied to any subqueries ->
// which in-and-of-itself is not necessarily bad. But somewhere later
// the choices made there caused joins to be dropped.
Session s = openSession();
String qryString =
"select a.id, a.description" +
" from Animal a" +
" left join a.offspring" +
" where a in (" +
" select a1 from Animal a1" +
" left join a1.offspring o" +
" where a1.id=1" +
")";
s.createQuery( qryString ).list();
qryString =
"select h.id, h.description" +
" from Human h" +
" left join h.friends" +
" where h in (" +
" select h1" +
" from Human h1" +
" left join h1.friends f" +
" where h1.id=1" +
")";
s.createQuery( qryString ).list();
qryString =
"select h.id, h.description" +
" from Human h" +
" left join h.friends f" +
" where f in (" +
" select h1" +
" from Human h1" +
" left join h1.friends f1" +
" where h = f1" +
")";
s.createQuery( qryString ).list();
s.close();
}
public void testCollectionFetchWithDistinctionAndLimit() {
// create some test data...
Session s = openSession();
Transaction t = s.beginTransaction();
int parentCount = 30;
for ( int i = 0; i < parentCount; i++ ) {
Animal child1 = new Animal();
child1.setDescription( "collection fetch distinction (child1 - parent" + i + ")" );
s.persist( child1 );
Animal child2 = new Animal();
child2.setDescription( "collection fetch distinction (child2 - parent " + i + ")" );
s.persist( child2 );
Animal parent = new Animal();
parent.setDescription( "collection fetch distinction (parent" + i + ")" );
parent.setSerialNumber( "123-" + i );
parent.addOffspring( child1 );
parent.addOffspring( child2 );
s.persist( parent );
}
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
// Test simple distinction
List results;
results = s.createQuery( "select distinct p from Animal p inner join fetch p.offspring" ).list();
assertEquals( "duplicate list() returns", 30, results.size() );
// Test first/max
results = s.createQuery( "select p from Animal p inner join fetch p.offspring order by p.id" )
.setFirstResult( 5 )
.setMaxResults( 20 )
.list();
assertEquals( "duplicate returns", 20, results.size() );
Animal firstReturn = ( Animal ) results.get( 0 );
assertEquals( "firstResult not applied correctly", "123-5", firstReturn.getSerialNumber() );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete Animal where mother is not null" ).executeUpdate();
s.createQuery( "delete Animal" ).executeUpdate();
t.commit();
s.close();
}
public void testFetchInSubqueryFailureExpected() {
Session s = openSession();
try {
s.createQuery( "from Animal a where a.mother in (select m from Animal a1 inner join a1.mother as m join fetch m.mother)" ).list();
fail( "fetch join allowed in subquery" );
}
catch( QueryException expected ) {
// expected behavior
}
s.close();
}
public void testQueryMetadataRetrievalWithFetching() {
// HHH-1464 : there was a problem due to the fact they we polled
// the shallow version of the query plan to get the metadata.
Session s = openSession();
Query query = s.createQuery( "from Animal a inner join fetch a.mother" );
assertEquals( 1, query.getReturnTypes().length );
assertNull( query.getReturnAliases() );
s.close();
}
public void testSuperclassPropertyReferenceAfterCollectionIndexedAccess() {
// note: simply performing syntax checking in the db
// test for HHH-429
Session s = openSession();
s.beginTransaction();
Mammal tiger = new Mammal();
tiger.setDescription( "Tiger" );
s.persist( tiger );
Mammal mother = new Mammal();
mother.setDescription( "Tiger's mother" );
mother.setBodyWeight( 4.0f );
mother.addOffspring( tiger );
s.persist( mother );
Zoo zoo = new Zoo();
zoo.setName( "Austin Zoo" );
zoo.setMammals( new HashMap() );
zoo.getMammals().put( "tiger", tiger );
s.persist( zoo );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
List results = s.createQuery( "from Zoo zoo where zoo.mammals['tiger'].mother.bodyWeight > 3.0f" ).list();
assertEquals( 1, results.size() );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.delete( tiger );
s.delete( mother );
s.delete( zoo );
s.getTransaction().commit();
s.close();
}
public void testJoinFetchCollectionOfValues() {
// note: simply performing syntax checking in the db
Session s = openSession();
s.beginTransaction();
s.createQuery( "select h from Human as h join fetch h.nickNames" ).list();
s.getTransaction().commit();
s.close();
}
public void testIntegerLiterals() {
// note: simply performing syntax checking in the db
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Foo where long = 1" ).list();
s.createQuery( "from Foo where long = " + Integer.MIN_VALUE ).list();
s.createQuery( "from Foo where long = " + Integer.MAX_VALUE ).list();
s.createQuery( "from Foo where long = 1L" ).list();
s.createQuery( "from Foo where long = " + (Long.MIN_VALUE + 1) + "L" ).list();
s.createQuery( "from Foo where long = " + Long.MAX_VALUE + "L" ).list();
s.createQuery( "from Foo where integer = " + (Long.MIN_VALUE + 1) ).list();
// currently fails due to HHH-1387
// s.createQuery( "from Foo where long = " + Long.MIN_VALUE ).list();
s.getTransaction().commit();
s.close();
}
public void testDecimalLiterals() {
// note: simply performing syntax checking in the db
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Animal where bodyWeight > 100.0e-10" ).list();
s.createQuery( "from Animal where bodyWeight > 100.0E-10" ).list();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?