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

📄 foobartest.java

📁 介绍了hibernate的入门有一些基本常用的事例
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			if ( !( getDialect() instanceof InterbaseDialect ) ) {				list = s.find("from Foo foo where ? = some elements(foo.component.importantDates)", new Date(), Hibernate.DATE);				assertTrue( "component query", list.size()==2 );			}			if( !( getDialect() instanceof TimesTenDialect)) {				list = s.find("from Foo foo where size(foo.component.importantDates) = 3"); //WAS: 4				assertTrue( "component query", list.size()==2 );				list = s.find("from Foo foo where 0 = size(foo.component.importantDates)");				assertTrue( "component query", list.size()==0 );			}			list = s.find("from Foo foo where exists elements(foo.component.importantDates)");			assertTrue( "component query", list.size()==2 );			s.find("from Foo foo where not exists (from Bar bar where bar.id = foo.id)");			s.find("select foo.foo from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)");			s.find("select foo.foo from Foo foo where foo = some(from Foo x where (x.long > foo.foo.long))");			if ( !( getDialect() instanceof TimesTenDialect)) {				s.find("select foo.foo from Foo foo where foo.long = some( select max(x.long) from Foo x where (x.long > foo.foo.long) group by x.foo )");			}			s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long) and foo.foo.string='baz'");			s.find("from Foo foo where foo.foo.string='baz' and foo = some(select x from Foo x where x.long > foo.foo.long)");			s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)");			s.iterate("select foo.string, foo.date, foo.foo.string, foo.id from Foo foo, Baz baz where foo in elements(baz.fooArray) and foo.string like 'foo'");		}		list = s.find("from Foo foo where foo.component.count is null order by foo.component.count");		assertTrue( "component query", list.size()==0 );		list = s.find("from Foo foo where foo.component.name='foo'");		assertTrue( "component query", list.size()==2 );		list = s.find("select distinct foo.component.name, foo.component.name from Foo foo where foo.component.name='foo'");		assertTrue( "component query", list.size()==1 );		list = s.find("select distinct foo.component.name, foo.id from Foo foo where foo.component.name='foo'");		assertTrue( "component query", list.size()==2 );		list = s.find("select foo.foo from Foo foo");		assertTrue( "query", list.size()==2 );		list = s.find("from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING);		assertTrue( "id query", list.size()==1 );		list = s.find("from Foo foo where foo.key=?", foo.getKey(), Hibernate.STRING);		assertTrue( "named id query", list.size()==1 );		assertTrue( "id query", list.get(0)==foo );		list = s.find("select foo.foo from Foo foo where foo.string='fizard'");		assertTrue( "query", list.size()==1 );		assertTrue( "returned object", list.get(0)==foo.getFoo() );		list = s.find("from Foo foo where foo.component.subcomponent.name='bar'");		assertTrue( "components of components", list.size()==2 );		list = s.find("select foo.foo from Foo foo where foo.foo.id=?", foo.getFoo().getKey(), Hibernate.STRING);		assertTrue( "by id query", list.size()==1 );		assertTrue( "by id returned object", list.get(0)==foo.getFoo() );		s.find( "from Foo foo where foo.foo = ?", foo.getFoo(), Hibernate.entity(Foo.class) );		assertTrue( !s.iterate("from Bar bar where bar.string='a string' or bar.string='a string'").hasNext() );		iter = s.iterate(			"select foo.component.name, elements(foo.component.importantDates) from Foo foo where foo.foo.id=?",			foo.getFoo().getKey(),			Hibernate.STRING		);		int i=0;		while ( iter.hasNext() ) {			i++;			Object[] row = (Object[]) iter.next();			assertTrue( row[0] instanceof String && ( row[1]==null || row[1] instanceof Date ) );		}		assertTrue(i==3); //WAS: 4		iter = s.iterate(			"select max( elements(foo.component.importantDates) ) from Foo foo group by foo.id"		);		assertTrue( iter.next() instanceof Date );		list = s.find(			"select foo.foo.foo.foo from Foo foo, Foo foo2 where"			+ " foo = foo2.foo and not not ( not foo.string='fizard' )"			+ " and foo2.string between 'a' and (foo.foo.string)"			+ ( ( getDialect() instanceof HSQLDialect || getDialect() instanceof InterbaseDialect || getDialect() instanceof TimesTenDialect)?				" and ( foo2.string in ( 'fiz', 'blah') or 1=1 )"				:				" and ( foo2.string in ( 'fiz', 'blah', foo.foo.string, foo.string, foo2.string ) )"			)		);		assertTrue( "complex query", list.size()==1 );		assertTrue( "returned object", list.get(0)==foo );		foo.setString("from BoogieDown  -tinsel town  =!@#$^&*())");		list = s.find("from Foo foo where foo.string='from BoogieDown  -tinsel town  =!@#$^&*())'");		assertTrue( "single quotes", list.size()==1 );		list = s.find("from Foo foo where not foo.string='foo''bar'");		assertTrue( "single quotes", list.size()==2 );		list = s.find("from Foo foo where foo.component.glarch.next is null");		assertTrue( "query association in component", list.size()==2 );		Bar bar = new Bar();		Baz baz = new Baz();		baz.setDefaults();		bar.setBaz(baz);		baz.setManyToAny( new ArrayList() );		baz.getManyToAny().add(bar);		baz.getManyToAny().add(foo);		s.save(bar);		s.save(baz);		list = s.find(" from Bar bar where bar.baz.count=667 and bar.baz.count!=123 and not bar.baz.name='1-E-1'");		assertTrue( "query many-to-one", list.size()==1 );		list = s.find(" from Bar i where i.baz.name='Bazza'");		assertTrue( "query many-to-one", list.size()==1 );		Iterator rs = s.iterate("select count(distinct foo.foo) from Foo foo");		assertTrue( "count", ( (Integer) rs.next() ).intValue()==2 );		assertTrue( !rs.hasNext() );		rs = s.iterate("select count(foo.foo.boolean) from Foo foo");		assertTrue( "count", ( (Integer) rs.next() ).intValue()==2 );		assertTrue( !rs.hasNext() );		rs = s.iterate("select count(*), foo.int from Foo foo group by foo.int");		assertTrue( "count(*) group by", ( (Object[]) rs.next() )[0].equals( new Integer(3) ) );		assertTrue( !rs.hasNext() );		rs = s.iterate("select sum(foo.foo.int) from Foo foo");		assertTrue( "sum", ( (Integer) rs.next() ).intValue()==4 );		assertTrue( !rs.hasNext() );		rs = s.iterate("select count(foo) from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING);		assertTrue( "id query count", ( (Integer) rs.next() ).intValue()==1 );		assertTrue( !rs.hasNext() );		list = s.find( "from Foo foo where foo.boolean = ?", new Boolean(true), Hibernate.BOOLEAN );		list = s.find("select new Foo(fo.x) from Fo fo");		list = s.find("select new Foo(fo.integer) from Foo fo");		list = s.createQuery("select new Foo(fo.x) from Foo fo")			//.setComment("projection test")			.setCacheable(true)			.list();		assertTrue(list.size()==3);		list = s.createQuery("select new Foo(fo.x) from Foo fo")			//.setComment("projection test 2")			.setCacheable(true)			.list();		assertTrue(list.size()==3);		rs = s.iterate("select new Foo(fo.x) from Foo fo");		assertTrue( "projection iterate (results)", rs.hasNext() );		assertTrue( "projection iterate (return check)", Foo.class.isAssignableFrom( rs.next().getClass() ) );		ScrollableResults sr = s.createQuery("select new Foo(fo.x) from Foo fo").scroll();		assertTrue( "projection scroll (results)", sr.next() );		assertTrue( "projection scroll (return check)", Foo.class.isAssignableFrom( sr.get(0).getClass() ) );		list = s.find("select foo.long, foo.component.name, foo, foo.foo from Foo foo");		rs = list.iterator();		int count=0;		while ( rs.hasNext() ) {			count++;			Object[] row = (Object[]) rs.next();			assertTrue( row[0] instanceof Long );			assertTrue( row[1] instanceof String );			assertTrue( row[2] instanceof Foo );			assertTrue( row[3] instanceof Foo );		}		assertTrue(count!=0);		list = s.find("select avg(foo.float), max(foo.component.name), count(distinct foo.id) from Foo foo");		rs = list.iterator();		count=0;		while ( rs.hasNext() ) {			count++;			Object[] row = (Object[]) rs.next();			assertTrue( row[0] instanceof Float );			assertTrue( row[1] instanceof String );			assertTrue( row[2] instanceof Integer );		}		assertTrue(count!=0);		list = s.find("select foo.long, foo.component, foo, foo.foo from Foo foo");		rs = list.iterator();		count=0;		while ( rs.hasNext() ) {			count++;			Object[] row = (Object[]) rs.next();			assertTrue( row[0] instanceof Long );			assertTrue( row[1] instanceof FooComponent );			assertTrue( row[2] instanceof Foo );			assertTrue( row[3] instanceof Foo );		}		assertTrue(count!=0);		s.save( new Holder("ice T") );		s.save( new Holder("ice cube") );		assertTrue( s.find("from java.lang.Object as o").size()==15 );		assertTrue( s.find("from Named").size()==7 );		assertTrue( s.find("from Named n where n.name is not null").size()==4 );		iter = s.iterate("from Named n");		while ( iter.hasNext() ) {			assertTrue( iter.next() instanceof Named );		}		s.save( new Holder("bar") );		iter = s.iterate("from Named n0, Named n1 where n0.name = n1.name");		int cnt = 0;		while ( iter.hasNext() ) {			Object[] row = (Object[]) iter.next();			if ( row[0]!=row[1] ) cnt++;		}		if ( !(getDialect() instanceof HSQLDialect) ) {			assertTrue(cnt==2);			assertTrue( s.find("from Named n0, Named n1 where n0.name = n1.name").size()==7 );		}		Query qu = s.createQuery("from Named n where n.name = :name");		qu.getReturnTypes();		qu.getNamedParameters();		iter = s.iterate("from java.lang.Object");		int c = 0;		while ( iter.hasNext() ) {			iter.next();			c++;		}		assertTrue(c==16);		s.iterate("select baz.code, min(baz.count) from Baz baz group by baz.code");		iter = s.iterate("selecT baz from Baz baz where baz.stringDateMap['foo'] is not null or baz.stringDateMap['bar'] = ?", new Date(), Hibernate.DATE);		assertFalse( iter.hasNext() );		list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null");		assertTrue( list.size()==1 );		list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null and baz.stringDateMap['big bang'] < baz.stringDateMap['now']");		assertTrue( list.size()==1 );		list = s.find("select index(date) from Baz baz join baz.stringDateMap date");		System.out.println(list);		assertTrue( list.size()==2 );		s.find("from Foo foo where foo.integer not between 1 and 5 and foo.string not in ('cde', 'abc') and foo.string is not null and foo.integer<=3");		s.find("from Baz baz inner join baz.collectionComponent.nested.foos foo where foo.string is null");		if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) /*&& !(dialect instanceof Oracle9Dialect)*/ )  {			s.find("from Baz baz inner join baz.fooSet where '1' in (from baz.fooSet foo where foo.string is not null)");			s.find("from Baz baz where 'a' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)");			s.find("from Baz baz where 'b' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)");		}		s.find("from Foo foo join foo.foo where foo.foo in ('1','2','3')");		if ( !(getDialect() instanceof HSQLDialect) ) s.find("from Foo foo left join foo.foo where foo.foo in ('1','2','3')");		s.find("select foo.foo from Foo foo where foo.foo in ('1','2','3')");		s.find("select foo.foo.string from Foo foo where foo.foo in ('1','2','3')");		s.find("select foo.foo.string from Foo foo where foo.foo.string in ('1','2','3')");		s.find("select foo.foo.long from Foo foo where foo.foo.string in ('1','2','3')");		s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') or foo.foo.long in (1,2,3)");		s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') group by foo.foo.long");		s.find("from Foo foo1 left join foo1.foo foo2 left join foo2.foo where foo1.string is not null");		s.find("from Foo foo1 left join foo1.foo.foo where foo1.string is not null");		s.find("from Foo foo1 left join foo1.foo foo2 left join foo1.foo.foo foo3 where foo1.string is not null");		s.find("select foo.formula from Foo foo where foo.formula > 0");		int len = s.find("from Foo as foo join foo.foo as foo2 where foo2.id >'a' or foo2.id <'a'").size();		assertTrue(len==2);		s.delete("from Holder");		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.createQuery("from Baz baz left outer join fetch baz.manyToAny").uniqueResult();		assertTrue( Hibernate.isInitialized( baz.getManyToAny() ) );		assertTrue( baz.getManyToAny().size()==2 );		BarProxy barp = (BarProxy) baz.getManyToAny().get(0);		s.find("from Baz baz join baz.manyToAny");		assertTrue( s.find("select baz from Baz baz join baz.manyToAny a where index(a) = 0").size()==1 );		FooProxy foop = (FooProxy) s.get( Foo.class, foo.getKey() );		assertTrue( foop == baz.getManyToAny().get(1) );		barp.setBaz(baz);		assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['now'] is not null").size()==1 );		assertTrue( s.find("select bar from Bar bar join bar.baz b where b.stringDateMap['big bang'] < b.stringDateMap['now'] and b.stringDateMap['now'] is not null").size()==1 );		assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['big bang'] < bar.baz.stringDateMap['now'] and bar.baz.stringDateMap['now'] is not null").size()==1 );		list = s.find("select foo.string, foo.component, foo.id from Bar foo");		assertTrue ( ( (FooComponent) ( (Object[]) list.get(0) )[1] ).getName().equals("foo") );		list = s.find("select elements(baz.components) from Baz baz");		assertTrue( list.size()==2 );		list = s.find("select bc.name from Baz baz join baz.components bc");		assertTrue( list.size()==2 );		//list = s.find("select bc from Baz baz join baz.components bc");		s.createQuery("from Foo foo where foo.integer < 10 order by foo.string").setMaxResults(12).list();		s.delete(barp);		s.delete(baz);		s.delete( foop.getFoo() );		s.delete(foop);		s.flush();		s.connection().commit();		s.close();	}	public void testCascadeDeleteDetached() throws Exception {		Session s = openSession();		Baz baz = new Baz();		List list = new ArrayList();		list.add( new Fee() );		baz.setFees(list);		s.save(baz);		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.get( Baz.class, baz.getCode() );		s.connection().commit();		s.close();		assertFalse( Hibernate.isInitialized( baz.getFees() ) );		s = openSession();		s.delete(baz);		s.flush();		assertFalse( s.iterate("from Fee").hasNext() );		s.connection().commit();		s.close();		s = openSession();		baz = new Baz();		list = new ArrayList();		list.add( new Fee() );		list.add( new Fee() );		baz.setFees(list);		s.save(baz);		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.get( Baz.class, baz.getCode() );		Hibernate.initialize( baz.getFees() );		s.connection().commit();		s.close();		assertTrue( baz.getFees().size()==2 );		s = openSession();		s.delete(baz);		s.flush();		assertFalse( s.iterate("from Fee").hasNext() );		s.connection().commit();		s.close();	}	public void testForeignKeys() throws Exception {		Session s = openSession();		Baz baz = new Baz();		Foo foo = new Foo();		List bag = new ArrayList();		bag.add(foo);		baz.setIdFooBag(bag);		baz.setFoo(foo);		s.save(baz);		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 testNonlazyCollection() throws Exception {		Session s = openSession();		Baz baz = new Baz();		s.save(baz);		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.createCriteria(Baz.class)			//.setComment("criteria test")			.setFetchMode("stringDateMap", FetchMode.EAGER)			.uniqueResult();		assertTrue( Hibernate.isInitialized( baz.getFooToGlarch() ) );		assertTrue( Hibernate.isInitialized( baz.getFooComponentToFoo() ) );		assertTrue( !Hibernate.isInitialized( baz.getStringSet() ) );		assertTrue( Hibernate.isInitialized( baz.getStringDateMap() ) );		s.delete(baz);		s.flush();		s.connection().commit();		s.close();	}	public void testReuseDeletedCollection() throws Exception {		Session s = openSession();		Baz baz = new Baz();		baz.setDefaults();		s.save(baz);		s.flush();		s.delete(baz);		Baz baz2 = new Baz();		baz2.setStringArray( new String[] {"x-y-z"} );		s.save(baz2);		s.flush();		s.connection().commit();		s.close();		baz2.setStringSet( baz.getStringSet() );		baz2.setStringArray( baz.getStringArray() );		baz2.setFooArray( baz.getFooArray() );		s = openSession();		s.update(baz2);		s.flush();

⌨️ 快捷键说明

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