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

📄 astparserloadingtest.java

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		Session s = openSession();		Transaction t = s.beginTransaction();		s.createQuery( "from Animal a where a.description = ? and a.bodyWeight = ?" )				.setString( 0, "something" )				.setFloat( 1, 123f )				.list();		s.createQuery( "from Animal a where a.bodyWeight in (?, ?)" )				.setFloat( 0, 999f )				.setFloat( 1, 123f )				.list();		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);		Double sum = (Double) 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);		Long id = (Long) s.createQuery("select max(a.id) from Animal a").uniqueResult();		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 testStr() {		Session session = openSession();		Transaction txn = session.beginTransaction();		Animal an = new Animal();		an.setBodyWeight(123.45f);		session.persist(an);		String str = (String) session.createQuery("select str(an.bodyWeight) from Animal an where str(an.bodyWeight) like '123%' or str(an.bodyWeight) like '1.23%'").uniqueResult();		if ( getDialect() instanceof DB2Dialect ) {			assertTrue( str.startsWith("1.234") );		}		else if ( getDialect() instanceof SQLServerDialect ) {			// no assertion as SQLServer always returns nulls here; even trying directly against the			// database, it seems to have problems with str() in the where clause...		}		else {			assertTrue( str.startsWith("123.4") );		}		if ( ! ( getDialect() instanceof SybaseDialect ) ) {			// In TransactSQL (the variant spoken by Sybase and SQLServer), the str() function			// is explicitly intended for numeric values only...			String dateStr1 = (String) session.createQuery("select str(current_date) from Animal").uniqueResult();			String dateStr2 = (String) session.createQuery("select str(year(current_date))||'-'||str(month(current_date))||'-'||str(day(current_date)) from Animal").uniqueResult();			System.out.println(dateStr1 + '=' + dateStr2);			if ( ! ( getDialect() instanceof Oracle9Dialect || getDialect() instanceof Oracle8iDialect ) ) { //Oracle renders the name of the month :(				String[] dp1 = StringHelper.split("-", dateStr1);				String[] dp2 = StringHelper.split("-", dateStr2);				for (int i=0; i<3; i++) {					if ( dp1[i].startsWith( "0" ) ) {						dp1[i] = dp1[i].substring( 1 );					}					assertEquals( dp1[i], dp2[i] );				}			}		}		session.delete(an);		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( "Steve", 'L', "Ebersole" ) );		session.save( human );		Human friend = new Human();		friend.setName( new Name( "John", 'Q', "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();		results = session.createQuery("select abs(a.bodyWeight-:param) from Animal a").setParameter("param", new Float(2.0)).list();		results = session.createQuery("select abs(:param - a.bodyWeight) from Animal a").setParameter("param", new Float(2.0)).list();		results = session.createQuery("select lower(upper('foo')) from Animal").list();		results = session.createQuery("select lower(upper('foo') || upper('bar')) from Animal").list();		results = session.createQuery("select sum(abs(bodyWeight - 1.0) * abs(length('ffobar')-3)) from Animal").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 );		}		txn.commit();		session.close();	}	public void testImplicitJoin() throws Exception {		Session session = openSession();		Transaction t = session.beginTransaction();		Animal a = new Animal();		a.setBodyWeight(0.5f);		a.setBodyWeight(1.5f);		Animal b = new Animal();		Animal mother = new Animal();		mother.setBodyWeight(10.0f);		mother.addOffspring(a);		mother.addOffspring(b);		session.persist(a);		session.persist(b);		session.persist(mother);		List list = session.createQuery("from Animal a where a.mother.bodyWeight < 2.0 or a.mother.bodyWeight > 9.0").list();		assertEquals( list.size(), 2 );		list = session.createQuery("from Animal a where a.mother.bodyWeight > 2.0 and a.mother.bodyWeight > 9.0").list();		assertEquals( list.size(), 2 );		session.delete(b);		session.delete(a);		session.delete(mother);		t.commit();		session.close();	}	public void testFromOnly() throws Exception {		createTestBaseData();

⌨️ 快捷键说明

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