Hibernate Tips

 
1.     from Bid bid where bid.amount between 1 and 10
 
session.createCriteria(Bid.class)
.add(Restrictions.between("amount",
new BigDecimal(1),
new BigDecimal(10))
).list();
 
 
2.     from Bid bid where bid.amount > 100
 
session.createCriteria(Bid.class)
.add(Restrictions.gt("amount", new BigDecimal(100) ) )
.list();
 
 
3.     from User u where u.email in ( "foo@hibernate.org", "bar@hibernate.org" )
 
String[] emails = { "foo@hibernate.org", "bar@hibernate.org" };
session.createCriteria(User.class)
.add(Restrictions.in("email", emails) )
.list();
 
 
4.     from User u where u.email is null
 
session.createCriteria(User.class)
.add(Restrictions.isNull("email") )
.list();
 
from User u where u.email is not null
 
session.createCriteria(User.class)
.add( Restrictions.isNotNull("email") )
.list();
 
 
5.     from User u where u.firstname like "G%"
 
session.createCriteria(User.class)
.add( Restrictions.like("firstname", "G%") )
.list();
session.createCriteria(User.class)
.add( Restrictions.like("firstname", "G", MatchMode.START) )
.list();
 
The allowed MatchModes are START, END, ANYWHERE, and EXACT.
 
 
6.     from User u where lower(u.email) = 'foo@hibernate.org'
 
session.createCriteria(User.class)
.add( Restrictions.eq("email", "foo@hibernate.org").ignoreCase() )
.list();
 
HQL支持对于数据库函数的调用;Criteria查询不支持。
 
 
7.     from User user
where user.firstname like "G%" and user.lastname like "K%"
 
session.createCriteria(User.class)
.add( Restrictions.like("firstname", "G%") )
.add( Restrictions.like("lastname", "K%") )
 
 
8.     from User user
where ( user.firstname like "G%" and user.lastname like "K%" )
or user.email in ( "foo@hibernate.org", "bar@hibernate.org" )
 
Criteria crit = session.createCriteria(User.class)
.add(
Restrictions.or(
Restrictions.and(
Restrictions.like("firstname", "G%"),
Restrictions.like("lastname", "K%")
),
Restrictions.in("email", emails)
)
);
 
Criteria crit = session.createCriteria(User.class)
.add( Restrictions.disjunction()
.add( Restrictions.conjunction()
.add( Restrictions.like("firstname", "G%") )
.add( Restrictions.like("lastname", "K%") )
)
.add( Restrictions.in("email", emails) )
);
 
HQL string is much easier to understand.
Complex criteria queries are useful only when they’re created programmatically; for example, in the case of a complex search screen with several optional search criteria, we might have a CriteriaBuilderthat translates user restrictions to Criteria instances.
 
 
9.     from User u order by u.lastname asc, u.firstname asc
 
List results = session.createCriteria(User.class)
.addOrder( Order.asc("lastname") )
.addOrder( Order.asc("firstname") )
.list();
 
10.  from Item item
left join fetch item.bids
where item.description like '%gc%'
 
session.createCriteria(Item.class)
.setFetchMode("bids", FetchMode.EAGER)
.add( Restrictions.like("description", "gc", MatchMode.ANYWHERE) )
.list();
 
Both of these queries result in the following SQL:
select I.DESCRIPTION, I.CREATED, I.SUCCESSFUL_BID, B.BID_ID,
B.AMOUNT, B.ITEM_ID, B.CREATED
from ITEM I
left outer join BID B on I.ITEM_ID = B.ITEM_ID
where I.DESCRIPTION like '%gc%'
 
 
Appendix
Ø          外连接(out join):
内连接是关联的表的公共字段值必须相同,所有不同的值的记录都没有了。
外连接是值一个表的中的公共字段的值可以不与另一个表的公共字段值相同。一般时它是null.
 
