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

The following examples show how to use org.hibernate.classic.Session#find() . 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: 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 2
Source File: ParentChildTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testQueryOneToOne() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Serializable id = s.save( new Parent() );
	assertTrue( s.find("from Parent p left join fetch p.child").size()==1 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Parent p = (Parent) s.createQuery("from Parent p left join fetch p.child").uniqueResult();
	assertTrue( p.getChild()==null );
	s.find("from Parent p join p.child c where c.x > 0");
	s.find("from Child c join c.parent p where p.x > 0");
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	s.delete( s.get(Parent.class, id) );
	t.commit();
	s.close();
}
 
Example 3
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testFetchList() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	s.save(baz);
	Foo foo = new Foo();
	s.save(foo);
	Foo foo2 = new Foo();
	s.save(foo2);
	s.flush();
	List list = new ArrayList();
	for ( int i=0; i<5; i++ ) {
		Fee fee = new Fee();
		list.add(fee);
	}
	baz.setFees(list);
	list = s.find("from Foo foo, Baz baz left join fetch baz.fees");
	assertTrue( Hibernate.isInitialized( ( (Baz) ( (Object[]) list.get(0) )[1] ).getFees() ) );
	s.delete(foo);
	s.delete(foo2);
	s.delete(baz);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 4
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testNoForeignKeyViolations() throws Exception {
	Session s = openSession();
	Glarch g1 = new Glarch();
	Glarch g2 = new Glarch();
	g1.setNext(g2);
	g2.setNext(g1);
	s.save(g1);
	s.save(g2);
	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();
	List l = s.find("from Glarch g where g.next is not null");
	s.delete( l.get(0) );
	s.delete( l.get(1) );
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 5
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testFetchInitializedCollectionDupe() throws Exception {
	Session s = openSession();
	Baz baz = new Baz();
	Collection fooBag = new ArrayList();
	fooBag.add( new Foo() );
	fooBag.add( new Foo() );
	baz.setFooBag(fooBag);
	s.save(baz);
	s.flush();
	fooBag = baz.getFooBag();
	s.find("from Baz baz left join fetch baz.fooBag");
	assertTrue( Hibernate.isInitialized(fooBag) );
	assertTrue( fooBag==baz.getFooBag() );
	assertTrue( baz.getFooBag().size()==2 );
	s.connection().commit();
	s.close();

	s = openSession();
	baz = (Baz) s.load( Baz.class, baz.getCode() );
	Object bag = baz.getFooBag();
	assertFalse( Hibernate.isInitialized(bag) );
	s.find("from Baz baz left join fetch baz.fooBag");
	assertTrue( Hibernate.isInitialized(bag) );
	assertTrue( bag==baz.getFooBag() );
	assertTrue( baz.getFooBag().size()==2 );
	s.delete(baz);
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 6
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSqlFunctionAsAlias() throws Exception {
	String functionName = locateAppropriateDialectFunctionNameForAliasTest();
	if (functionName == null) {
		log.info("Dialect does not list any no-arg functions");
		return;
	}

	log.info("Using function named [" + functionName + "] for 'function as alias' test");
	String query = "select " + functionName + " from Simple as " + functionName + " where " + functionName + ".id = 10";

	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();
	List result = s.find(query);
	assertTrue( result.size() == 1 );
	assertTrue(result.get(0) instanceof Simple);
	s.delete( result.get(0) );
	t.commit();
	s.close();
}
 
Example 7
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 8
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCallback() throws Exception {
	Session s = openSession();
	Qux q = new Qux("0");
	s.save(q);
	q.setChild( new Qux("1") );
	s.save( q.getChild() );
	Qux q2 = new Qux("2");
	q2.setChild( q.getChild() );
	Qux q3 = new Qux("3");
	q.getChild().setChild(q3);
	s.save(q3);
	Qux q4 = new Qux("4");
	q4.setChild(q3);
	s.save(q4);
	s.save(q2);
	s.flush();
	s.connection().commit();
	s.close();
	s = openSession();
	List l = s.find("from Qux");
	assertTrue( "", l.size()==5);
	s.delete( l.get(0) );
	s.delete( l.get(1) );
	s.delete( l.get(2) );
	s.delete( l.get(3) );
	s.delete( l.get(4) );
	s.flush();
	s.connection().commit();
	s.close();
}
 
Example 9
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testSqlFunctionAsAlias() throws Exception {
	String functionName = locateAppropriateDialectFunctionNameForAliasTest();
	if (functionName == null) {
		log.info("Dialect does not list any no-arg functions");
		return;
	}

	log.info("Using function named [" + functionName + "] for 'function as alias' test");
	String query = "select " + functionName + " from Simple as " + functionName + " where " + functionName + ".id = 10";

	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();
	List result = s.find(query);
	assertTrue( result.size() == 1 );
	assertTrue(result.get(0) instanceof Simple);
	s.delete( result.get(0) );
	t.commit();
	s.close();
}
 
Example 10
Source File: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNotNullDiscriminator() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Up up = new Up();
	up.setId1("foo");
	up.setId2(123l);
	Down down = new Down();
	down.setId1("foo");
	down.setId2(321l);
	down.setValue(12312312l);
	s.save(up);
	s.save(down);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	List list = s.find("from Up up order by up.id2 asc");
	assertTrue( list.size()==2 );
	assertFalse( list.get(0) instanceof Down );
	assertTrue( list.get(1) instanceof Down );
	list = s.find("from Down down");
	assertTrue( list.size()==1 );
	assertTrue( list.get(0) instanceof Down );
	//list = s.find("from Up down where down.class = Down");
	assertTrue( list.size()==1 );
	assertTrue( list.get(0) instanceof Down );
	s.delete("from Up up");
	t.commit();
	s.close();

}
 
Example 11
Source File: MultiTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNarrow() throws Exception {
	Session s = openSession();
	s.find("from Po po, Lower low where low.mypo = po");
	s.find("from Po po join po.set as sm where sm.amount > 0");
	s.find("from Po po join po.top as low where low.foo = 'po'");
	s.connection().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 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();
}
 
Example 13
Source File: CustomSQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testCRUD() throws HibernateException, SQLException {

		if ( getDialect() instanceof HSQLDialect ) return;
		if ( getDialect() instanceof MySQLDialect ) return;

		Person p = new Person();

		p.setName("Max");
		p.setLastName("Andersen");
		p.setNationalID("110974XYZ�");
		p.setAddress("P. P. Street 8");

		Session s = openSession();

		s.save(p);
		s.flush();

		s.connection().commit();
		s.close();

		getSessions().evict(Person.class);
		s = openSession();

		Person p2 = (Person) s.get(Person.class, p.getId());
		assertNotSame(p, p2);
		assertEquals(p2.getId(),p.getId());
		assertEquals(p2.getLastName(),p.getLastName());
		s.flush();

		List list = s.find("select p from Party as p");
		assertTrue(list.size() == 1);

		s.connection().commit();
		s.close();

		s = openSession();

		list = s.find("select p from Person as p where p.address = 'L�rkev�nget 1'");
		assertTrue(list.size() == 0);
		p.setAddress("L�rkev�nget 1");
		s.update(p);
		list = s.find("select p from Person as p where p.address = 'L�rkev�nget 1'");
		assertTrue(list.size() == 1);
		list = s.find("select p from Party as p where p.address = 'P. P. Street 8'");
		assertTrue(list.size() == 0);

		s.delete(p);
		list = s.find("select p from Person as p");
		assertTrue(list.size() == 0);

		s.connection().commit();
		s.close();


	}
 
Example 14
Source File: MultiTableTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSubclassCollection() throws Exception {
	//if ( getDialect() instanceof HSQLDialect ) return; //TODO: figure out why!?
	Session s = openSession();
	SubMulti sm = new SubMulti();
	SubMulti sm1 = new SubMulti();
	SubMulti sm2 = new SubMulti();
	ArrayList list = new ArrayList();
	ArrayList anotherList = new ArrayList();
	sm.setChildren(list);
	sm.setMoreChildren(anotherList);
	sm.setExtraProp("foo");
	list.add(sm1);
	list.add(sm2);
	anotherList.add(sm1);
	anotherList.add(sm2);
	sm1.setParent(sm);
	sm2.setParent(sm);
	Serializable id = s.save(sm);
	s.save(sm1);
	s.save(sm2);
	s.flush();
	s.connection().commit();
	s.close();

	getSessions().evict(SubMulti.class);

	s = openSession();
	s.connection().createStatement().executeQuery(
		"select * from leafsubsubclass sm, nonleafsubclass m, rootclass s where sm.sid=m.sid and sm.sid=s.id1_ and sm.sid=1"
	).next();
	assertTrue( s.find("select s from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==2 );
	s.find("select c from SubMulti sm join sm.children c");
	assertTrue( s.find("select elements(sm.children) from SubMulti as sm").size()==2 );
	assertTrue( s.find("select distinct sm from SubMulti as sm join sm.children as s where s.amount>-1 and s.name is null").size()==1 );
	sm = (SubMulti) s.load(SubMulti.class, id);
	assertTrue( sm.getChildren().size()==2 );
	assertEquals(
		s.filter( sm.getMoreChildren(), "select count(*) where this.amount>-1 and this.name is null" ).iterator().next(),
		new Long(2)
	);
	assertEquals( "FOO", sm.getDerived() );
	assertSame(
		s.iterate("select distinct s from SubMulti s where s.moreChildren[1].amount < 1.0").next(),
		sm
	);
	assertTrue( sm.getMoreChildren().size()==2 );
	s.delete(sm);
	Iterator iter = sm.getChildren().iterator();
	while ( iter.hasNext() ) s.delete( iter.next() );
	s.flush();
	s.connection().commit();
	s.close();

}
 
Example 15
Source File: ABCProxyTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testOneToOne() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	A a = new A();
	E d1 = new E();
	C1 c = new C1();
	E d2 = new E();
	a.setForward(d1);
	d1.setReverse(a);
	c.setForward(d2);
	d2.setReverse(c);
	Serializable aid = s.save(a);
	Serializable d2id = s.save(d2);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	List l = s.find( "from E e, A a where e.reverse = a.forward and a = ?", a, Hibernate.entity(A.class) );
	assertTrue( l.size()==1 );
	l = s.find( "from E e join fetch e.reverse" );
	assertTrue( l.size()==2 );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	l = s.find( "from E e" );
	assertTrue( l.size()==2 );
	E e = (E) l.get(0);
	assertTrue( e==e.getReverse().getForward() );
	e = (E) l.get(1);
	assertTrue( e==e.getReverse().getForward() );
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	a = (A) s.load(A.class, aid);
	d2 = (E) s.load(E.class, d2id);
	assertTrue( a==a.getForward().getReverse() );
	assertTrue( d2==d2.getReverse().getForward() );
	s.delete(a);
	s.delete( a.getForward() );
	s.delete(d2);
	s.delete( d2.getReverse() );
	t.commit();

	s = openSession();
	t = s.beginTransaction();
	l = s.find( "from E e" );
	assertTrue( l.size()==0 );
	t.commit();
	s.close();
}
 
Example 16
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testFind() throws Exception {
	Session s = openSession();
	Transaction txn = s.beginTransaction();

	Bar bar = new Bar();
	s.save(bar);
	bar.setBarString("bar bar");
	bar.setString("xxx");
	Foo foo = new Foo();
	s.save(foo);
	foo.setString("foo bar");
	s.save( new Foo() );
	s.save( new Bar() );
	List list1 = s.find("select foo from Foo foo where foo.string='foo bar'");
	assertTrue( "find size", list1.size()==1 );
	assertTrue( "find ==", list1.get(0)==foo );
	List list2 = s.find("from Foo foo order by foo.string, foo.date");
	assertTrue( "find size", list2.size()==4 );

	list1 = s.find("from Foo foo where foo.class='B'");
	assertTrue( "class special property", list1.size()==2);
	list1 = s.find("from Foo foo where foo.class=Bar");
	assertTrue( "class special property", list1.size()==2);
	list1 = s.find("from Foo foo where foo.class=Bar");
	list2 = s.find("select bar from Bar bar, Foo foo where bar.string = foo.string and not bar=foo");
	assertTrue( "class special property", list1.size()==2);
	assertTrue( "select from a subclass", list2.size()==1);
	Trivial t = new Trivial();
	s.save(t);
	txn.commit();
	s.close();

	s = openSession();
	txn = s.beginTransaction();
	list1 = s.find("from Foo foo where foo.string='foo bar'");
	assertTrue( "find size", list1.size()==1 );
	// There is an interbase bug that causes null integers to return as 0, also numeric precision is <= 15
	assertTrue( "find equals", ( (Foo) list1.get(0) ).equalsFoo(foo) );
	list2 = s.find("select foo from Foo foo");
	assertTrue( "find size", list2.size()==5 );
	List list3 = s.find("from Bar bar where bar.barString='bar bar'");
	assertTrue( "find size", list3.size()==1 );
	assertTrue( "find same instance", list2.contains( list1.get(0) ) && list2.contains( list2.get(0) ) );
	assertTrue( s.find("from Trivial").size()==1 );
	s.delete("from Trivial");

	list2 = s.find("from Foo foo where foo.date = ?", new java.sql.Date(123), Hibernate.DATE);
	assertTrue ( "find by date", list2.size()==4 );
	Iterator iter = list2.iterator();
	while ( iter.hasNext() ) {
		s.delete( iter.next() );
	}
	list2 = s.find("from Foo foo");
	assertTrue( "find deleted", list2.size()==0);
	txn.commit();
	s.close();
}
 
Example 17
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void ntestAssociationId() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Bar bar = new Bar();
	String id = (String) s.save(bar);
	MoreStuff more = new MoreStuff();
	more.setName("More Stuff");
	more.setIntId(12);
	more.setStringId("id");
	Stuff stuf = new Stuff();
	stuf.setMoreStuff(more);
	more.setStuffs( new ArrayList() );
	more.getStuffs().add(stuf);
	stuf.setFoo(bar);
	stuf.setId(1234);
	stuf.setProperty( TimeZone.getDefault() );
	s.save(more);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	assertTrue( s.find(
		"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.id.intId = ? and s.moreStuff.id.stringId = ?",
		new Object[] { bar, new Long(1234), new Integer(12), "id" },
		new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.INTEGER, Hibernate.STRING }
	).size()==1 );
	assertTrue( s.find(
		"from Stuff as s where s.foo.id = ? and s.id.id = ? and s.moreStuff.name = ?",
		new Object[] { bar, new Long(1234), "More Stuff" },
		new Type[] { Hibernate.entity(Foo.class), Hibernate.LONG, Hibernate.STRING }
	).size()==1 );
	s.find("from Stuff as s where s.foo.string is not null");
	assertTrue(
		s.find("from Stuff as s where s.foo > '0' order by s.foo").size()==1
	);
	//s.createCriteria(Stuff.class).createCriteria("id.foo").add( Expression.isNull("foo") ).list();
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	FooProxy foo = (FooProxy) s.load(Foo.class, id);
	s.load(more, more);
	t.commit();
	s.close();

	s = openSession();
	t = s.beginTransaction();
	Stuff stuff = new Stuff();
	stuff.setFoo(foo);
	stuff.setId(1234);
	stuff.setMoreStuff(more);
	s.load(stuff, stuff);
	assertTrue( stuff.getProperty().equals( TimeZone.getDefault() ) );
	assertTrue( stuff.getMoreStuff().getName().equals("More Stuff") );
	s.delete("from MoreStuff");
	s.delete("from Foo foo");
	t.commit();
	s.close();
}
 
Example 18
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 19
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 20
Source File: FumTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();
}