astparserloadingtest.java
来自「hibernate-3.0.5 中文文档」· Java 代码 · 共 673 行 · 第 1/2 页
JAVA
673 行
// $Id: ASTParserLoadingTest.java,v 1.28 2005/05/18 23:45:36 oneovthafew Exp $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.Iterator;import java.util.List;import java.util.Map;import junit.framework.Test;import junit.framework.TestSuite;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.cfg.Environment;import org.hibernate.dialect.DB2Dialect;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.test.TestCase;import org.hibernate.test.cid.Customer;import org.hibernate.test.cid.LineItem;import org.hibernate.test.cid.Order;import org.hibernate.test.cid.Product;/** * Tests the integration of the new AST parser into the loading of query results using * the Hibernate persisters and loaders. * * @author Steve */public class ASTParserLoadingTest extends TestCase { public ASTParserLoadingTest(String name) { super( name ); } private List createdAnimalIds = new ArrayList(); 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", "batchfetch/ProductLine.hbm.xml", "cid/Customer.hbm.xml", "cid/Order.hbm.xml", "cid/LineItem.hbm.xml", "cid/Product.hbm.xml" }; } public void testAliases() { Session s = openSession(); Transaction t = s.beginTransaction(); Animal a = new Animal(); a.setBodyWeight(12.4f); a.setDescription("an animal"); s.persist(a); String[] aliases1 = s.createQuery("select a.bodyWeight as abw, a.description from Animal a").getReturnAliases(); assertEquals(aliases1[0], "abw"); assertEquals(aliases1[1], "1"); String[] aliases2 = s.createQuery("select count(*), avg(a.bodyWeight) as avg from Animal a").getReturnAliases(); assertEquals(aliases2[0], "0"); assertEquals(aliases2[1], "avg"); s.delete(a); t.commit(); s.close(); } public void testIndexParams() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery("from Zoo zoo where zoo.mammals[:name] = :id") .setParameter("name", "Walrus") .setParameter("id", new Long(123)) .list(); s.createQuery("from Zoo zoo where zoo.mammals[:name].bodyWeight > :w") .setParameter("name", "Walrus") .setParameter("w", new Float(123.32)) .list(); s.createQuery("from Zoo zoo where zoo.animals[:sn].mother.bodyWeight < :mw") .setParameter("sn", "ant-123") .setParameter("mw", new Float(23.32)) .list(); /*s.createQuery("from Zoo zoo where zoo.animals[:sn].description like :desc and zoo.animals[:sn].bodyWeight > :wmin and zoo.animals[:sn].bodyWeight < :wmax") .setParameter("sn", "ant-123") .setParameter("desc", "%big%") .setParameter("wmin", new Float(123.32)) .setParameter("wmax", new Float(167.89)) .list();*/ /*s.createQuery("from Human where addresses[:type].city = :city and addresses[:type].country = :country") .setParameter("type", "home") .setParameter("city", "Melbourne") .setParameter("country", "Australia") .list();*/ t.commit(); s.close(); } public void testAggregation() { Session s = openSession(); Transaction t = s.beginTransaction(); Human h = new Human(); h.setBodyWeight( (float) 74.0 ); h.setHeight(120.5); h.setDescription("Me"); h.setName( new Name("Gavin", 'A', "King") ); h.setNickName("Oney"); s.persist(h); Float sum = (Float) s.createQuery("select sum(h.bodyWeight) from Human h").uniqueResult(); Double avg = (Double) s.createQuery("select avg(h.height) from Human h").uniqueResult(); assertEquals(sum.floatValue(), 74.0, 0.01); assertEquals(avg.doubleValue(), 120.5, 0.01); s.delete(h); t.commit(); s.close(); } public void testSelectClauseCase() { Session s = openSession(); Transaction t = s.beginTransaction(); Human h = new Human(); h.setBodyWeight( (float) 74.0 ); h.setHeight(120.5); h.setDescription("Me"); h.setName( new Name("Gavin", 'A', "King") ); h.setNickName("Oney"); s.persist(h); String name = (String) s.createQuery("select case nickName when 'Oney' then 'gavin' when 'Turin' then 'christian' else nickName end from Human").uniqueResult(); assertEquals(name, "gavin"); String result = (String) s.createQuery("select case when bodyWeight > 100 then 'fat' else 'skinny' end from Human").uniqueResult(); assertEquals(result, "skinny"); s.delete(h); t.commit(); s.close(); } public void testImplicitPolymorphism() { Session s = openSession(); Transaction t = s.beginTransaction(); Product product = new Product(); product.setDescription( "My Product" ); product.setNumberAvailable( 10 ); product.setPrice( new BigDecimal( 123 ) ); product.setProductId( "4321" ); s.save( product ); List list = s.createQuery("from java.lang.Comparable").list(); assertEquals( list.size(), 0 ); list = s.createQuery("from java.lang.Object").list(); assertEquals( list.size(), 1 ); s.delete(product); list = s.createQuery("from java.lang.Object").list(); assertEquals( list.size(), 0 ); t.commit(); s.close(); } public void testCoalesce() { Session session = openSession(); Transaction txn = session.beginTransaction(); session.createQuery("from Human h where coalesce(h.nickName, h.name.first, h.name.last) = 'max'").list(); session.createQuery("select nullif(nickName, '1e1') from Human").list(); txn.commit(); session.close(); } public void testCast() { if ( (getDialect() instanceof MySQLDialect) || (getDialect() instanceof DB2Dialect) ) return; Session session = openSession(); Transaction txn = session.beginTransaction(); session.createQuery("from Human h where h.nickName like 'G%'").list(); session.createQuery("from Animal a where cast(a.bodyWeight as string) like '1.%'").list(); session.createQuery("from Animal a where cast(a.bodyWeight as integer) = 1").list(); txn.commit(); session.close(); } public void testExtract() { Session session = openSession(); Transaction txn = session.beginTransaction(); session.createQuery("select second(current_timestamp()), minute(current_timestamp()), hour(current_timestamp()) from Mammal m").list(); session.createQuery("select day(m.birthdate), month(m.birthdate), year(m.birthdate) from Mammal m").list(); if ( !(getDialect() instanceof DB2Dialect) ) { //no ANSI extract session.createQuery("select extract(second from current_timestamp()), extract(minute from current_timestamp()), extract(hour from current_timestamp()) from Mammal m").list(); session.createQuery("select extract(day from m.birthdate), extract(month from m.birthdate), extract(year from m.birthdate) from Mammal m").list(); } txn.commit(); session.close(); } public void testOneToManyFilter() throws Throwable { Session session = openSession(); Transaction txn = session.beginTransaction(); Product product = new Product(); product.setDescription( "My Product" ); product.setNumberAvailable( 10 ); product.setPrice( new BigDecimal( 123 ) ); product.setProductId( "4321" ); session.save( product ); Customer customer = new Customer(); customer.setCustomerId( "123456789" ); customer.setName( "My customer" ); customer.setAddress( "somewhere" ); session.save( customer ); Order order = customer.generateNewOrder( new BigDecimal( 1234 ) ); session.save( order ); LineItem li = order.generateLineItem( product, 5 ); session.save( li ); session.flush(); assertEquals( session.createFilter( customer.getOrders(), "" ).list().size(), 1 ); assertEquals( session.createFilter( order.getLineItems(), "" ).list().size(), 1 ); assertEquals( session.createFilter( order.getLineItems(), "where this.quantity > :quantity" ).setInteger( "quantity", 5 ).list().size(), 0 ); session.delete(li); session.delete(order); session.delete(product); session.delete(customer); txn.commit(); session.close(); } public void testManyToManyFilter() throws Throwable { Session session = openSession(); Transaction txn = session.beginTransaction(); Human human = new Human(); human.setName( new Name() ); human.getName().setFirst( "Steve" ); human.getName().setInitial( 'L' ); human.getName().setLast( "Ebersole" ); session.save( human ); Human friend = new Human(); friend.setName( new Name() ); friend.getName().setFirst( "John" ); friend.getName().setInitial( 'Q' ); friend.getName().setLast( "Doe" ); friend.setBodyWeight( 11.0f ); session.save( friend ); human.setFriends( new ArrayList() ); friend.setFriends( new ArrayList() ); human.getFriends().add( friend ); friend.getFriends().add( human ); session.flush(); assertEquals( session.createFilter( human.getFriends(), "" ).list().size(), 1 ); assertEquals( session.createFilter( human.getFriends(), "where this.bodyWeight > ?" ).setFloat( 0, 10f ).list().size(), 1 ); assertEquals( session.createFilter( human.getFriends(), "where this.bodyWeight < ?" ).setFloat( 0, 10f ).list().size(), 0 ); session.delete(human); session.delete(friend); txn.commit(); session.close(); } public void testSelectExpressions() { createTestBaseData(); Session session = openSession(); Transaction txn = session.beginTransaction(); Human h = new Human(); h.setName( new Name("Gavin", 'A', "King") ); h.setNickName("Oney"); h.setBodyWeight(1.0f); session.persist(h); List results = session.createQuery("select 'found', lower(h.name.first) from Human h where lower(h.name.first) = 'gavin'").list(); results = session.createQuery("select 'found', lower(h.name.first) from Human h where concat(h.name.first, ' ', h.name.initial, ' ', h.name.last) = 'Gavin A King'").list(); results = session.createQuery("select 'found', lower(h.name.first) from Human h where h.name.first||' '||h.name.initial||' '||h.name.last = 'Gavin A King'").list(); results = session.createQuery("select a.bodyWeight + m.bodyWeight from Animal a join a.mother m").list(); results = session.createQuery("select 2.0 * (a.bodyWeight + m.bodyWeight) from Animal a join a.mother m").list(); results = session.createQuery("select sum(a.bodyWeight + m.bodyWeight) from Animal a join a.mother m").list(); results = session.createQuery("select sum(a.mother.bodyWeight * 2.0) from Animal a").list(); results = session.createQuery("select concat(h.name.first, ' ', h.name.initial, ' ', h.name.last) from Human h").list(); results = session.createQuery("select h.name.first||' '||h.name.initial||' '||h.name.last from Human h").list(); results = session.createQuery("select nickName from Human").list(); results = session.createQuery("select lower(nickName) from Human").list(); results = session.createQuery("select abs(bodyWeight*-1) from Human").list(); results = session.createQuery("select upper(h.name.first||' ('||h.nickName||')') from Human h").list(); session.delete(h); txn.commit(); session.close(); destroyTestBaseData(); } private void createTestBaseData() { Session session = openSession(); Transaction txn = session.beginTransaction(); Mammal m1 = new Mammal(); m1.setBodyWeight( 11f ); m1.setDescription( "Mammal #1" ); session.save( m1 ); Mammal m2 = new Mammal(); m2.setBodyWeight( 9f ); m2.setDescription( "Mammal #2" ); m2.setMother( m1 ); session.save( m2 ); txn.commit(); session.close(); createdAnimalIds.add( m1.getId() ); createdAnimalIds.add( m2.getId() ); } private void destroyTestBaseData() { Session session = openSession(); Transaction txn = session.beginTransaction(); for ( int i = 0; i < createdAnimalIds.size(); i++ ) { Animal animal = ( Animal ) session.load( Animal.class, ( Long ) createdAnimalIds.get( i ) ); session.delete( animal ); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?