06_传智播客hibernate教程_完善HibernateUtil类及hql查询入门 &&07_传智播客hibernate教程_实体类或属性名与数据库关键字冲突问题

HQL and native SQL queries are represented with an instance of org.hibernate.Query.

This interface offers methods for

  1 parameter binding,

2 result set handling,

3 the execution of the actual query.

 

接口Query 提供的3项功能

 

补充 

  HQl 语句操作的是对象,不是数据库的表,并且支持多态 from 类名 
  如果 from Object 则查询数据库所有的表

 

You always obtain a Query using the current Session:

 

List cats = session.createQuery(

"from Cat as cat where cat.birthdate < ?").setDate(0, date).list();

 

List mothers = session.createQuery(

"select mother from Cat as cat join cat.mother as mother where cat.name = ?")

.setString(0, name).list();

 

参数绑定除了基本类型还可以是 自定义对象

List kittens = session.createQuery(

"from Cat as cat where cat.mother = ?").setEntity(0, pk).list();

 

Cat mother = (Cat) session.createQuery(

"select cat.mother from Cat as cat where cat = ?").setEntity(0, izi).uniqueResult();]]

(如果检索出的记录是多条的话,报错)

 

Query mothersWithKittens = (Cat) session.createQuery(

"select mother from Cat as mother left join fetch mother.kittens");

Set uniqueMothers = new HashSet(mothersWithKittens.list());

 

A query is usually executed by invoking list(). The result of the query will be loaded completely into a collection in memory. Entity instances retrieved by a query are in a persistent state. The

uniqueResult() method offers a shortcut if you know your query will only return a single object.

Queries that make use of eager fetching of collections usually return duplicates of the root objects, but with their collections initialized. You can filter these duplicates through a Set.

 

0 7_传智播客hibernate教程_实体类或属性名与数据库关键字冲突问题

 

解决:

  1 修改数据库表名和字段名

  2 在配置文件 用 反引号 ··  table 和 column 的值引起来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值