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

The following examples show how to use org.hibernate.classic.Session#createCriteria() . 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: QueryByExampleTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testSimpleQBE() throws Exception {
	deleteData();
    initData();

    Session s = openSession();

    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", "open sourc%", "open source1");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike();
    crit.add(ex);
    List result = crit.list();
    assertNotNull(result);
    assertEquals(1, result.size());

    t.commit();
    s.close();
}
 
Example 2
Source File: QueryByExampleTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testJunctionNotExpressionQBE() throws Exception {
    deleteData();
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", null, "ope%");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike();

    crit.add(Expression.or(Expression.not(ex), ex));

    List result = crit.list();
    assertNotNull(result);
    assertEquals(2, result.size());
    t.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 testCriteria() throws Exception {
	Session s = openSession();
	Transaction t = s.beginTransaction();
	Lower l = new Lower();
	s.save(l);
	assertTrue( l==s.createCriteria(Top.class).uniqueResult() );
	s.delete(l);
	s.flush();
	Criteria c = s.createCriteria(Lower.class);
	c.createCriteria("yetanother")
		.add( Expression.isNotNull("id") )
		.createCriteria("another");
	c.createCriteria("another").add( Expression.isNotNull("id") );
	c.list();
	t.commit();
	s.close();
}
 
Example 4
Source File: QueryByExampleTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testExcludingQBE() throws Exception {
    deleteData();
    initData();
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Componentizable master = getMaster("hibernate", null, "ope%");
    Criteria crit = s.createCriteria(Componentizable.class);
    Example ex = Example.create(master).enableLike()
        .excludeProperty("component.subComponent");
    crit.add(ex);
    List result = crit.list();
    assertNotNull(result);
    assertEquals(3, result.size());

    master = getMaster("hibernate", "ORM tool", "fake stuff");
    crit = s.createCriteria(Componentizable.class);
    ex = Example.create(master).enableLike()
        .excludeProperty("component.subComponent.subName1");
    crit.add(ex);
    result = crit.list();
    assertNotNull(result);
    assertEquals(1, result.size());
    t.commit();
    s.close();


}
 
Example 5
Source File: FooBarTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testSortables() throws Exception {
	Session s = openSession();
	Baz b = new Baz();
	b.setName("name");
	SortedSet ss = new TreeSet();
	ss.add( new Sortable("foo") );
	ss.add( new Sortable("bar") );
	ss.add( new Sortable("baz") );
	b.setSortablez(ss);
	s.save(b);
	s.flush();
	s.connection().commit();
	s.close();

	s = openSession();
	Criteria cr = s.createCriteria(Baz.class);
	cr.setFetchMode("topGlarchez", FetchMode.LAZY);
	List result = cr
		.addOrder( Order.asc("name") )
		.list();
	assertTrue( result.size()==1 );
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.connection().commit();
	s.close();

	s = openSession();
	result = s.createQuery("from Baz baz left join fetch baz.sortablez order by baz.name asc")
		.list();
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.connection().commit();
	s.close();

	s = openSession();
	result = s.createQuery("from Baz baz order by baz.name asc")
		.list();
	b = (Baz) result.get(0);
	assertTrue( b.getSortablez().size()==3 );
	assertEquals( ( (Sortable) b.getSortablez().iterator().next() ).getName(), "bar" );
	s.delete(b);
	s.flush();
	s.connection().commit();
	s.close();

}