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

📄 fumtest.java

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		s = openSession();		assertTrue(			s.iterate("select fum.id.short, fum.id.date, fum.id.string from Fum fum").hasNext()		);		assertTrue(			s.iterate("select fum.id from Fum fum").hasNext()		);		Query qu = s.createQuery("select fum.fum, fum , fum.fum, fum.id.date from Fum fum");		Type[] types = qu.getReturnTypes();		assertTrue(types.length==4);		for ( int k=0; k<types.length; k++) {			assertTrue( types[k]!=null );		}		assertTrue(types[0] instanceof StringType);		assertTrue(types[1] instanceof EntityType);		assertTrue(types[2] instanceof StringType);		assertTrue(types[3] instanceof DateType);		Iterator iter = qu.iterate();		int j = 0;		while ( iter.hasNext() ) {			j++;			assertTrue( ( (Object[]) iter.next() )[1] instanceof Fum );		}		assertTrue( "iterate on composite key", j==8 );		fum = (Fum) s.load( Fum.class, fum.getId() );		s.filter( fum.getQuxArray(), "where this.foo is null" );		s.filter( fum.getQuxArray(), "where this.foo.id = ?", "fooid", Hibernate.STRING );		Query f = s.createFilter( fum.getQuxArray(), "where this.foo.id = :fooId" );		f.setString("fooId", "abc");		assertFalse( f.iterate().hasNext() );		iter = s.iterate("from Fum fum where not fum.fum='FRIEND'");		int i = 0;		while ( iter.hasNext() ) {			fum = (Fum) iter.next();			//iter.remove();			s.delete(fum);			i++;		}		assertTrue( "iterate on composite key", i==4 );		s.flush();		s.iterate("from Fum fu, Fum fo where fu.fo.id.string = fo.id.string and fo.fum is not null");		s.find("from Fumm f1 inner join f1.fum f2");		s.connection().commit();		s.close();	}	public void testCompositeIDCollections() throws Exception {		Session s = openSession();		Fum fum1 = new Fum( fumKey("fum1") );		Fum fum2 = new Fum( fumKey("fum2") );		fum1.setFum("fee fo fi");		fum2.setFum("fee fo fi");		s.save(fum1);		s.save(fum2);		Qux q = new Qux();		s.save(q);		Set set = new HashSet();		List list = new ArrayList();		set.add(fum1); set.add(fum2);		list.add(fum1);		q.setFums(set);		q.setMoreFums(list);		fum1.setQuxArray( new Qux[] {q} );		s.flush();		s.connection().commit();		s.close();		s = openSession();		q = (Qux) s.load( Qux.class, q.getKey() );		assertTrue( "collection of fums", q.getFums().size()==2 );		assertTrue( "collection of fums", q.getMoreFums().size()==1 );		assertTrue( "unkeyed composite id collection", ( (Fum) q.getMoreFums().get(0) ).getQuxArray()[0]==q );		Iterator iter = q.getFums().iterator();		iter.hasNext();		Fum f = (Fum) iter.next();		s.delete(f);		iter.hasNext();		f = (Fum) iter.next();		s.delete(f);		s.delete(q);		s.flush();		s.connection().commit();		s.close();	}	public void testDeleteOwner() throws Exception {		Session s = openSession();		Qux q = new Qux();		s.save(q);		Fum f1 = new Fum( fumKey("f1") );		Fum f2 = new Fum( fumKey("f2") );		Set set = new HashSet();		set.add(f1);		set.add(f2);		List list = new LinkedList();		list.add(f1);		list.add(f2);		f1.setFum("f1");		f2.setFum("f2");		q.setFums(set);		q.setMoreFums(list);		s.save(f1);		s.save(f2);		s.flush();		s.connection().commit();		s.close();		s = openSession();		q = (Qux) s.load( Qux.class, q.getKey(), LockMode.UPGRADE );		s.lock( q, LockMode.UPGRADE );		s.delete(q);		s.flush();		s.connection().commit();		s.close();		s = openSession();		list = s.find("from Fum fum where not fum.fum='FRIEND'");		assertTrue( "deleted owner", list.size()==2 );		s.lock( list.get(0), LockMode.UPGRADE );		s.lock( list.get(1), LockMode.UPGRADE );		Iterator iter = list.iterator();		while ( iter.hasNext() ) {			s.delete( iter.next() );		}		s.flush();		s.connection().commit();		s.close();	}	public void testCompositeIDs() throws Exception {		Session s = openSession();		Fo fo = Fo.newFo();		Properties props = new Properties();		props.setProperty("foo", "bar");		props.setProperty("bar", "foo");		fo.setSerial(props);		fo.setBuf( "abcdefghij1`23%$*^*$*\n\t".getBytes() );		s.save( fo, fumKey("an instance of fo") );		s.flush();		props.setProperty("x", "y");		s.flush();		s.connection().commit();		s.close();		s = openSession();		fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") );		props = (Properties) fo.getSerial();		assertTrue( props.getProperty("foo").equals("bar") );		//assertTrue( props.contains("x") );		assertTrue( props.getProperty("x").equals("y") );		assertTrue( fo.getBuf()[0]=='a' );		fo.getBuf()[1]=(byte)126;		s.flush();		s.connection().commit();		s.close();		s = openSession();		fo = (Fo) s.load( Fo.class, fumKey("an instance of fo") );		assertTrue( fo.getBuf()[1]==126 );		assertTrue(			s.iterate("from Fo fo where fo.id.string like 'an instance of fo'").next()==fo		);		s.delete(fo);		s.flush();		try {			s.save( Fo.newFo() );			assertTrue(false);		}		catch (Exception e) {			//System.out.println( e.getMessage() );		}		s.connection().commit();		s.close();	}	public void testKeyManyToOne() throws Exception {		Session s = openSession();		Inner sup = new Inner();		InnerKey sid = new InnerKey();		sup.setDudu("dudu");		sid.setAkey("a");		sid.setBkey("b");		sup.setId(sid);		Middle m = new Middle();		MiddleKey mid = new MiddleKey();		mid.setOne("one");		mid.setTwo("two");		mid.setSup(sup);		m.setId(mid);		m.setBla("bla");		Outer d = new Outer();		OuterKey did = new OuterKey();		did.setMaster(m);		did.setDetailId("detail");		d.setId(did);		d.setBubu("bubu");		s.save(sup);		s.save(m);		s.save(d);		s.flush();		s.connection().commit();		s.close();		s = openSession();		Inner in = (Inner) s.find("from Inner").get(0);		assertTrue( in.getMiddles().size()==1 );		s.flush();		s.connection().commit();		s.close();		s = openSession();		assertTrue( s.find("from Inner _inner join _inner.middles middle").size()==1 );		s.flush();		s.connection().commit();		s.close();		s = openSession();		d = (Outer) s.load(Outer.class, did);		assertTrue( d.getId().getMaster().getId().getSup().getDudu().equals("dudu") );		s.delete(d);		s.delete( d.getId().getMaster() );		s.save( d.getId().getMaster() );		s.save(d);		s.flush();		s.connection().commit();		s.close();		s = openSession();		d = (Outer) s.find("from Outer o where o.id.detailId = ?", d.getId().getDetailId(), Hibernate.STRING ).get(0);		s.find("from Outer o where o.id.master.id.sup.dudu is not null");		s.find("from Outer o where o.id.master.id.sup.id.akey is not null");		s.find("from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey");		List l = s.find("select o.id.master.id.sup.dudu from Outer o where o.id.master.id.sup.dudu is not null");		assertTrue(l.size()==1);		l = s.find("select o.id.master.id.sup.id.akey from Outer o where o.id.master.id.sup.id.akey is not null");		assertTrue(l.size()==1);		s.find("select i.backOut.id.master.id.sup.id.akey from Inner i where i.backOut.id.master.id.sup.id.akey = i.id.bkey");		s.find("from Outer o where o.id.master.bla = ''");		s.find("from Outer o where o.id.master.id.one = ''");		s.find("from Inner inn where inn.id.bkey is not null and inn.backOut.id.master.id.sup.id.akey > 'a'");		s.find("from Outer as o left join o.id.master m left join m.id.sup where o.bubu is not null");		s.find("from Outer as o left join o.id.master.id.sup s where o.bubu is not null");		s.find("from Outer as o left join o.id.master m left join o.id.master.id.sup s where o.bubu is not null");		s.delete(d);		s.delete( d.getId().getMaster() );		s.delete( d.getId().getMaster().getId().getSup() );		s.flush();		s.connection().commit();		s.close();	}	public void testCompositeKeyPathExpressions() throws Exception {		Session s = openSession();		s.find("select fum1.fo from Fum fum1 where fum1.fo.fum is not null");		s.find("from Fum fum1 where fum1.fo.fum is not null order by fum1.fo.fum");		if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof PointbaseDialect) ) {			s.find("from Fum fum1 where exists elements(fum1.friends)");			if(!(getDialect() instanceof TimesTenDialect)) { // can't execute because TimesTen can't do subqueries combined with aggreations				s.find("from Fum fum1 where size(fum1.friends) = 0");			}		}		s.find("select elements(fum1.friends) from Fum fum1");		s.find("from Fum fum1, fr in elements( fum1.friends )");		s.connection().commit();		s.close();	}	public void testUnflushedSessionSerialization() throws Exception {		///////////////////////////////////////////////////////////////////////////		// Test insertions across serializations		Session s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		Simple simple = new Simple();		simple.setAddress("123 Main St. Anytown USA");		simple.setCount(1);		simple.setDate( new Date() );		simple.setName("My UnflushedSessionSerialization Simple");		simple.setPay( new Float(5000) );		s.save( simple, new Long(10) );		// Now, try to serialize session without flushing...		s.disconnect();		Session s2 = spoofSerialization(s);		s.close();		s = s2;		s.reconnect();		simple = (Simple) s.load( Simple.class, new Long(10) );		Simple other = new Simple();		other.init();		s.save( other, new Long(11) );		simple.setOther(other);		s.flush();		s.connection().commit();		s.close();		Simple check = simple;		///////////////////////////////////////////////////////////////////////////		// Test updates across serializations		s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		simple = (Simple) s.get( Simple.class, new Long(10) );		assertTrue("Not same parent instances", check.getName().equals( simple.getName() ) );		assertTrue("Not same child instances", check.getOther().getName().equals( other.getName() ) );		simple.setName("My updated name");		s.disconnect();		s2 = spoofSerialization(s);		s.close();		s = s2;		s.reconnect();		s.flush();		s.connection().commit();		s.close();		check = simple;		///////////////////////////////////////////////////////////////////////////		// Test deletions across serializations		s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		simple = (Simple) s.get( Simple.class, new Long(10) );		assertTrue("Not same parent instances", check.getName().equals( simple.getName() ) );		assertTrue("Not same child instances", check.getOther().getName().equals( other.getName() ) );		// Now, lets delete across serialization...		s.delete(simple);		s.disconnect();		s2 = spoofSerialization(s);		s.close();		s = s2;		s.reconnect();		s.flush();		s.connection().commit();		s.close();		///////////////////////////////////////////////////////////////////////////		// Test collection actions across serializations		s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		Fum fum = new Fum( fumKey("uss-fum") );		fum.setFo( new Fum( fumKey("uss-fo") ) );		fum.setFum("fo fee fi");		fum.getFo().setFum("stuff");		Fum fr = new Fum( fumKey("uss-fr") );		fr.setFum("goo");		Fum fr2 = new Fum( fumKey("uss-fr2") );		fr2.setFum("soo");		fum.setFriends( new HashSet() );		fum.getFriends().add(fr);		fum.getFriends().add(fr2);		s.save(fr);		s.save(fr2);		s.save( fum.getFo() );		s.save(fum);		s.disconnect();		s2 = spoofSerialization(s);		s.close();		s = s2;		s.reconnect();		s.flush();		s.connection().commit();		s.close();		s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		fum = (Fum) s.load( Fum.class, fum.getId() );		assertTrue("the Fum.friends did not get saved", fum.getFriends().size() == 2);		fum.setFriends(null);		s.disconnect();		s2 = spoofSerialization(s);		s.close();				s = s2;		s.reconnect();		s.flush();		s.connection().commit();		s.close();		s = getSessions().openSession();		s.setFlushMode(FlushMode.MANUAL);		fum = (Fum) s.load( Fum.class, fum.getId() );		assertTrue("the Fum.friends is not empty", fum.getFriends() == null || fum.getFriends().size() == 0);		s.connection().commit();		s.close();	}	private Session spoofSerialization(Session session) throws IOException {		try {			// Serialize the incoming out to memory			ByteArrayOutputStream serBaOut = new ByteArrayOutputStream();			ObjectOutputStream serOut = new ObjectOutputStream(serBaOut);			serOut.writeObject(session);			// Now, re-constitute the model from memory			ByteArrayInputStream serBaIn =			        new ByteArrayInputStream(serBaOut.toByteArray());			ObjectInputStream serIn = new ObjectInputStream(serBaIn);			Session outgoing = (Session) serIn.readObject();			return outgoing;		}		catch (ClassNotFoundException cnfe) {			throw new IOException("Unable to locate class on reconstruction");		}	}}

⌨️ 快捷键说明

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