下面列出了org.hibernate.classic.Session#createCriteria ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
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();
}
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();
}
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();
}
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();
}