📄 astparserloadingtest.java
字号:
// this query, apparently relying on the buggy behavior somehow; thus // moved here to at least get some syntax checking... // // fyi... found and fixed the problem in the classic parser; still // leaving here for syntax checking new SyntaxChecker( "from Commento c where c.marelo.commento.mcompr is null" ).checkAll(); } public void testSpecialClassPropertyReference() { // this is a long standing bug in Hibernate when applied to joined-subclasses; // see HHH-939 for details and history new SyntaxChecker( "from Zoo zoo where zoo.class = PettingZoo" ).checkAll(); new SyntaxChecker( "select a.description from Animal a where a.class = Mammal" ).checkAll(); new SyntaxChecker( "select a.class from Animal a" ).checkAll(); new SyntaxChecker( "from DomesticAnimal an where an.class = Dog" ).checkAll(); new SyntaxChecker( "from Animal an where an.class = Dog" ).checkAll(); } public void testSpecialClassPropertyReferenceFQN() { // tests relating to HHH-2376 new SyntaxChecker( "from Zoo zoo where zoo.class = org.hibernate.test.hql.PettingZoo" ).checkAll(); new SyntaxChecker( "select a.description from Animal a where a.class = org.hibernate.test.hql.Mammal" ).checkAll(); new SyntaxChecker( "from DomesticAnimal an where an.class = org.hibernate.test.hql.Dog" ).checkAll(); new SyntaxChecker( "from Animal an where an.class = org.hibernate.test.hql.Dog" ).checkAll(); } public void testSubclassOrSuperclassPropertyReferenceInJoinedSubclass() { // this is a long standing bug in Hibernate; see HHH-1631 for details and history // // (1) pregnant is defined as a property of the class (Mammal) itself // (2) description is defined as a property of the superclass (Animal) // (3) name is defined as a property of a particular subclass (Human) new SyntaxChecker( "from Zoo z join z.mammals as m where m.name.first = 'John'" ).checkIterate(); new SyntaxChecker( "from Zoo z join z.mammals as m where m.pregnant = false" ).checkAll(); new SyntaxChecker( "select m.pregnant from Zoo z join z.mammals as m where m.pregnant = false" ).checkAll(); new SyntaxChecker( "from Zoo z join z.mammals as m where m.description = 'tabby'" ).checkAll(); new SyntaxChecker( "select m.description from Zoo z join z.mammals as m where m.description = 'tabby'" ).checkAll(); new SyntaxChecker( "from Zoo z join z.mammals as m where m.name.first = 'John'" ).checkAll(); new SyntaxChecker( "select m.name from Zoo z join z.mammals as m where m.name.first = 'John'" ).checkAll(); new SyntaxChecker( "select m.pregnant from Zoo z join z.mammals as m" ).checkAll(); new SyntaxChecker( "select m.description from Zoo z join z.mammals as m" ).checkAll(); new SyntaxChecker( "select m.name from Zoo z join z.mammals as m" ).checkAll(); new SyntaxChecker( "from DomesticAnimal da join da.owner as o where o.nickName = 'Gavin'" ).checkAll(); new SyntaxChecker( "select da.father from DomesticAnimal da join da.owner as o where o.nickName = 'Gavin'" ).checkAll(); } public void testSimpleSelectWithLimitAndOffset() throws Exception { if ( ! ( getDialect().supportsLimit() && getDialect().supportsLimitOffset() ) ) { reportSkip( "dialect does not support offset and limit combo", "limit and offset combination" ); return; } // just checking correctness of param binding code... Session session = openSession(); session.createQuery( "from Animal" ) .setFirstResult( 2 ) .setMaxResults( 1 ) .list(); session.close(); } 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(); Type[] types = s.createQuery( "select h.name from Human h" ).getReturnTypes(); assertEquals( 1, types.length ); assertTrue( types[0] instanceof ComponentType ); // 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.createQuery( "from Human h order by h.name" ).list(); s.getTransaction().commit(); s.close(); } public void testComponentParameterBinding() { // HHH-1774 : parameters are bound incorrectly with component parameters... Session s = openSession(); s.beginTransaction(); Order.Id oId = new Order.Id( "1234", 1 ); // control s.createQuery("from Order o where o.customer.name =:name and o.id = :id") .setParameter( "name", "oracle" ) .setParameter( "id", oId ) .list(); // this is the form that caused problems in the original case... s.createQuery("from Order o where o.id = :id and o.customer.name =:name ") .setParameter( "id", oId ) .setParameter( "name", "oracle" ) .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 testFetchInSubqueryFails() { 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();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -