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

The following examples show how to use org.hibernate.classic.Session#createQuery() . 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: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testPSCache() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();
	for ( int i=0; i<10; i++ ) s.save( new Foo() );
	Query q = s.createQuery("from Foo");
	q.setMaxResults(2);
	q.setFirstResult(5);
	assertTrue( q.list().size()==2 );
	q = s.createQuery("from Foo");
	assertTrue( q.list().size()==10 );
	assertTrue( q.list().size()==10 );
	q.setMaxResults(3);
	q.setFirstResult(3);
	assertTrue( q.list().size()==3 );
	q = s.createQuery("from Foo");
	assertTrue( q.list().size()==10 );
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	q = s.createQuery("from Foo");
	assertTrue( q.list().size()==10 );
	q.setMaxResults(5);
	assertTrue( q.list().size()==5 );
	s.delete("from Foo");
	txn.commit();
	s.close();

}
 
Example 2
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetProperties() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(simple);
	assertTrue( q.list().get(0)==simple );
	//misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
	Single single = new Single() { // trivial hack to test properties with arrays.
		String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); }
	};

	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	single.setSeveral(l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );


	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 3
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetPropertiesMap() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Map parameters = new HashMap();
	parameters.put("name", simple.getName());
	parameters.put("count", new Integer(simple.getCount()));
	
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(((Map)parameters));
	assertTrue( q.list().get(0)==simple );
	
	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	parameters.put("several", l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(parameters);
	assertTrue( q.list().get(0)==simple );


	parameters.put("stuff", l.toArray(new String[0]));
	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(parameters);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 4
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSetProperties() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save(simple, new Long(10) );
	Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count");
	q.setProperties(simple);
	assertTrue( q.list().get(0)==simple );
	//misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;)
	Single single = new Single() { // trivial hack to test properties with arrays.
		String[] getStuff() { return (String[]) getSeveral().toArray(new String[getSeveral().size()]); }
	};

	List l = new ArrayList();
	l.add("Simple 1");
	l.add("Slimeball");
	single.setSeveral(l);
	q = s.createQuery("from Simple s where s.name in (:several)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );


	q = s.createQuery("from Simple s where s.name in (:stuff)");
	q.setProperties(single);
	assertTrue( q.list().get(0)==simple );
	s.delete(simple);
	t.commit();
	s.close();
}
 
Example 5
Source File: BulkManipulationTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testIncorrectSyntax() {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	try {
		s.createQuery( "update Human set Human.description = 'xyz' where Human.id = 1 and Human.description is null" );
		fail( "expected failure" );
	}
	catch( QueryException expected ) {
		// ignore : expected behavior
	}
	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 testQueryLockMode() throws Exception {

		Session s = openSession();
		Transaction tx = s.beginTransaction();
		Bar bar = new Bar();
		s.save(bar);
		s.flush();
		bar.setString("changed");
		Baz baz = new Baz();
		baz.setFoo(bar);
		s.save(baz);
		Query q = s.createQuery("from Foo foo, Bar bar");
		if ( !(getDialect() instanceof DB2Dialect) ) {
			q.setLockMode("bar", LockMode.UPGRADE);
		}
		Object[] result = (Object[]) q.uniqueResult();
		Object b = result[0];
		assertTrue( s.getCurrentLockMode(b)==LockMode.WRITE && s.getCurrentLockMode( result[1] )==LockMode.WRITE );
		tx.commit();
		s.disconnect();

		s.reconnect();
		tx = s.beginTransaction();
		assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
		s.find("from Foo foo");
		assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
		q = s.createQuery("from Foo foo");
		q.setLockMode("foo", LockMode.READ);
		q.list();
		assertTrue( s.getCurrentLockMode(b)==LockMode.READ);
		s.evict(baz);
		tx.commit();
		s.disconnect();
		
		s.reconnect();
		tx = s.beginTransaction();
		assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
		s.delete( s.load( Baz.class, baz.getCode() ) );
		assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
		tx.commit();
		s.close();

		s = openSession();
		tx = s.beginTransaction();
		q = s.createQuery("from Foo foo, Bar bar, Bar bar2");
		if ( !(getDialect() instanceof DB2Dialect) ) {
			q.setLockMode("bar", LockMode.UPGRADE);
		}
		q.setLockMode("bar2", LockMode.READ);
		result = (Object[]) q.list().get(0);
		if ( !(getDialect() instanceof DB2Dialect) ) {
			assertTrue( s.getCurrentLockMode( result[0] )==LockMode.UPGRADE && s.getCurrentLockMode( result[1] )==LockMode.UPGRADE );
		}
		s.delete( result[0] );
		tx.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 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();
	Transaction txn = s.beginTransaction();
	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 );

	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();
}
 
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 testScrollableIterator() throws Exception {
	if ( getDialect() instanceof DB2Dialect || getDialect() instanceof OracleDialect || getDialect() instanceof SybaseDialect || getDialect() instanceof HSQLDialect ) {
		Session s = openSession();
		Transaction txn = s.beginTransaction();
		s.save( new Foo() );
		s.save( new Foo() );
		s.save( new Foo() );
		s.save( new Bar() );
		Query query = s.createQuery("select f, f.integer from Foo f");
		assertTrue( query.getReturnTypes().length==2 );
		ScrollableResults iter = query.scroll();
		assertTrue( iter.next() );
		assertTrue( iter.scroll(1) );
		FooProxy f2 = (FooProxy) iter.get()[0];
		assertTrue( f2!=null );
		assertTrue( iter.scroll(-1) );
		Object f1 = iter.get(0);
		iter.next();
		assertTrue( f1!=null && iter.get(0)==f2 );
		iter.getInteger(1);

		assertTrue( !iter.scroll(100) );
		assertTrue( iter.first() );
		assertTrue( iter.scroll(3) );
		Object f4 = iter.get(0);
		assertTrue( f4!=null );
		assertTrue( !iter.next() );
		assertTrue( iter.first() );
		assertTrue( iter.get(0)==f1 );
		assertTrue( iter.last() );
		assertTrue( iter.get(0)==f4 );
		assertTrue( iter.previous() );
		txn.commit();
		s.close();

		s = openSession();
		txn = s.beginTransaction();
		query = s.createQuery("select f, f.integer from Foo f");
		assertTrue( query.getReturnTypes().length==2 );
		iter = query.scroll();
		assertTrue( iter.next() );
		assertTrue( iter.scroll(1) );
		f2 = (FooProxy) iter.get()[0];
		assertTrue( f2!=null );
		assertTrue( f2.getString()!=null  && f2.getComponent().getImportantDates().length > 0 );
		assertTrue( iter.scroll(-1) );
		f1 = iter.get(0);
		iter.next();
		assertTrue( f1!=null && iter.get(0)==f2 );
		iter.getInteger(1);

		assertTrue( !iter.scroll(100) );
		assertTrue( iter.first() );
		assertTrue( iter.scroll(3) );
		f4 = iter.get(0);
		assertTrue( f4!=null );
		assertTrue( !iter.next() );
		assertTrue( iter.first() );
		assertTrue( iter.get(0)==f1 );
		assertTrue( iter.last() );
		assertTrue( iter.get(0)==f4 );
		assertTrue( iter.previous() );
		assertTrue( s.delete("from Foo")==4 );
		s.flush();
		assertTrue( s.find("from java.lang.Object").size()==0 );
		txn.commit();
		s.close();
	}
}
 
Example 9
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQuery() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=:name");
	q.setString("name", "Simple 2");
	q.setCacheable(true);
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
Example 10
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQueryRegion() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
Example 11
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQueryOnInsert() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s");
	List list = q.setCacheable(true).list();
	assertTrue( list.size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Simple simple2 = new Simple();
	simple2.setCount(133);
	s.save( simple2, new Long(12) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==2 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==2 );
	Iterator i = list.iterator();
	while ( i.hasNext() ) s.delete( i.next() );
	t.commit();
	s.close();

}
 
Example 12
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 13
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQuery() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=:name");
	q.setString("name", "Simple 2");
	q.setCacheable(true);
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
Example 14
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQueryRegion() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	q = s.createQuery("from Simple s where s.name=:name");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString("name", "Simple 1");
	assertTrue( q.list().size()==1 );
	simple = (Simple) q.list().get(0);

	q.setString("name", "Simple 2");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	simple.setName("Simple 2");
	assertTrue( q.list().size()==1 );
	assertTrue( q.list().size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.update( simple, new Long(10) );
	s.delete(simple);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s where s.name=?");
	q.setCacheRegion("foo");
	q.setCacheable(true);
	q.setString(0, "Simple 1");
	assertTrue( q.list().size()==0 );
	assertTrue( q.list().size()==0 );
	t.commit();
	s.close();
}
 
Example 15
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCachedQueryOnInsert() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Simple simple = new Simple();
	simple.setName("Simple 1");
	s.save( simple, new Long(10) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Query q = s.createQuery("from Simple s");
	List list = q.setCacheable(true).list();
	assertTrue( list.size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Simple simple2 = new Simple();
	simple2.setCount(133);
	s.save( simple2, new Long(12) );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==2 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	q = s.createQuery("from Simple s");
	list = q.setCacheable(true).list();
	assertTrue( list.size()==2 );
	Iterator i = list.iterator();
	while ( i.hasNext() ) s.delete( i.next() );
	t.commit();
	s.close();

}