Java Code Examples for org.hibernate.classic.Session#iterate()

The following examples show how to use org.hibernate.classic.Session#iterate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testSelfManyToOne() throws Exception {

		//if (dialect instanceof HSQLDialect) return;

		Session s = openSession();
		Transaction t = s.beginTransaction();
		Master m = new Master();
		m.setOtherMaster(m);
		s.save(m);
		t.commit();
		s.close();
		s = openSession();
		t = s.beginTransaction();
		Iterator i = s.iterate("from Master");
		m = (Master) i.next();
		assertTrue( m.getOtherMaster()==m );
		if (getDialect() instanceof HSQLDialect) { m.setOtherMaster(null); s.flush(); }
		s.delete(m);
		t.commit();
		s.close();
	}
 
Example 2
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testListIdentifiers() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	Fum fum = new Fum( fumKey("fum") );
	fum.setFum("fo fee fi");
	s.save(fum);
	fum = new Fum( fumKey("fi") );
	fum.setFum("fee fi fo");
	s.save(fum);
	List list = s.find("select fum.id from Fum as fum where not fum.fum='FRIEND'");
	assertTrue( "list identifiers", list.size()==2);
	Iterator iter = s.iterate("select fum.id from Fum fum where not fum.fum='FRIEND'");
	int i=0;
	while ( iter.hasNext() ) {
		assertTrue( "iterate identifiers",  iter.next() instanceof FumCompositeID);
		i++;
	}
	assertTrue(i==2);

	s.delete( s.load(Fum.class, (Serializable) list.get(0) ) );
	s.delete( s.load(Fum.class, (Serializable) list.get(1) ) );
	txn.commit();
	s.close();
}
 
Example 3
Source File: MultiTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testQueries() throws Exception {
	Session s = openSession();
	Long id = ( Long ) s.save( new TrivialClass() );

	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();
	TrivialClass tc = (TrivialClass) s.load(TrivialClass.class, id);
	s.find("from TrivialClass s where s.id = 2");
	s.find("select t.count from Top t");
	s.find("from Lower s where s.another.name='name'");
	s.find("from Lower s where s.yetanother.name='name'");
	s.find("from Lower s where s.yetanother.name='name' and s.yetanother.foo is null");
	s.find("from Top s where s.count=1");
	s.find("select s.count from Top s, Lower ls where ls.another=s");
	s.find("select elements(ls.bag), elements(ls.set) from Lower ls");
	s.iterate("from Lower");
	s.iterate("from Top");
	s.delete(tc);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 4
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testProxyArray() throws Exception {
	Session s = openSession();
	GlarchProxy g = new Glarch();
	Glarch g1 = new Glarch();
	Glarch g2 = new Glarch();
	g.setProxyArray( new GlarchProxy[] { g1, g2 } );
	Glarch g3 = new Glarch();
	s.save(g3);
	g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
	Set set = new HashSet();
	set.add(g1);
	set.add(g2);
	g.setProxySet(set);
	s.save(g);
	s.save(g1);
	s.save(g2);
	Serializable id = s.getIdentifier(g);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	g = (GlarchProxy) s.load(Glarch.class, id);
	assertTrue( "array of proxies", g.getProxyArray().length==2 );
	assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
	assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
	assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
	assertTrue( "set of proxies", g.getProxySet().size()==2 );
	Iterator iter = s.iterate("from Glarch g");
	while ( iter.hasNext() ) {
		iter.next();
		iter.remove();
	}

	s.flush();
	s.connection().commit();
	s.disconnect();
	SerializationHelper.deserialize( SerializationHelper.serialize(s) );
	s.close();
}
 
Example 5
Source File: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCollectionQuery() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof MckoiDialect) ) {
		s.iterate("FROM Master m WHERE NOT EXISTS ( FROM m.details d WHERE NOT d.i=5 )");
		s.iterate("FROM Master m WHERE NOT 5 IN ( SELECT d.i FROM m.details AS d )");
	}
	s.iterate("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");
	s.find("SELECT m FROM Master m JOIN m.details d WHERE d.i=5");
	s.find("SELECT m.id FROM Master AS m JOIN m.details AS d WHERE d.i=5");
	t.commit();
	s.close();
}
 
Example 6
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testNewFlushing() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	Baz baz = new Baz();
	baz.setDefaults();
	s.save(baz);
	s.flush();
	baz.getStringArray()[0] = "a new value";
	Iterator iter = s.iterate("from Baz baz");//no flush
	assertTrue( iter.next()==baz );
	iter = s.iterate("select elements(baz.stringArray) from Baz baz");
	boolean found = false;
	while ( iter.hasNext() ) {
		if ( iter.next().equals("a new value") ) found = true;
	}
	assertTrue(found);
	baz.setStringArray(null);
	s.iterate("from Baz baz"); //no flush
	iter = s.iterate("select elements(baz.stringArray) from Baz baz");
	assertTrue( !iter.hasNext() );
	baz.getStringList().add("1E1");
	iter = s.iterate("from Foo foo");//no flush
	assertTrue( !iter.hasNext() );
	iter = s.iterate("select elements(baz.stringList) from Baz baz");
	found = false;
	while ( iter.hasNext() ) {
		if ( iter.next().equals("1E1") ) found = true;
	}
	assertTrue(found);
	baz.getStringList().remove("1E1");
	iter = s.iterate("select elements(baz.stringArray) from Baz baz"); //no flush
	iter = s.iterate("select elements(baz.stringList) from Baz baz");
	found = false;
	while ( iter.hasNext() ) {
		if ( iter.next().equals("1E1") ) found = true;
	}
	assertTrue(!found);

	List newList = new ArrayList();
	newList.add("value");
	baz.setStringList(newList);
	iter = s.iterate("from Foo foo");//no flush
	baz.setStringList(null);
	iter = s.iterate("select elements(baz.stringList) from Baz baz");
	assertTrue( !iter.hasNext() );

	baz.setStringList(newList);
	iter = s.iterate("from Foo foo");//no flush
	iter = s.iterate("select elements(baz.stringList) from Baz baz");
	assertTrue( iter.hasNext() );

	s.delete(baz);
	txn.commit();
	s.close();
}
 
Example 7
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testRecursiveLoad() throws Exception {
	//Non polymorphic class (there is an implementation optimization
	//being tested here)
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	GlarchProxy last = new Glarch();
	s.save(last);
	last.setOrder( (short) 0 );
	for (int i=0; i<5; i++) {
		GlarchProxy next = new Glarch();
		s.save(next);
		last.setNext(next);
		last = next;
		last.setOrder( (short) (i+1) );
	}
	Iterator iter = s.iterate("from Glarch g");
	while ( iter.hasNext() ) {
		iter.next();
	}
	List list = s.find("from Glarch g");
	assertTrue( "recursive find", list.size()==6 );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	list = s.find("from Glarch g");
	assertTrue( "recursive iter", list.size()==6 );
	list = s.find("from Glarch g where g.next is not null");
	assertTrue( "recursive iter", list.size()==5 );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	iter = s.iterate("from Glarch g order by g.order asc");
	while ( iter.hasNext() ) {
		GlarchProxy g = (GlarchProxy) iter.next();
		assertTrue( "not null", g!=null );
		iter.remove();
	}
	txn.commit();
	s.close();

	//Same thing but using polymorphic class (no optimisation possible):
	s = openSession();
	txn = s.beginTransaction();
	FooProxy flast = new Bar();
	s.save(flast);
	flast.setString( "foo0" );
	for (int i=0; i<5; i++) {
		FooProxy foo = new Bar();
		s.save(foo);
		flast.setFoo(foo);
		flast = flast.getFoo();
		flast.setString( "foo" + (i+1) );
	}
	iter = s.iterate("from Foo foo");
	while ( iter.hasNext() ) {
		iter.next();
	}
	list = s.find("from Foo foo");
	assertTrue( "recursive find", list.size()==6 );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	list = s.find("from Foo foo");
	assertTrue( "recursive iter", list.size()==6 );
	iter = list.iterator();
	while ( iter.hasNext() ) {
		assertTrue( "polymorphic recursive load", iter.next() instanceof BarProxy );
	}
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	iter = s.iterate("from Foo foo order by foo.string asc");
	while ( iter.hasNext() ) {
		BarProxy bar = (BarProxy) iter.next();
		assertTrue( "not null", bar!=null );
		iter.remove();
	}
	txn.commit();
	s.close();
}
 
Example 8
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testMultiColumnQueries() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	Foo foo = new Foo();
	s.save(foo);
	Foo foo1 = new Foo();
	s.save(foo1);
	foo.setFoo(foo1);
	List l = s.find("select parent, child from Foo parent, Foo child where parent.foo = child");
	assertTrue( "multi-column find", l.size()==1 );

	Iterator rs = s.iterate("select count(distinct child.id), count(distinct parent.id) from Foo parent, Foo child where parent.foo = child");
	Object[] row = (Object[]) rs.next();
	assertTrue( "multi-column count", ( (Long) row[0] ).intValue()==1 );
	assertTrue( "multi-column count", ( (Long) row[1] ).intValue()==1 );
	assertTrue( !rs.hasNext() );

	rs = s.iterate("select child.id, parent.id, child.long from Foo parent, Foo child where parent.foo = child");
	row = (Object[]) rs.next();
	assertTrue( "multi-column id", row[0].equals( foo.getFoo().getKey() ) );
	assertTrue( "multi-column id", row[1].equals( foo.getKey() ) );
	assertTrue( "multi-column property", row[2].equals( foo.getFoo().getLong() ) );
	assertTrue( !rs.hasNext() );

	rs = s.iterate("select child.id, parent.id, child.long, child, parent.foo from Foo parent, Foo child where parent.foo = child");
	row = (Object[]) rs.next();
	assertTrue(
		foo.getFoo().getKey().equals( row[0] ) &&
		foo.getKey().equals( row[1] ) &&
		foo.getFoo().getLong().equals( row[2] ) &&
		row[3] == foo.getFoo() &&
		row[3]==row[4]
	);
	assertTrue( !rs.hasNext() );

	row = (Object[]) l.get(0);
	assertTrue( "multi-column find", row[0]==foo && row[1]==foo.getFoo() );
	txn.commit();
	s.close();
	
	s = openSession();
	txn = s.beginTransaction();
	Iterator iter = s.iterate("select parent, child from Foo parent, Foo child where parent.foo = child and parent.string='a string'");
	int deletions=0;
	while ( iter.hasNext() ) {
		Object[] pnc = (Object[]) iter.next();
		s.delete( pnc[0] );
		s.delete( pnc[1] );
		deletions++;
	}
	assertTrue("multi-column iterate", deletions==1);
	txn.commit();
	s.close();
}
 
Example 9
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testAutoFlushCollections() throws Exception {
	Session s = openSession();
	Transaction tx = s.beginTransaction();
	Baz baz = new Baz();
	baz.setDefaults();
	s.save(baz);
	tx.commit();
	s.close();

	s = openSession();
	tx = s.beginTransaction();
	baz = (Baz) s.load(Baz.class, baz.getCode());
	baz.getStringArray()[0] = "bark";
	Iterator i = s.iterate("select elements(baz.stringArray) from Baz baz");
	boolean found = false;
	while ( i.hasNext() ) {
		if ( "bark".equals( i.next() ) ) found = true;
	}
	assertTrue(found);
	baz.setStringArray(null);
	i = s.iterate("select distinct elements(baz.stringArray) from Baz baz");
	assertTrue( !i.hasNext() );
	baz.setStringArray( new String[] { "foo", "bar" } );
	i = s.iterate("select elements(baz.stringArray) from Baz baz");
	assertTrue( i.hasNext() );

	Foo foo = new Foo();
	s.save(foo);
	s.flush();
	baz.setFooArray( new Foo[] {foo} );

	i = s.iterate("select foo from Baz baz join baz.fooArray foo");
	found = false;
	while ( i.hasNext() ) {
		if ( foo==i.next() ) found = true;
	}
	assertTrue(found);

	baz.getFooArray()[0] = null;
	i = s.iterate("select foo from Baz baz join baz.fooArray foo");
	assertTrue( !i.hasNext() );
	baz.getFooArray()[0] = foo;
	i = s.iterate("select elements(baz.fooArray) from Baz baz");
	assertTrue( i.hasNext() );

	if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof HSQLDialect) && !(getDialect() instanceof InterbaseDialect) && !(getDialect() instanceof PointbaseDialect) && !(getDialect() instanceof SAPDBDialect) )  {
		baz.getFooArray()[0] = null;
		i = s.iterate(
			"from Baz baz where ? in elements(baz.fooArray)",
			foo, Hibernate.entity(Foo.class)
		);
		assertTrue( !i.hasNext() );
		baz.getFooArray()[0] = foo;
		i = s.iterate(
			"select foo from Foo foo where foo in "
			+ "(select elt from Baz baz join baz.fooArray elt)"
		);
		assertTrue( i.hasNext() );
	}
	s.delete(foo);
	s.delete(baz);
	tx.commit();
	s.close();

}
 