任务:查询员工表,显示员工的项目,部门名称,部门位置,要求显示所有的员工,即使员工没有部门。
select a.ename,b.dname,b.loc
from emp a left outer join dept b
on a.deptno=b.deptno
 
注:此任务无法使用正常的内连接。因为有一个员工没有部门,它的部门编号为空。
 
 
Ø          外连接语法:
(1)左连接:取出左边的表的所有记录
select 子句
from 表1 left outer join 表2
on 表1.公共字段=表2.公共字段
(2) 右连接: 取出右边表的所有记录
select 子句
from 表1 right outer join 表2
on 表1.公共字段=表2.公共字段
(3) 全连接(左右连接):左右两边的表的记录都取。
select 子句
from 表1 full outer join 表2
on 表1.公共字段=表2.公共字段
 
select a.empno,a.deptno,b.deptno,b.dname
from emp a left outer join dept b
on a.deptno=b.deptno
 
select a.empno,a.deptno,b.deptno,b.dname
from emp a right outer join dept b
on a.deptno=b.deptno
 
select a.ename,b.dname,b.loc
from emp a full outer join dept b
on a.deptno=b.deptno
 
select a.dname, b.ename
from dept a full outer join emp b
on a.deptno=b.deptno
 
一般情况下,不使用上述的语法,而使用如下的语法:
select a.dname, b.ename
from dept a, emp b
where a.deptno(+)=b.deptno    ß--
一般情况情况下,(+)放在关联表的主键的一侧,才有实际的意义。
没有(+)的表的取所有的记录,关联的表如果有记录对应就显示关联的值,没有关联的值显示null.
 
但使用(+)的情况下,无法实现全连接。 因为无法在where 的左右同时使用(+).
select a.ename,b.dname
from emp a, dept b
where a.deptno=b.deptno(+)
下列的语句是无法通过的:
select a.ename,b.dname
from emp a, dept b
where a.deptno(+)=b.deptno(+)
 
 
文件已上传到百度网盘,附件中是下载地址。真正免积分免费完整版,绝不出现仅下载到部分章节,书中广 告页要求QQ联系支付宝购买完整版的流氓行为! 欢迎免积分下载更多本人独有网上难寻觅的 高清IT电子书:http://download.csdn.net/user/sinophp123 人无我有,人有我优,人优我廉!我的版本是全网最清晰的独家制作版本,还不要资源分。 同样一本书,下我的就可以了! 本人上传资料的原则: (1)如果CSDN和网上其他地方已随处可见高清下载,本人不再上传。 (2)如果网上已有我还上传,那么肯定是经重新制作,如不再缺页,清晰度更高,或者加上书签。 (3)每本书都经过逐页纯手工精心处理,包括清晰度的增强,水印的去除。当然最重要的,是尽量保证有书 签方便您浏览。 (4)如果是中译版,文件名前半部分是英文原版书名,后面是中译版书名。 如:“Implementing.Responsive.Design-响应式Web设计实践”。书名经反复校对绝无一字错漏。 (5)每本pdf书默认都是有书签的。来源不限“某星”网站,还有各大网络书店和出版社官网的页码信息。 凡无书签的pdf文件名均含“_no.bookmark”字样,如“HTML5程序开发范例宝典_no.bookmark”。请不要再 浪费时间去寻找书签,肯定没有,网上别人提供的下载版本也绝对不会有(除非您是出版社内部人员!), 没有人会浪费自己时间逐页输入章节页码来费时费力免费给您制作。 (6)只提供中文书籍,您不用担心下载的是英文原版。 (7)只提供完整版,绝不上传只有部分章节的所谓“迷你书”,“试读版”。 (8)书中绝不含广告页和水印LOGO。 Hibernate-A.Developer's.Notebook—Hibernate程序高手秘笈 [美] james elliott(著) | o'reilly taiwan公司编译(译) | 东南大学出版社 | 9787564106379 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值