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

📄 foobartest.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		s = openSession();		baz = (Baz) s.load(Baz.class, id);		assertTrue( !Hibernate.isInitialized( baz.getFooBag() ) );		assertTrue( baz.getFooBag().size()==1 );		if ( !(getDialect() instanceof HSQLDialect) ) assertTrue( Hibernate.isInitialized( baz.getFooBag().iterator().next() ) );		s.delete(baz);		s.flush();		s.connection().commit();		s.close();			}		public void testIdBag() throws Exception {		Session s = openSession();		Baz baz = new Baz();		s.save(baz);		List l = new ArrayList();		List l2 = new ArrayList();		baz.setIdFooBag(l);		baz.setByteBag(l2);		l.add( new Foo() );		l.add( new Bar() );		byte[] bytes = "ffo".getBytes();		l2.add(bytes);		l2.add( "foo".getBytes() );		s.flush();		l.add( new Foo() );		l.add( new Bar() );		l2.add( "bar".getBytes() );		s.flush();		s.delete( l.remove(3) );		bytes[1]='o';		s.flush();		s.connection().commit();		s.close();				s = openSession();		baz = (Baz) s.load(Baz.class, baz.getCode());		assertTrue( baz.getIdFooBag().size()==3 );		assertTrue( baz.getByteBag().size()==3 );		bytes = "foobar".getBytes();		Iterator iter = baz.getIdFooBag().iterator();		while ( iter.hasNext() ) s.delete( iter.next() );		baz.setIdFooBag(null);		baz.getByteBag().add(bytes);		baz.getByteBag().add(bytes);		assertTrue( baz.getByteBag().size()==5 );		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.load(Baz.class, baz.getCode());		assertTrue( baz.getIdFooBag().size()==0 );		assertTrue( baz.getByteBag().size()==5 );		baz.getIdFooBag().add( new Foo() );		iter = baz.getByteBag().iterator();		iter.next();		iter.remove();		s.flush();		s.connection().commit();		s.close();		s = openSession();		baz = (Baz) s.load(Baz.class, baz.getCode());		assertTrue( baz.getIdFooBag().size()==1 );		assertTrue( baz.getByteBag().size()==4 );		s.delete(baz);		s.flush();		s.connection().commit();		s.close();	}		public void testForceOuterJoin() throws Exception {				if ( !( (SessionFactoryImplementor) getSessions() ).isOuterJoinedFetchEnabled() ) return;				Session s = openSession();		Glarch g = new Glarch();		FooComponent fc = new FooComponent();		fc.setGlarch(g);		FooProxy f = new Foo();		FooProxy f2 = new Foo();		f.setComponent(fc);		f.setFoo(f2);		s.save(f2);		Serializable id = s.save(f);		Serializable gid = s.getIdentifier( f.getComponent().getGlarch() );		s.flush();		s.connection().commit();		s.close();				getSessions().evict(Foo.class);				s = openSession();		f = (FooProxy) s.load(Foo.class, id);		assertFalse( Hibernate.isInitialized(f) );		assertTrue( Hibernate.isInitialized( f.getComponent().getGlarch() ) ); //outer-join="true"		assertFalse( Hibernate.isInitialized( f.getFoo() ) ); //outer-join="auto"		assertEquals( s.getIdentifier( f.getComponent().getGlarch() ), gid );		s.delete(f);		s.delete( f.getFoo() );		s.flush();		s.connection().commit();		s.close();	}		public void testEmptyCollection() throws Exception {		Session s = openSession();		Serializable id = s.save( new Baz() );		s.flush();		s.connection().commit();		s.close();		s = openSession();		Baz baz = (Baz) s.load(Baz.class, id);		Set foos = baz.getFooSet();		assertTrue( foos.size()==0 );		Foo foo = new Foo();		foos.add(foo);		s.save(foo);		s.flush();		s.delete(foo);		s.delete(baz);		s.flush();		s.connection().commit();		s.close();	}	public void testOneToOneGenerator() throws Exception {		Session s = openSession();		X x = new X();		Y y = new Y();		x.setY(y);		y.setTheX(x);		x.getXxs().add( new X.XX(x) );		x.getXxs().add( new X.XX(x) );		Serializable id = s.save(y);		assertEquals( id, s.save(x) );		s.flush();		assertTrue( s.contains(y) && s.contains(x) );		s.connection().commit();		s.close();		assertEquals( new Long(x.getId()), y.getId() );		s = openSession();		x = new X();		y = new Y();		x.setY(y);		y.setTheX(x);		x.getXxs().add( new X.XX(x) );		s.save(y);		s.flush();		assertTrue( s.contains(y) && s.contains(x) );		s.connection().commit();		s.close();		assertEquals( new Long(x.getId()), y.getId() );		s = openSession();		x = new X();		y = new Y();		x.setY(y);		y.setTheX(x);		x.getXxs().add( new X.XX(x) );		x.getXxs().add( new X.XX(x) );		id = s.save(x);		assertEquals( id, y.getId() );		assertEquals( id, new Long( x.getId() ) );		s.flush();		assertTrue( s.contains(y) && s.contains(x) );		s.delete("from X x");		s.flush();		s.connection().commit();		s.close();	}		public void testLimit() throws Exception {		Session s = openSession();		for ( int i=0; i<10; i++ ) s.save( new Foo() );		Iterator iter = s.createQuery("from Foo foo")			.setMaxResults(4)			.setFirstResult(2)			.iterate();		int count=0;		while ( iter.hasNext() ) {			iter.next();			count++;		}		assertTrue(count==4);		iter = s.createQuery("select distinct foo from Foo foo")			.setMaxResults(2)			.setFirstResult(2)			.list()			.iterator();		count=0;		while ( iter.hasNext() ) {			iter.next();			count++;		}		assertTrue(count==2);		assertTrue( s.delete("from Foo foo")==10 );		s.flush();		s.connection().commit();		s.close();	}		public void testCustom() throws Exception {		GlarchProxy g = new Glarch();		Multiplicity m = new Multiplicity();		m.count = 12;		m.glarch = (Glarch) g;		g.setMultiple(m);		Session s = openSession();		Serializable gid = s.save(g);		s.flush();		s.connection().commit();		s.close();				s = openSession();		g = (Glarch) s.find("from Glarch g where g.multiple.glarch=g and g.multiple.count=12").get(0);		assertTrue( g.getMultiple()!=null );		assertEquals( g.getMultiple().count, 12 );		assertSame(g.getMultiple().glarch, g);		s.flush();		s.connection().commit();		s.close();		s = openSession();		g = (GlarchProxy) s.load(Glarch.class, gid);		assertTrue( g.getMultiple()!=null );		assertEquals( g.getMultiple().count, 12 );		assertSame(g.getMultiple().glarch, g);		s.delete(g);		s.flush();		s.connection().commit();		s.close();	}		public void testSaveAddDelete() throws Exception {		Session s = openSession();		Baz baz = new Baz();		Set bars = new HashSet();		baz.setCascadingBars(bars);		s.save(baz);		s.flush();		baz.getCascadingBars().add( new Bar() );		s.delete(baz);		s.flush();		s.connection().commit();		s.close();	}		public void testNamedParams() throws Exception {		Bar bar = new Bar();		Bar bar2 = new Bar();		bar.setName("Bar");		bar2.setName("Bar Two");		bar.setX(10);		bar2.setX(1000);Baz baz = new Baz();		baz.setCascadingBars( new HashSet() );		baz.getCascadingBars().add(bar);		bar.setBaz(baz);		Session s = openSession();		s.save(baz);		s.save(bar2);				List list = s.find("from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'");		Object row = list.iterator().next();		assertTrue( row instanceof Object[] && ( (Object[]) row ).length==3 );				Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'");		list = q.list();		if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );		q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal");		HashSet nameList = new HashSet();		nameList.add("bar");		nameList.add("Bar");		nameList.add("Bar Two");		q.setParameterList("nameList", nameList);		q.setParameter("stringVal", "a string");		list = q.list();		if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 );		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");		if ( getDialect() instanceof HSQLDialect ) {			q.setString("name", "Bar %");		}		else {			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);		s.flush();		s.connection().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();		}			 	}	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);

⌨️ 快捷键说明

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