Example 10
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCompositeID() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	Fum fum = new Fum( fumKey("fum") );
	fum.setFum("fee fi fo");
	s.save(fum);
	assertTrue( "load by composite key", fum==s.load( Fum.class, fumKey("fum") ) );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	fum = (Fum) s.load( Fum.class, fumKey("fum"), LockMode.UPGRADE );
	assertTrue( "load by composite key", fum!=null );

	Fum fum2 = new Fum( fumKey("fi") );
	fum2.setFum("fee fo fi");
	fum.setFo(fum2);
	s.save(fum2);
	assertTrue(
		"find composite keyed objects",
		s.find("from Fum fum where not fum.fum='FRIEND'").size()==2
	);
	assertTrue(
		"find composite keyed object",
		s.find("select fum from Fum fum where fum.fum='fee fi fo'").get(0)==fum
	);
	fum.setFo(null);
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	Iterator 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==2 );
	txn.commit();
	s.close();
}
 
Example 11
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCompositeIDQuery() throws Exception {
	Session s = openSession();
	Fum fee = new Fum( fumKey("fee",true) );
	fee.setFum("fee");
	s.save(fee);
	Fum fi = new Fum( fumKey("fi",true) );
	fi.setFum("fi");
	short fiShort = fi.getId().getShort();
	s.save(fi);
	Fum fo = new Fum( fumKey("fo",true) );
	fo.setFum("fo");
	s.save(fo);
	Fum fum = new Fum( fumKey("fum",true) );
	fum.setFum("fum");
	s.save(fum);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	// Try to find the Fum object "fo" that we inserted searching by the string in the id
	List vList = s.find("from Fum fum where fum.id.string='fo'"  );
	assertTrue( "find by composite key query (find fo object)", vList.size() == 1 );
	fum = (Fum)vList.get(0);
	assertTrue( "find by composite key query (check fo object)", fum.getId().getString().equals("fo") );

	// Try to find the Fum object "fi" that we inserted searching by the date in the id
	vList = s.find("from Fum fum where fum.id.short = ?",new Short(fiShort),Hibernate.SHORT);
	assertTrue( "find by composite key query (find fi object)", vList.size() == 1 );
	fi = (Fum)vList.get(0);
	assertTrue( "find by composite key query (check fi object)", fi.getId().getString().equals("fi") );

	// Make sure we can return all of the objects by searching by the date id
	assertTrue(
		"find by composite key query with arguments",
		s.find("from Fum fum where fum.id.date <= ? and not fum.fum='FRIEND'",new Date(),Hibernate.DATE).size()==4
	);
	s.flush();
	s.connection().commit();
	s.close();

	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();
}
 
Example 12
Source File: ParentChildTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testParentChild() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Parent p = new Parent();
	Child c = new Child();
	c.setParent(p);
	p.setChild(c);
	s.save(p);
	s.save(c);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	c = (Child) s.load( Child.class, new Long( c.getId() ) );
	p = c.getParent();
	assertTrue( "1-1 parent", p!=null );
	c.setCount(32);
	p.setCount(66);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	c = (Child) s.load( Child.class, new Long( c.getId() ) );
	p = c.getParent();
	assertTrue( "1-1 update", p.getCount()==66 );
	assertTrue( "1-1 update", c.getCount()==32 );
	assertTrue(
		"1-1 query",
		s.find("from Child c where c.parent.count=66").size()==1
	);
	assertTrue(
		"1-1 query",
		( (Object[]) s.find("from Parent p join p.child c where p.count=66").get(0) ).length==2
	);
	s.find("select c, c.parent from Child c order by c.parent.count");
	s.find("select c, c.parent from Child c where c.parent.count=66 order by c.parent.count");
	s.iterate("select c, c.parent, c.parent.count from Child c order by c.parent.count");
	assertTrue(
		"1-1 query",
		s.find("FROM Parent AS p WHERE p.count = ?", new Integer(66), Hibernate.INTEGER).size()==1
	);
	s.delete(c); s.delete(p);
	t.commit();
	s.close();
}