- 浏览: 514406 次
- 性别:
- 来自: 惠州
文章分类
- 全部博客 (255)
- ant (1)
- springMVC (2)
- ajax (4)
- oracle (12)
- SSH (13)
- struts1 (2)
- Hibernate (14)
- spring (5)
- jstl (1)
- 连接池 (1)
- acegi (4)
- java (17)
- jquery (11)
- div+css (4)
- drupal (1)
- php (8)
- freemaker调模板生成静态页面 (1)
- xml (1)
- json (2)
- javascript (9)
- 正则表达式 (4)
- Ext (8)
- jdbc (1)
- sql server (2)
- perl (5)
- db4o (1)
- webservice (4)
- flex (13)
- it资讯 (1)
- joomla (0)
- 设计模式 (1)
- struts2 (4)
- s2sh (8)
- linux (3)
- ejb (2)
- android旅途 (24)
- android (36)
- C/C++ (16)
- mysql (1)
最新评论
-
fengyuxing168:
IBelyService bs = IBelyService. ...
为 Android 添加 Java 层服务也就是添加自定义的aidl服务到serviceManager 通过ServiceManager.getService取 -
dengzhangtao:
"由于ActivityManagerService是 ...
binder理解 -
yzyspy:
ActivityManagerService:startHom ...
Android的Launcher成为系统中第一个启动的,也是唯一的 -
Matchstick:
使用SELECT DISTINCT alias FROM Po ...
hibernate 一对多表查询时fetchMode.join 生成left outer join 出来数据重复问题 -
dlheart:
没看懂你什么意思啊,我遇到的问题是一对多,设了fetch = ...
hibernate 一对多表查询时fetchMode.join 生成left outer join 出来数据重复问题
Hibernate查询功能 hibernate hql使用 hibernate 使用sql 使用Query执行hql,不用find
- 博客分类:
- Hibernate
Hibernate查询功能 hibernate hql使用 hibernate 使用sql 使用Query执行hql,不用find
Hibernate学习笔记19---HQL3(简单属性查询和实体对象查询)
2009/12/01 17:30
------------------------------☆☆☆简单属性查询☆☆☆☆------------------------------------
①,单一属性的查询
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//从Student实体中查询name属性,Student是实体类名,name是Student类中的属性名
//返回结果集属性列表,元素类型和实体类中相应的属性类型一致(这里name是String类型,那么List中元素的类型就是String)
List names = session.createQuery("select name from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
System.out.println("StudentName : " + iter.next());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,多个属性查询
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//笔记HQL1中自定义的带参数的构造方法这里就用上了,
//这样可以查询实体中的对象
List names = session.createQuery("select new Student(id, name) from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentId : " + student.getId() + ", StudentName : " + student.getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
//也可以这么查,查询指定的多个属性
List names = session.createQuery("select id, name from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
//一条结果集可以看作是一个对象数组,因为数组具体是什么类型难以确定
Object[] obj = (Object[])iter.next();
//对象数组按你指定的顺序(id,name)排列,那么0就是id,1就是name
System.out.println("StudentId : " + obj[0] + ", StudentName : " + obj[1]);
}
------------------------------☆☆☆实体对象查询☆☆☆☆------------------------------------
①,from Student
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//返回Studen对象集合,(可以省略select)
List lists = session.createQuery("from Student").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,from Student s
//返回Studen对象集合,(可以省略select)
//对于实体对象名,可以使用别名
List lists = session.createQuery("from Student s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
③,from Student as s
//使用别名的时候可以用as,也可以不用像②那样
List lists = session.createQuery("from Student as s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
④,select s from Student as s
//可以使用select,但使用select时,必须使用别名在先,因为select后要跟别名
//而且,不支持select * from 这样的查询
List lists = session.createQuery("select s from Student as s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
------------------------------☆☆☆实体对象查询( 有关list()和iterator() )☆☆☆☆------------------------------------
①,Iterator
//除了list()外iterator()也可以保存查询结果,
//不过iterator()会发生1+N问题,1是查询出主键,N即根据主键查询出每一行结果
//这样每一行结果都会发出一条SQL语句,严重影响性能
Iterator iter = session.createQuery("from Student").iterate();
while (iter.hasNext()) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
②,iterator的二次查询会使用1级缓存
//第一次查询用list(),list()会把结果集放入1级缓存
List lists = session.createQuery("from Student").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
System.out.println("---------------------------------------");
//第二次查询使用iterator(),iterator()会直接到1级缓存中寻找数据
//这样就不会发出1+N个SQL语句了,只发出一个查询主键的即可
Iterator iter2 = session.createQuery("from Student").iterate();
while (iter2.hasNext()) {
Student student = (Student)iter2.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
③,list()默认不会到1级缓存中查找数据
//list()会把查询到的结果放到1级缓存中
List lists = session.createQuery("from Student").list();
....省略
//但是list()默认情况下,不会去1级缓存查找数据
List lists2 = session.createQuery("from Student").list();
....省略
查看文章
Hibernate学习笔记19---HQL4(条件查询)
Hibernate查询where条件过滤
用Query执行hql语句,find已经不用了
2009/12/03 10:05
在HQL中也是可以使用where条件语句的,下面看看例子吧:
(实体类和实体类配置文件都沿用HQL1中的文件)
①,普通的where语句(参数含在HQL语句中)
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//where条件句也是可以用的
List lists = session.createQuery("select id, name from Student where name like '%1%'").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID : " + obj[0]);
System.out.println("StudentName : " + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,可以在HQL语句之外指定参数 query.setParameter(数字,参数)
//?用作占位符
//参数的索引是从0开始
Query query = session.createQuery("select id, name from Student where name like ?");
query.setParameter(0, "%1%");
List lists = query.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID2 : " + obj[0]);
System.out.println("StudentName2 : " + obj[1]);
}
②-1,上面的例子还可以写成下面这样
//因为createQuery方法返回的也是Query对象,所以可以直接.出Query的setParameter方法
//setParameter也返回Query对象,所以也可以.出Query的list()方法
List lists = session.createQuery("select id, name from Student where name like ?")
.setParameter(0, "%1%")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID2 : " + obj[0]);
System.out.println("StudentName2 : " + obj[1]);
}
③,还可以这样指定query.setParameter(名称,参数)
//像这样就不用占位符了,直接用参数名代替,:参数名
List lists = session.createQuery("select s.id, s.name from Student s where name like :myname")
.setParameter("myname", "%1%")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID3 : " + obj[0]);
System.out.println("StudentName3 : " + obj[1]);
}
④,where ... and
public void testquery4(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//将日期格式化为yyyy-MM的格式,方便后面使用
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//where后可以接and或or语句,这里是and
//找出1班的,创建日期在2009年3月之前的学生
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where s.name like :name and s.createDate < :createDate")
.setParameter("name", "%クラス1の%")
//format.parse方法是将String转换为Date的唯一方法,我知道的^_^
.setParameter("createDate", format.parse("2009-03"))
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID4 : " + obj[0]);
System.out.println("StudentName4 : " + obj[1]);
System.out.println("StudentCreateDate4 : " + obj[2]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
⑤,where ... in
//setParameterList,里面可以放集合或对象数组,这样就可以指定一组参数了
//查询学号为1,2,3,4,5的五名学生的信息
List lists = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
.setParameterList("myids", new Object[]{1,2,3,4,5})
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5 : " + obj[0]);
System.out.println("StudentName5 : " + obj[1]);
}
⑤-2,上面的例子还可以写成这样
//使用多个?号占位符,查询学号为10,22,35,48,54 五名学生的信息
List lists = session.createQuery("select s.id, s.name from Student s where s.id in(?,?,?,?,?)")
.setParameter(0, 10)
.setParameter(1, 22)
.setParameter(2, 35)
.setParameter(3, 48)
.setParameter(4, 54)
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5_2 : " + obj[0]);
System.out.println("StudentName5_2 : " + obj[1]);
}
⑥,可以使用数据库的内置函数 (这里使用的是date_format)
//查找2009-02入学的学生
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where date_format(s.createDate,'%Y-%m') = ?")
.setParameter(0, "2009-02")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID6 : " + obj[0]);
System.out.println("StudentName6 : " + obj[1]);
System.out.println("StudentCreateDate6 : " + obj[2]);
}
⑦,可以使用between and
//支持between and
//需要注意的是setParameter(数字,对象),对象这块是任何类型都可以放的,这里是时间,不过先得从字符串转换才行
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where s.createDate between ? and ?")
//查找从2009-03到2009-05入学的学生
.setParameter(0, format.parse("2009-03-01 00:00:00"))
.setParameter(1, format.parse("2009-05-31 23:59:59"))
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5 : " + obj[0]);
System.out.println("StudentName5 : " + obj[1]);
System.out.println("StudentCreateDate5 : " + obj[2]);
Hibernate学习笔记19---HQL5(原生SQL,外置命名查询,查询过滤器,分页查询,对象导航查询)
2009/12/04 10:53
①,原生SQL查询 用createSQLQuery,后面就可以跟标准的SQL语句了
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//createSQLQuery支持原生标准SQL查询,Hibernate3.0后引入
List lists = (List)session.createSQLQuery("select * from t_student").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID : " + obj[0]);
System.out.println("StudentName : " + obj[1]);
System.out.println("StudentCreateDate : " + obj[2]);
System.out.println("StudentClassid : " + obj[3]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,外部命名查询,即把HQL语句写在配置文件中,用到的时候再调用即可
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ahuzl.hibernate.Student" table="t_student">
<id name="id" column="student_id">
<generator class="native"/>
</id>
<property name="name" column="student_name"/>
<property name="createDate"/>
<many-to-one name="classes" column="classid"/>
</class>
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
</hibernate-mapping>
其实关键就是后面Query那句话,这段语句放在任何一个配置文件中都行,(比如放在Class.hbm.xml中,这里因为是查询Student信息,从逻辑上考虑就放在了Student.hbm.xml中)
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
③,查询过滤器,其实就是在每一句HQL后都加上一个提前设定的条件(比如只让查找某个用户的东西啦)
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ahuzl.hibernate.Student" table="t_student">
<id name="id" column="student_id">
<generator class="native"/>
</id>
<property name="name" column="student_name"/>
<property name="createDate"/>
<many-to-one name="classes" column="classid"/>
<!-- 定义好了查询过滤器之后,就可以拿来用了。条件里面有大于号小于号的,要用转义字符代替(因为XML会把<>识别为自己的开始结束符号) -->
<filter name="filterTest" condition="student_id < :myid"/>
</class>
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
<!-- 先定义一个查询过滤器 -->
<filter-def name="filterTest">
<filter-param name="myid" type="integer"/>
</filter-def>
</hibernate-mapping>
查询
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//先启用过滤器,然后设置参数
session.enableFilter("filterTest").setParameter("myid", 10);
List lists = (List)session.createQuery("from Student").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " +student.getName());
System.out.println("StudentCreateDate : " + student.getCreateDate());
System.out.println("StudentClassid : " + student.getClasses().getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
④,分页查询
//从第0条开始,到第5条结束
List lists = (List)session.createQuery("from Student")
.setFirstResult(0)
.setMaxResults(5)
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " +student.getName());
}
⑤,对象导航查询
仔细看看HQL中的这句: s.classes.name 这真是把对象和SQL揉到一起了啊
List lists = (List)session.createQuery("select s.name from Student s where s.classes.name like '%1%'").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
String name = (String)iter.next();
System.out.println("StudentName : " + name);
}
类别:Hibernate | | 添加到搜藏 | 分享到i贴吧 | 浏览(413) | 评论 (0)
上一篇:Hibernate学习笔记19---HQL4(条... 下一篇:Hibernate学习笔记19---HQL6(内...
Hibernate学习笔记19---HQL6(内连接,左连接,外连接)
2009/12/08 17:21
内连接:把两面共有的部分查出来
左连接:把左边的部分全查出来
右连接:把右边的部分全查出来
/**
* inner join
*/
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//可以省略inner,因为只要写了join,默认就是inner内连接
//List lists = (List)session.createQuery("select c.name, s.name from Student s join s.classes c").list();
//写上的话,就是这个样子啦
List lists = (List)session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* left join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有班级的学生也查出来,注意left join 后接的表有点特殊,是c.student即班级中的student属性,hibernate会依照这个属性帮我们找到相应的表
List lists = (List)session.createQuery("select c.name, s.name from Student s left join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* right join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有学生的班级也查出来,注意right join 后接的也比较特殊,理由同上
List lists = (List)session.createQuery("select c.name, s.name from Student s right join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
Hibernate学习笔记19---HQL6(内连接,左连接,外连接)
2009/12/08 17:21
内连接:把两面共有的部分查出来
左连接:把左边的部分全查出来
右连接:把右边的部分全查出来
/**
* inner join
*/
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//可以省略inner,因为只要写了join,默认就是inner内连接
//List lists = (List)session.createQuery("select c.name, s.name from Student s join s.classes c").list();
//写上的话,就是这个样子啦
List lists = (List)session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* left join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有班级的学生也查出来,注意left join 后接的表有点特殊,是c.student即班级中的student属性,hibernate会依照这个属性帮我们找到相应的表
List lists = (List)session.createQuery("select c.name, s.name from Student s left join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* right join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有学生的班级也查出来,注意right join 后接的也比较特殊,理由同上
List lists = (List)session.createQuery("select c.name, s.name from Student s right join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
Hibernate学习笔记19---HQL3(简单属性查询和实体对象查询)
2009/12/01 17:30
------------------------------☆☆☆简单属性查询☆☆☆☆------------------------------------
①,单一属性的查询
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//从Student实体中查询name属性,Student是实体类名,name是Student类中的属性名
//返回结果集属性列表,元素类型和实体类中相应的属性类型一致(这里name是String类型,那么List中元素的类型就是String)
List names = session.createQuery("select name from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
System.out.println("StudentName : " + iter.next());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,多个属性查询
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//笔记HQL1中自定义的带参数的构造方法这里就用上了,
//这样可以查询实体中的对象
List names = session.createQuery("select new Student(id, name) from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentId : " + student.getId() + ", StudentName : " + student.getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
//也可以这么查,查询指定的多个属性
List names = session.createQuery("select id, name from Student").list();
for (Iterator iter = names.iterator();iter.hasNext();) {
//一条结果集可以看作是一个对象数组,因为数组具体是什么类型难以确定
Object[] obj = (Object[])iter.next();
//对象数组按你指定的顺序(id,name)排列,那么0就是id,1就是name
System.out.println("StudentId : " + obj[0] + ", StudentName : " + obj[1]);
}
------------------------------☆☆☆实体对象查询☆☆☆☆------------------------------------
①,from Student
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//返回Studen对象集合,(可以省略select)
List lists = session.createQuery("from Student").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,from Student s
//返回Studen对象集合,(可以省略select)
//对于实体对象名,可以使用别名
List lists = session.createQuery("from Student s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
③,from Student as s
//使用别名的时候可以用as,也可以不用像②那样
List lists = session.createQuery("from Student as s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
④,select s from Student as s
//可以使用select,但使用select时,必须使用别名在先,因为select后要跟别名
//而且,不支持select * from 这样的查询
List lists = session.createQuery("select s from Student as s").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID2 : " + student.getId());
System.out.println("StudentName2 : " + student.getName());
}
//上下的代码就省略了,和①里的一样
------------------------------☆☆☆实体对象查询( 有关list()和iterator() )☆☆☆☆------------------------------------
①,Iterator
//除了list()外iterator()也可以保存查询结果,
//不过iterator()会发生1+N问题,1是查询出主键,N即根据主键查询出每一行结果
//这样每一行结果都会发出一条SQL语句,严重影响性能
Iterator iter = session.createQuery("from Student").iterate();
while (iter.hasNext()) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
②,iterator的二次查询会使用1级缓存
//第一次查询用list(),list()会把结果集放入1级缓存
List lists = session.createQuery("from Student").list();
System.out.println(lists.size());
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
System.out.println("---------------------------------------");
//第二次查询使用iterator(),iterator()会直接到1级缓存中寻找数据
//这样就不会发出1+N个SQL语句了,只发出一个查询主键的即可
Iterator iter2 = session.createQuery("from Student").iterate();
while (iter2.hasNext()) {
Student student = (Student)iter2.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " + student.getName());
}
③,list()默认不会到1级缓存中查找数据
//list()会把查询到的结果放到1级缓存中
List lists = session.createQuery("from Student").list();
....省略
//但是list()默认情况下,不会去1级缓存查找数据
List lists2 = session.createQuery("from Student").list();
....省略
查看文章
Hibernate学习笔记19---HQL4(条件查询)
Hibernate查询where条件过滤
用Query执行hql语句,find已经不用了
2009/12/03 10:05
在HQL中也是可以使用where条件语句的,下面看看例子吧:
(实体类和实体类配置文件都沿用HQL1中的文件)
①,普通的where语句(参数含在HQL语句中)
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//where条件句也是可以用的
List lists = session.createQuery("select id, name from Student where name like '%1%'").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID : " + obj[0]);
System.out.println("StudentName : " + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,可以在HQL语句之外指定参数 query.setParameter(数字,参数)
//?用作占位符
//参数的索引是从0开始
Query query = session.createQuery("select id, name from Student where name like ?");
query.setParameter(0, "%1%");
List lists = query.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID2 : " + obj[0]);
System.out.println("StudentName2 : " + obj[1]);
}
②-1,上面的例子还可以写成下面这样
//因为createQuery方法返回的也是Query对象,所以可以直接.出Query的setParameter方法
//setParameter也返回Query对象,所以也可以.出Query的list()方法
List lists = session.createQuery("select id, name from Student where name like ?")
.setParameter(0, "%1%")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID2 : " + obj[0]);
System.out.println("StudentName2 : " + obj[1]);
}
③,还可以这样指定query.setParameter(名称,参数)
//像这样就不用占位符了,直接用参数名代替,:参数名
List lists = session.createQuery("select s.id, s.name from Student s where name like :myname")
.setParameter("myname", "%1%")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID3 : " + obj[0]);
System.out.println("StudentName3 : " + obj[1]);
}
④,where ... and
public void testquery4(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//将日期格式化为yyyy-MM的格式,方便后面使用
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//where后可以接and或or语句,这里是and
//找出1班的,创建日期在2009年3月之前的学生
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where s.name like :name and s.createDate < :createDate")
.setParameter("name", "%クラス1の%")
//format.parse方法是将String转换为Date的唯一方法,我知道的^_^
.setParameter("createDate", format.parse("2009-03"))
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID4 : " + obj[0]);
System.out.println("StudentName4 : " + obj[1]);
System.out.println("StudentCreateDate4 : " + obj[2]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
⑤,where ... in
//setParameterList,里面可以放集合或对象数组,这样就可以指定一组参数了
//查询学号为1,2,3,4,5的五名学生的信息
List lists = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
.setParameterList("myids", new Object[]{1,2,3,4,5})
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5 : " + obj[0]);
System.out.println("StudentName5 : " + obj[1]);
}
⑤-2,上面的例子还可以写成这样
//使用多个?号占位符,查询学号为10,22,35,48,54 五名学生的信息
List lists = session.createQuery("select s.id, s.name from Student s where s.id in(?,?,?,?,?)")
.setParameter(0, 10)
.setParameter(1, 22)
.setParameter(2, 35)
.setParameter(3, 48)
.setParameter(4, 54)
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5_2 : " + obj[0]);
System.out.println("StudentName5_2 : " + obj[1]);
}
⑥,可以使用数据库的内置函数 (这里使用的是date_format)
//查找2009-02入学的学生
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where date_format(s.createDate,'%Y-%m') = ?")
.setParameter(0, "2009-02")
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID6 : " + obj[0]);
System.out.println("StudentName6 : " + obj[1]);
System.out.println("StudentCreateDate6 : " + obj[2]);
}
⑦,可以使用between and
//支持between and
//需要注意的是setParameter(数字,对象),对象这块是任何类型都可以放的,这里是时间,不过先得从字符串转换才行
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List lists = session.createQuery("select s.id, s.name, s.createDate from Student s where s.createDate between ? and ?")
//查找从2009-03到2009-05入学的学生
.setParameter(0, format.parse("2009-03-01 00:00:00"))
.setParameter(1, format.parse("2009-05-31 23:59:59"))
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID5 : " + obj[0]);
System.out.println("StudentName5 : " + obj[1]);
System.out.println("StudentCreateDate5 : " + obj[2]);
Hibernate学习笔记19---HQL5(原生SQL,外置命名查询,查询过滤器,分页查询,对象导航查询)
2009/12/04 10:53
①,原生SQL查询 用createSQLQuery,后面就可以跟标准的SQL语句了
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//createSQLQuery支持原生标准SQL查询,Hibernate3.0后引入
List lists = (List)session.createSQLQuery("select * from t_student").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println("StudentID : " + obj[0]);
System.out.println("StudentName : " + obj[1]);
System.out.println("StudentCreateDate : " + obj[2]);
System.out.println("StudentClassid : " + obj[3]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
②,外部命名查询,即把HQL语句写在配置文件中,用到的时候再调用即可
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ahuzl.hibernate.Student" table="t_student">
<id name="id" column="student_id">
<generator class="native"/>
</id>
<property name="name" column="student_name"/>
<property name="createDate"/>
<many-to-one name="classes" column="classid"/>
</class>
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
</hibernate-mapping>
其实关键就是后面Query那句话,这段语句放在任何一个配置文件中都行,(比如放在Class.hbm.xml中,这里因为是查询Student信息,从逻辑上考虑就放在了Student.hbm.xml中)
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
③,查询过滤器,其实就是在每一句HQL后都加上一个提前设定的条件(比如只让查找某个用户的东西啦)
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ahuzl.hibernate.Student" table="t_student">
<id name="id" column="student_id">
<generator class="native"/>
</id>
<property name="name" column="student_name"/>
<property name="createDate"/>
<many-to-one name="classes" column="classid"/>
<!-- 定义好了查询过滤器之后,就可以拿来用了。条件里面有大于号小于号的,要用转义字符代替(因为XML会把<>识别为自己的开始结束符号) -->
<filter name="filterTest" condition="student_id < :myid"/>
</class>
<query name="searchStudent">
<![CDATA[
select s from Student s where s.id < ?
]]>
</query>
<!-- 先定义一个查询过滤器 -->
<filter-def name="filterTest">
<filter-param name="myid" type="integer"/>
</filter-def>
</hibernate-mapping>
查询
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//先启用过滤器,然后设置参数
session.enableFilter("filterTest").setParameter("myid", 10);
List lists = (List)session.createQuery("from Student").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " +student.getName());
System.out.println("StudentCreateDate : " + student.getCreateDate());
System.out.println("StudentClassid : " + student.getClasses().getName());
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
④,分页查询
//从第0条开始,到第5条结束
List lists = (List)session.createQuery("from Student")
.setFirstResult(0)
.setMaxResults(5)
.list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println("StudentID : " + student.getId());
System.out.println("StudentName : " +student.getName());
}
⑤,对象导航查询
仔细看看HQL中的这句: s.classes.name 这真是把对象和SQL揉到一起了啊
List lists = (List)session.createQuery("select s.name from Student s where s.classes.name like '%1%'").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
String name = (String)iter.next();
System.out.println("StudentName : " + name);
}
类别:Hibernate | | 添加到搜藏 | 分享到i贴吧 | 浏览(413) | 评论 (0)
上一篇:Hibernate学习笔记19---HQL4(条... 下一篇:Hibernate学习笔记19---HQL6(内...
Hibernate学习笔记19---HQL6(内连接,左连接,外连接)
2009/12/08 17:21
内连接:把两面共有的部分查出来
左连接:把左边的部分全查出来
右连接:把右边的部分全查出来
/**
* inner join
*/
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//可以省略inner,因为只要写了join,默认就是inner内连接
//List lists = (List)session.createQuery("select c.name, s.name from Student s join s.classes c").list();
//写上的话,就是这个样子啦
List lists = (List)session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* left join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有班级的学生也查出来,注意left join 后接的表有点特殊,是c.student即班级中的student属性,hibernate会依照这个属性帮我们找到相应的表
List lists = (List)session.createQuery("select c.name, s.name from Student s left join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* right join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有学生的班级也查出来,注意right join 后接的也比较特殊,理由同上
List lists = (List)session.createQuery("select c.name, s.name from Student s right join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
Hibernate学习笔记19---HQL6(内连接,左连接,外连接)
2009/12/08 17:21
内连接:把两面共有的部分查出来
左连接:把左边的部分全查出来
右连接:把右边的部分全查出来
/**
* inner join
*/
public void testquery1(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//可以省略inner,因为只要写了join,默认就是inner内连接
//List lists = (List)session.createQuery("select c.name, s.name from Student s join s.classes c").list();
//写上的话,就是这个样子啦
List lists = (List)session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* left join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有班级的学生也查出来,注意left join 后接的表有点特殊,是c.student即班级中的student属性,hibernate会依照这个属性帮我们找到相应的表
List lists = (List)session.createQuery("select c.name, s.name from Student s left join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
/**
* right join
*/
public void testquery2(){
Session session = null;
try{
session = HibernateUtils2.singleInstance.getSession();
session.beginTransaction();
//把没有学生的班级也查出来,注意right join 后接的也比较特殊,理由同上
List lists = (List)session.createQuery("select c.name, s.name from Student s right join c.student s").list();
for (Iterator iter = lists.iterator();iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
HibernateUtils2.singleInstance.closeSession(session);
}
}
发表评论
-
hibernate 一对多表查询时fetchMode.join 生成left outer join 出来数据重复问题
2010-09-06 20:58 5407hibernate 一对多表查询时fetchMode.join ... -
hibernate中执行标准原始sql需要注意的问题
2010-08-26 08:41 1725hibernate中执行标准sql需要注意的问题 关键字: ... -
个人心得Hibernate对视图的映射
2010-08-25 00:35 3485个人心得Hibernate对视图的映射 java技术 201 ... -
[org.hibernate.util.JDBCExceptionReporter] - 列名 'a_b_c' 无效。
2010-06-01 14:29 30572010-06-01 14:19:34,041 [main] ... -
我的Hibernate学习笔记 Hibernate理解 hibernate知识要点
2010-05-18 13:34 1666Hibernate---我的Hibernate学习笔记 ... -
Hibernate集合过滤、子查询
2010-05-11 16:31 1519Hibernate集合过滤、子查 ... -
Hibernate中多对多关系的常见问题
2010-03-26 09:44 8831、到底在哪用cascade="..."? ... -
Hibernate通常是三种查询小结
2009-03-24 16:33 1697通常使用的Hibernate通常是三种:hql查询,QBC查询 ... -
Hibernate查询语言
2009-03-24 16:31 957第 15 章 HQL: Hibernate查询 ... -
Hibernate的缓存机制
2008-11-25 21:10 934Hibernate的缓存机制 1. Cache简介: ... -
Criteria進行查詢
2008-09-09 16:31 1504使用Criteria進行查詢時,不僅僅能組合出SQL中wher ... -
hibernate多表查询
2008-08-26 18:37 2902hibernate多表查询 关键字: hibernate 我 ... -
Hibernate中Criteria的完整用法
2008-07-21 14:26 2201Hibernate中Criteria的完整 ...
相关推荐
在数据查询方面,Hibernate提供了多种查询方式,其中HQL(Hibernate Query Language)是一种基于SQL的领域查询语言,它允许开发者以面向对象的方式编写查询语句,而无需直接处理复杂的SQL语句。本文将深入探讨...
本教程重点聚焦于Hibernate的数据检索机制,特别是Hibernate查询语言(HQL),它是Hibernate提供的一个强大的SQL方言,用于在ORM层面上执行数据库查询。 **一、Hibernate概述** Hibernate作为Java中的ORM框架,它...
Hibernate HQL,全称为Hibernate Query Language,是Hibernate框架中用于操作对象关系映射(ORM)的查询语言。它类似于SQL,但设计目的是针对Java对象,而不是数据库表。HQL允许开发者以面向对象的方式编写查询,...
Hibernate 的核心功能之一是提供了一套强大的查询接口和方法,包括基于对象的查询、SQL 查询和 HQL (Hibernate Query Language) 查询等。本文将详细介绍 Hibernate 提供的几种查询方式及其应用场景。 #### 二、重要...
如果在没有启用查询缓存时,使用`load()`方法获取的单个持久化对象会被缓存,但若想缓存`findAll()`, `list()`, `Iterator()`, `createQuery()`, `createCriteria()`等方法获取的结果集,必须设置`hibernate.cache....
主要通过`Session`接口的`find()`方法来执行查询。例如: ```java Session session = sessionFactory.openSession(); List<User> users = session.find("from User"); ``` 上述代码中的`find("from User")`就是...
此外,它还提供了查询语言HQL(Hibernate Query Language)和Criteria API,用于执行复杂的数据库查询。 3. **主要组件**: - **SessionFactory**:它是线程安全的,负责创建Session实例,是整个Hibernate的核心。...
在本文中,我们将深入探讨Hibernate框架中的数据查询,特别是使用Hibernate Query Language (HQL)。Hibernate是一个流行的Java持久化框架,它允许开发者通过面向对象的方式来操作数据库,极大地简化了数据库交互的...
- `List find(String queryString)`:通过HQL(Hibernate Query Language)查询字符串来获取实体集合。 - `List findByNamedQuery(String queryName)`:根据命名查询返回实体集合。 - `Object get(Class entity...
- **HQL/SQL查询**:Hibernate支持HQL(Hibernate Query Language)和原生SQL进行数据查询,提供了更加灵活的查询方式。 2. **源码解析** 在`HibernateDao.java`中,我们能看到如下的方法: - `save(Object ...
在 Hibernate 中,主要使用 HQL(Hibernate Query Language)进行查询,这是一种面向对象的查询语言,类似于 SQL。以下是一些关于 HQL 查询的关键点: - **大小写敏感**:HQL 中的对象名和属性名是大小写敏感的,与...
- **查询工具**: 提供方便的查询API,支持HQL(Hibernate Query Language)和Criteria查询。 **2. S2Hibernate与Seasar框架的整合** Seasar框架的组件化设计使得S2Hibernate能与其他组件无缝集成。例如: - **S2...
此外,Hibernate提供了强大的 Criteria API 和 HQL(Hibernate Query Language),使得查询更加灵活。 **配置和使用步骤:** 1. 编写Hibernate配置文件(通常为`hibernate.cfg.xml`),包含数据库连接信息、方言等...
7. **HQL(Hibernate Query Language)**:是Hibernate的专用查询语言,类似于SQL,但更加面向对象。在实例中,你将学习如何使用HQL进行复杂的数据查询。 8. **事务管理(Transaction)**:Hibernate通过...
HQL(Hibernate Query Language)是Hibernate框架中用于查询对象关系映射(ORM)的数据的语言,类似于SQL,但它是面向对象的。以下是对标题和描述中所提到的HQL语句的详细说明: 1. 实体查询: HQL的基本查询形式...
`getHibernateTemplate().find(hql)`执行查询并返回结果列表。 4. **HQL多表查询**: 多表查询在HQL中同样直观。第22行的查询演示了如何从`PhCorrelativeUnit`和`PhCorrelativeUnitFunction`两个表中选取数据,并...
- Query:提供查询数据库的功能,包括HQL(Hibernate Query Language)和Criteria API。 3. **配置Hibernate** - 配置文件(hibernate.cfg.xml):定义数据库连接参数,如URL、用户名、密码、方言等。 - 实体类...
Query接口则用于执行HQL(Hibernate Query Language)和SQL查询。 3. **配置Hibernate** 在使用Hibernate之前,需要进行相应的配置,包括数据库连接信息、实体类映射文件(hbm.xml)以及Hibernate配置文件...