hibernate findbyexample findbycriteria
来源:互联网 发布:淘宝考试答案50题 编辑:程序博客网 时间:2024/06/03 17:04
用了这么长时间的hibernate/spring,如果不是今天用的findByExample方法到现在还不知道findByExample的机制。惭愧
Class User{
String username;
String password = "默认口令";
Company company;
getter()&setter().....
}
Company company = companyDao.getById("id");
User user = new User();
user.setSid("主键");
uer.setUsername("user");
use.setCompany(company);
userDao.findByExample(user);
这个时候的SQL条件为:
select * from user
where username = ?
and password = ?
findByExample()使用时得出结论:
1.不支持主键
2.不支持关联
3.不支持NULL
查询示例
org.hibernate.criterion.Example
类允许你通过一个给定实例构建一个条件查询。
此实例的属性值将做成查询条件。
- Cat cat = new Cat();
- cat.setSex('F');
- cat.setColor(Color.BLACK);
- List results = session.createCriteria(Cat.class)
- .add( Example.create(cat))
- .list();
Example.create(cat) 表示根据cat这个对象来构造一个查询条件。
上面这条语句将查询sex 为 F 而且Color为BLACK的所有Cat记录。
版本属性、标识符和关联被忽略。默认情况下值为null的属性将被排除。
可以自行调整Example使之更实用。
- Example example = Example.create(cat)
- .excludeZeroes() //排除值为0的属性
- .excludeProperty("color") //排除 color属性
- .ignoreCase() //忽略大小写
- .enableLike(); //启用模糊查询
- List results = session.createCriteria(Cat.class)
- .add(example)
- .list();
甚至可以使用examples在关联对象上放置条件。
- List results = session.createCriteria(Cat.class)
- .add( Example.create(cat) )
- .createCriteria("mate")
- .add( Example.create( cat.getMate() ) )
- .list();
- hibernate findbyexample findbycriteria
- findByExample和findByCriteria的用法
- Hibernate/Spring的findByExample
- hibernate的findByExample
- hibernate的findByExample
- hibernate的findByExample
- hibernate的findByExample
- hibernate的findByExample
- Spring Hibernate findByCriteria(...)方法 使用
- HIbernate的hibernateTemplate.findByCriteria用法
- findByExample
- findByExample
- 关于使用Hibernate findByExample的注意事项
- Hibernate中的DAO操作的findByExample
- 使用Spring封装的Hibernate findByExample方法
- Hibernate 的 findByExample 用法需要注意的地方
- hibernate中用findByExample方法出现Unknown entity: XXX异常
- Hibernate 中的attachDirty,attachClean,merge,findByProperty和findByExample
- 线程安全总结(二)
- 任意点序列的外围正放着的矩形
- C变长参数
- CSDN专家分和博客系统积分获得规则!
- Xcode的控制台调试命令
- hibernate findbyexample findbycriteria
- 工作随感
- 数据库建表语句(oracle)
- GEL语言运用
- 出现ORA-03115:unsupported network datatype or representation错误的解决方法
- 图说Android碎片化:这玩意到底有多严重?
- OnlineSchemaChange 对FACEBOOK的MYSQL的优化
- Js的parentNodes和ChildNodes
- 关于DirectShow中摄像头的帧率设置