⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 astparserloadingtest.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: ASTParserLoadingTest.java,v 1.18 2005/04/13 06:59:49 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.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.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 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 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 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 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -