1。文件的映射。由于使用 MyEclipse(傻瓜版^_^) 所以 hibernate工具(extensions,Middlegen,tools等)一直没好好研究这就不献丑了。
2。Criteria
1)查询条件通过 Criteria.add 添加 Expression 用于描述条件
Expression.( and or like in le lt ..... )
参见: Criteria查询
2)criteria.setFirstResult(100);
criteria.setMaxResults(20); //检索范围
criteria.addOrder(Order.asc("name")); //排序
.add( Expression.sql("lower($alias.name) like lower(?)", "Fritz%", Hibernate.STRING) //直接嵌入SQL
3)Cat cat = new Cat();
....
List results = session.createCriteria(Cat.class).add( Example.create(cat) ).list(); //根据对象查询
3。HQL 完全面向对象的,具备继承、多态和关联等特性。
1)参见: HQL
2)内连接,inner join
左外连接,left outer join
右外连接,right outer join
http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
代表性的语句
//简单的 结果: 2 liukaiyi:heha
//给出对象 结果 : heha
//给出Object 结果:heha xx
//给出 Map heha xx
Query qu = se.createQuery( " select new map( h.name as hn,f.name as fn)from Hql h join h.fkSet f where f.name='xx' " );
List list = qu.list() ;
tr.commit();
System.out.println(list.size());
Map map = ((Map)list.get( 0 ));
System.out.println( map.get( " hn " ) );
}
//报表语句 结果: 1
//集合内 结果 liukaiyi:heha
// 元素集(elements与indices 函数) 可以使用 any, some, all, exists, in
// EG: from Player p where 3 > all elements(p.scores)
//使用 javaBean 绑定
2。Criteria
1)查询条件通过 Criteria.add 添加 Expression 用于描述条件
Expression.( and or like in le lt ..... )
参见: Criteria查询
2)criteria.setFirstResult(100);
criteria.setMaxResults(20); //检索范围
criteria.addOrder(Order.asc("name")); //排序
.add( Expression.sql("lower($alias.name) like lower(?)", "Fritz%", Hibernate.STRING) //直接嵌入SQL
3)Cat cat = new Cat();
....
List results = session.createCriteria(Cat.class).add( Example.create(cat) ).list(); //根据对象查询
3。HQL 完全面向对象的,具备继承、多态和关联等特性。
1)参见: HQL
2)内连接,inner join
左外连接,left outer join
右外连接,right outer join
http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
代表性的语句
Hql.hbm.xml
< set name ="fkSet" inverse ="true" >
< key column ="id" />
< one-to-many class ="Fk" />
</ set >
SQL
alter table fk add constraint FK_hf foreign key(id) references hql(id)
mysql> select * from hql;
+----+----------+
| id | name |
+----+----------+
| 0 | liukaiyi |
| 1 | heha |
+----+----------+
2 rows in set (0.00 sec)
mysql> select * from fk;
+----+------+
| id | name |
+----+------+
| 0 | yy |
| 1 | xx |
+----+------+
2 rows in set (0.00 sec)
< set name ="fkSet" inverse ="true" >
< key column ="id" />
< one-to-many class ="Fk" />
</ set >
SQL
alter table fk add constraint FK_hf foreign key(id) references hql(id)
mysql> select * from hql;
+----+----------+
| id | name |
+----+----------+
| 0 | liukaiyi |
| 1 | heha |
+----+----------+
2 rows in set (0.00 sec)
mysql> select * from fk;
+----+------+
| id | name |
+----+------+
| 0 | yy |
| 1 | xx |
+----+------+
2 rows in set (0.00 sec)
//简单的 结果: 2 liukaiyi:heha
Query qu
=
se.createQuery(
"
select h.name
"
+
" from Hql h , Fk f " +
" where h.id = f.id " );
List list = qu.list() ;
tr.commit();
System.out.println( list.size() );
System.out.println( list.get( 0 ) + " : " + list.get( 1 ) );
" from Hql h , Fk f " +
" where h.id = f.id " );
List list = qu.list() ;
tr.commit();
System.out.println( list.size() );
System.out.println( list.get( 0 ) + " : " + list.get( 1 ) );
//给出对象 结果 : heha
Query qu
=
se.createQuery(
"
from Hql h where h.fkSet.name='xx'
"
);
((Hql)list.get( 0 )).getName() ;
((Hql)list.get( 0 )).getName() ;
//给出Object 结果:heha xx
Query qu
=
se.createQuery(
"
select h.name,f.name from Hql h join h.fkSet f where f.name='xx'
"
);
List list = qu.list() ;
tr.commit();
System.out.println(list.size());
Object[] objs = (Object[]) list.get( 0 ) ;
System.out.println( objs[ 0 ] + " : " + objs[ 1 ] );
List list = qu.list() ;
tr.commit();
System.out.println(list.size());
Object[] objs = (Object[]) list.get( 0 ) ;
System.out.println( objs[ 0 ] + " : " + objs[ 1 ] );
//给出 Map heha xx
Query qu = se.createQuery( " select new map( h.name as hn,f.name as fn)from Hql h join h.fkSet f where f.name='xx' " );
List list = qu.list() ;
tr.commit();
System.out.println(list.size());
Map map = ((Map)list.get( 0 ));
System.out.println( map.get( " hn " ) );
}
//报表语句 结果: 1
Query qu
=
se.createQuery(
"
select count(*) from Hql h join h.fkSet f
"
+
" where h.name='heha' and h.id=f.id " +
" group by h.name " );
List list = qu.list() ;
tr.commit();
System.out.println( list.get( 0 ) );
" where h.name='heha' and h.id=f.id " +
" group by h.name " );
List list = qu.list() ;
tr.commit();
System.out.println( list.get( 0 ) );
//集合内 结果 liukaiyi:heha
// 元素集(elements与indices 函数) 可以使用 any, some, all, exists, in
// EG: from Player p where 3 > all elements(p.scores)
+----+----------+
| id | name |
+----+----------+
| 0 | liukaiyi |
| 1 | heha |
| 3 | oo |
+----+----------+
Query qu = se.createQuery( " select h.name " +
" from Hql h " +
" where h.id in elements(h.fkSet.id) " );
List list = qu.list() ;
tr.commit();
System.out.println( list.get( 0 ) + " : " + list.get( 1 ) );
| id | name |
+----+----------+
| 0 | liukaiyi |
| 1 | heha |
| 3 | oo |
+----+----------+
Query qu = se.createQuery( " select h.name " +
" from Hql h " +
" where h.id in elements(h.fkSet.id) " );
List list = qu.list() ;
tr.commit();
System.out.println( list.get( 0 ) + " : " + list.get( 1 ) );
//使用 javaBean 绑定
Query q
=
s.createQuery(
"
from foo Foo as foo where foo.name=:name and foo.size=:size
"
);
q.setProperties(fooBean); // fooBean包含方法getName()与getSize()
List foos = q.list();
q.setProperties(fooBean); // fooBean包含方法getName()与getSize()
List foos = q.list();