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

📄 foobartest.java

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		try {			q.setParameterList("nameList", (Collection)null);			fail("Should throw an queryexception when passing a null!");		} catch (QueryException qe) {			//should happen		}		if (dialectSupportsEmptyInList("HQL 'x in (:name)'  with EMPTY_LIST.")) { 				q.setParameterList("nameList", Collections.EMPTY_LIST);			list = q.list();			assertTrue( list.size()==0 );		}		q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");		Object result = q.uniqueResult();		assertTrue( result!=null );		q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name");		q.setString("name", "Bar%");		list = q.list();		assertTrue( list.size()==1 );		// This test added for issue HB-297 - there is an named parameter in the Order By clause		q = s.createQuery("select bar from Bar bar order by ((bar.x - :valueX)*(bar.x - :valueX))");		q.setInteger("valueX", bar.getX()+1);		list = q.list();		assertTrue( ((Bar)list.get(0)).getX() == bar.getX());		q.setInteger("valueX", bar2.getX()+1);		list = q.list();		assertTrue( ((Bar)list.get(0)).getX() == bar2.getX());		s.delete(baz);		s.delete(bar2);		txn.commit();		s.close();	}	public void testParameterCheck() throws HibernateException {		Session s = openSession();		try {			Query q = s.createQuery("select bar from Bar as bar where bar.x > :myX");			q.list();			fail("Should throw QueryException for missing myX");		}		catch (QueryException iae) {			// should happen		}		finally {			s.close();		}		s = openSession();		try {			Query q = s.createQuery("select bar from Bar as bar where bar.x > ?");			q.list();			fail("Should throw QueryException for missing ?");		}		catch (QueryException iae) {			// should happen		}		finally {			s.close();		}		s = openSession();		try {			Query q = s.createQuery("select bar from Bar as bar where bar.x > ? or bar.short = 1 or bar.string = 'ff ? bb'");			q.setInteger(0, 1);			q.list();		}		catch (QueryException iae) {			fail("Should not throw QueryException for missing ?");		}		finally {			s.close();		}		s = openSession();		try {			Query q = s.createQuery("select bar from Bar as bar where bar.string = ' ? ' or bar.string = '?'");			q.list();		}		catch (QueryException iae) {			fail("Should not throw QueryException for ? in quotes");		}		finally {			s.close();		}		s = openSession();		try {			Query q = s.createQuery("select bar from Bar as bar where bar.string = ? or bar.string = ? or bar.string = ?");			q.setParameter(0, "bull");			q.setParameter(2, "shit");			q.list();			fail("should throw exception telling me i have not set parameter 1");		}		catch (QueryException iae) {			// should happen!		}		finally {			s.close();		}	}	public void testDyna() throws Exception {		Session s = openSession();		GlarchProxy g = new Glarch();		g.setName("G");		Serializable id = s.save(g);		s.flush();		s.connection().commit();		s.close();		s = openSession();		g = (GlarchProxy) s.load(Glarch.class, id);		assertTrue( g.getName().equals("G") );		assertTrue( g.getDynaBean().get("foo").equals("foo") && g.getDynaBean().get("bar").equals( new Integer(66) ) );		assertTrue( ! (g instanceof Glarch) );		g.getDynaBean().put("foo", "bar");		s.flush();		s.connection().commit();		s.close();		s = openSession();		g = (GlarchProxy) s.load(Glarch.class, id);		assertTrue( g.getDynaBean().get("foo").equals("bar") && g.getDynaBean().get("bar").equals( new Integer(66) ) );		g.setDynaBean(null);		s.flush();		s.connection().commit();		s.close();		s = openSession();		g = (GlarchProxy) s.load(Glarch.class, id);		assertTrue( g.getDynaBean()==null );		s.delete(g);		s.flush();		s.connection().commit();		s.close();	}	public void testFindByCriteria() throws Exception {		if ( getDialect() instanceof DB2Dialect ) return;		Session s = openSession();		Transaction txn = s.beginTransaction();		Foo f = new Foo();		s.save(f);		s.flush();		List list = s.createCriteria(Foo.class)			.add( Restrictions.eq( "integer", f.getInteger() ) )			.add( Restrictions.eqProperty("integer", "integer") )			.add( Restrictions.like( "string", f.getString().toUpperCase() ).ignoreCase() )			.add( Restrictions.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) )			.setFetchMode("foo", FetchMode.JOIN)			.setFetchMode("baz", FetchMode.SELECT)			.setFetchMode("abstracts", FetchMode.JOIN)			.list();		assertTrue( list.size()==1 && list.get(0)==f );		list = s.createCriteria(Foo.class).add(				Restrictions.disjunction()					.add( Restrictions.eq( "integer", f.getInteger() ) )					.add( Restrictions.like( "string", f.getString() ) )					.add( Restrictions.eq( "boolean", f.getBoolean() ) )			)			.add( Restrictions.isNotNull("boolean") )			.list();		assertTrue( list.size()==1 && list.get(0)==f );		Foo example = new Foo();		example.setString("a STRing");		list = s.createCriteria(Foo.class).add(			Example.create(example)				.excludeZeroes()				.ignoreCase()				.excludeProperty("bool")				.excludeProperty("char")				.excludeProperty("yesno")			)			.list();		assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f );		example.setString("rin");		list = s.createCriteria(Foo.class).add(			Example.create(example)				.excludeZeroes()				.enableLike(MatchMode.ANYWHERE)				.excludeProperty("bool")				.excludeProperty("char")				.excludeProperty("yesno")			)			.list();		assertTrue( "Example API without like did not work correctly, size was " + list.size(), list.size()==1 && list.get(0)==f );		list = s.createCriteria(Foo.class)			.add( Restrictions.or(					Restrictions.and(					Restrictions.eq( "integer", f.getInteger() ),					Restrictions.like( "string", f.getString() )				),				Restrictions.eq( "boolean", f.getBoolean() )			) )			.list();		assertTrue( list.size()==1 && list.get(0)==f );		list = s.createCriteria(Foo.class)			.setMaxResults(5)			.addOrder( Order.asc("date") )			.list();		assertTrue( list.size()==1 && list.get(0)==f );		if(!(getDialect() instanceof TimesTenDialect)) {			list = s.createCriteria(Foo.class).setMaxResults(0).list();			assertTrue( list.size()==0 );		}		list = s.createCriteria(Foo.class)			.setFirstResult(1)			.addOrder( Order.asc("date") )			.addOrder( Order.desc("string") )			.list();		assertTrue( list.size()==0 );		list = s.createCriteria(Foo.class)			.setFetchMode("component.importantDates", FetchMode.EAGER)			.list();		assertTrue( list.size()==3 );		list = s.createCriteria(Foo.class)			.setFetchMode("component.importantDates", FetchMode.EAGER)			.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)			.list();		assertTrue( list.size()==1 );		f.setFoo( new Foo() );		s.save( f.getFoo() );		txn.commit();		s.close();		s = openSession();		txn = s.beginTransaction();		list = s.createCriteria(Foo.class)			.add( Restrictions.eq( "integer", f.getInteger() ) )			.add( Restrictions.like( "string", f.getString() ) )			.add( Restrictions.in( "boolean", new Boolean[] { f.getBoolean(), f.getBoolean() } ) )			.add( Restrictions.isNotNull("foo") )			.setFetchMode("foo", FetchMode.EAGER)			.setFetchMode("baz", FetchMode.LAZY)			.setFetchMode("component.glarch", FetchMode.LAZY)			.setFetchMode("foo.baz", FetchMode.LAZY)			.setFetchMode("foo.component.glarch", FetchMode.LAZY)			.list();		f = (Foo) list.get(0);		assertTrue( Hibernate.isInitialized( f.getFoo() ) );		assertTrue( !Hibernate.isInitialized( f.getComponent().getGlarch() ) );		s.save( new Bar() );		list = s.createCriteria(Bar.class)			.list();		assertTrue( list.size()==1 );		assertTrue( s.createCriteria(Foo.class).list().size()==3 );		s.delete( list.get(0) );		s.delete( f.getFoo() );		s.delete(f);		txn.commit();		s.close();	}	public void testAfterDelete() throws Exception {		Session s = openSession();		Foo foo = new Foo();		s.save(foo);		s.flush();		s.delete(foo);		s.save(foo);		s.delete(foo);		s.flush();		s.connection().commit();		s.close();	}	public void testCollectionWhere() throws Exception {		Foo foo1 = new Foo();		Foo foo2 = new Foo();		Baz baz = new Baz();		Foo[] arr = new Foo[10];		arr[0] = foo1;		arr[9] = foo2;		Session s = openSession();		s.save(foo1);		s.save(foo2);		baz.setFooArray(arr);		s.save(baz);		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.load( Baz.class, baz.getCode() );		assertTrue( baz.getFooArray().length==1 );		assertTrue( s.find("from Baz baz join baz.fooArray foo").size()==1 );		assertTrue( s.find("from Foo foo").size()==2 );		assertTrue( s.filter( baz.getFooArray(), "" ).size()==1 );		//assertTrue( s.delete("from java.lang.Object o")==9 );		s.delete("from Foo foo");		String bazid = baz.getCode();		s.delete(baz);		int rows=s.connection().createStatement().executeUpdate(			"delete from fooArray where id_='" + bazid + "' and i>=8"		);		assertTrue(rows==1);		s.flush();		s.connection().commit();		s.close();	}	public void testComponentParent() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		BarProxy bar = new Bar();		bar.setBarComponent( new FooComponent() );		Baz baz = new Baz();		baz.setComponents( new FooComponent[] { new FooComponent(), new FooComponent() } );		s.save(bar);		s.save(baz);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		bar = (BarProxy) s.load(Bar.class, bar.getKey());		s.load(baz, baz.getCode());		assertTrue( bar.getBarComponent().getParent()==bar );		assertTrue( baz.getComponents()[0].getBaz()==baz && baz.getComponents()[1].getBaz()==baz );		s.delete(baz);		s.delete(bar);		t.commit();		s.close();	}	public void testCollectionCache() throws Exception {		Session s = openSession();		Baz baz = new Baz();		baz.setDefaults();		s.save(baz);		s.flush();		s.connection().commit();		s.close();		s = openSession();		s.load( Baz.class, baz.getCode() );		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.load( Baz.class, baz.getCode() );		s.delete(baz);		s.flush();		s.connection().commit();		s.close();	}	public void ntestAssociationId() throws Exception {		Session s = openSession();		Transaction t = s.beginTransaction();		Bar bar = new Bar();		String id = (String) s.save(bar);		MoreStuff more = new MoreStuff();		more.setName("More Stuff");		more.setIntId(12);		more.setStringId("id");		Stuff stuf = new Stuff();		stuf.setMoreStuff(more);		more.setStuffs( new ArrayList() );		more.getStuffs().add(stuf);		stuf.setFoo(bar);		stuf.setId(1234);		stuf.setProperty( TimeZone.getDefault() );		s.save(more);		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		assertTrue( s.find(			"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?",			new Object[] { bar, new Long(1234), new Integer(12), "id" },			new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING }		).size()==1 );		assertTrue( s.find(			"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?",			new Object[] { bar, new Long(1234), "More Stuff" },			new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING }		).size()==1 );		s.find("from Stuff as s where s.foo.string is not null");		assertTrue(			s.find("from Stuff as s where s.foo > '0' order by s.foo").size()==1		);		//s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list();		t.commit();		s.close();		s = openSession();		t = s.beginTransaction();		FooProxy foo = (FooProxy) s.load(Foo.class, id);		s.load(more, more);		t.commit();		s.close();		s = openSessio

⌨️ 快捷键说明

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