hibernate查询技巧

Create DataBase SHOPPING;

go

use SHOPPING;

go

/*==============================================================*/

/* Table: CATEGORIES */

/*==============================================================*/

create table CATEGORIES (

CATEGORY_ID bigint identity,

CATEGORY_NAME varchar(100) not null,

CATEGORY_DESCN varchar(500) null,

constraint PK_CATEGORIES primary key (CATEGORY_ID))

go

/*==============================================================*/

/* Table: PRODUCTS */

/*==============================================================*/

create table PRODUCTS (

PRODUCT_NO varchar(10) not null,

CATEGORY_ID bigint not null,

PRODUCT_NAME varchar(300) not null,

PRODUCT_PRICE float not null,

PHOTO_PATH varchar(100) null,

PRODUCT_DESCN varchar(2000) null,

constraint PK_PRODUCTS primary key (PRODUCT_NO))

go

/*==============================================================*/

/* Table: PRODUCT_SUPPLY */

/*==============================================================*/

create table PRODUCT_SUPPLY (

SUPPLY_NO varchar(10) null,

PRODUCT_NO varchar(10) null)

go

/*==============================================================*/

/* Table: SUPPLIERS */

/*==============================================================*/

create table SUPPLIERS (

SUPPLY_NO varchar(10) not null,

SUPPLY_NAME varchar(200) not null,

SUPPLY_DESCN varchar(400) null,

constraint PK_SUPPLIERS primary key (SUPPLY_NO))

go

/*==============================================================*/

/* Create Relation */

/*==============================================================*/

alter table PRODUCTS

add constraint FK_PRODUCTS_REFERENCE_CATEGORI foreign key (CATEGORY_ID)

references CATEGORIES (CATEGORY_ID)

go

alter table PRODUCT_SUPPLY

add constraint FK_PRODUCT__REFERENCE_PRODUCTS foreign key (PRODUCT_NO)

references PRODUCTS (PRODUCT_NO)

go

alter table PRODUCT_SUPPLY add constraint FK_PRODUCT__REFERENCE_SUPPLIER foreign key (SUP

PLY_NO) references SUPPLIERS (SUPPLY_NO)

go

?

?

?

创建数据库脚本的

PRODUCTS(产品表) 和 CATEGORIES(类别表)一对多????? PRODUCT_SUPPLY? 为中间表 SUPPLIERS(供货商表)??? 和 PRODUCTS 为多对多的关系。

products 表 hbm.xml ????????????????

view plaincopy to clipboardprint?

  1. ????????????????????<many-to-one?name="category"?class="category"?cascade="save-update">??
  2. ??
  3. ????<column?name="category_id"?>??
  4. ??
  5. </many-to-one>??
  6. ??
  7. ??
  8. ??
  9. <set?name="supplys"?table="product_supply"?cascade="save-update">??
  10. ??
  11. ???????????????????????????????
  12. ??
  13. ????<key?column="product_no"></key>??
  14. ??
  15. ???????????????????????????????
  16. ??
  17. ????<many-to-many?class="supply"?column="supply_no"></many-to-many>??
  18. ??
  19. ??

<many-to-one name="category" class="Category" cascade="save-update"> <column name="CATEGORY_ID"> </many-to-one> <key column="PRODUCT_NO"></key> <many-to-many class="Supply" column="SUPPLY_NO"></many-to-many>

category? 表 hbm.xml?

?

view plaincopy to clipboardprint?

  1. <set?name="productes"?table="productes"?cascade="save-update"?inverse="true">??
  2. ??
  3. ??????????????????????????????????
  4. ??
  5. ????????????????<key?column="category_id"></key>??
  6. ??
  7. ????????????????<one-to-many?class="product">??
  8. ??
  9. ??

<key column="CATEGORY_ID"></key> <one-to-many class="Product">

view plaincopy to clipboardprint?

  1. supply??表?hbm.xml??

supply? 表 hbm.xml

view plaincopy to clipboardprint?

  1. <pre?class=xml?name="code"><set?name="products"?table="product_supply"?inverse="true"?cascade="save-update">??
  2. ??
  3. ????????????????<key?column="supply_no"></key>??
  4. ??
  5. ????????????????<many-to-many?class="product"?column="product_no"></many-to-many>??
  6. ??
  7. ??

view plaincopy to clipboardprint?

  1. <set?name="products"?table="product_supply"?inverse="true"?cascade="save-update">??
  2. ??
  3. ????????????????<key?column="supply_no"></key>??
  4. ??
  5. ????????????????<many-to-many?class="product"?column="product_no"></many-to-many>??
  6. ??
  7. ??

<key column="SUPPLY_NO"></key> <many-to-many class="Product" column="PRODUCT_NO"></many-to-many>

1,?添加一个的新商品名称为”Compaq 2620” 该商品属于“笔记本”类别 由当前所有的提供商提供货源

??????????????????????? List list?? = session.createQuery("from Supply").list(); ???Category c = (Category) session.get(Category.class, new Long(1)); ??? ???product.setCategory(c); ???product.setSupplys(new HashSet(list)); ??? ???session.save(product);

2,?查询编号为” S0001”的提供商提供的所有商品 ??????????????????????????????????????????????????????? //通过隐式内连接导航 List list?? = session.createQuery("from Product p where p.supplys.supply_no='S0001'").list(); 隐式内连接导航 要注意的一个问题是 从many端到 one 端 可以无限导航 但从one到many端只能导航一级

3,查询编号为”S0002”的提供商提供的所有商品所涉及的类别 session.createQuery("from Category c? where c.productes.product_no in (select p.product_no from Product p where p.supplys.supply_no='S0002' ) ").list(); 用到子查询

4,查询名称为”TCL SHE8533”的商品的每个提供商的编号、名称(部分属性查询) session.createQuery("select s.supply_no,s.supply_name from Supply s where s.products.product_name='TCL SHE8533'").list(); //投影查询。如果想将查询出来的 结果封装成对象 用 select new? package.Temp(s.a,s.b...) from ....?? Temp提供相应的构造方法包含可选的字段注意带包名。

5,?查询多于3种商品的类别信息(使用size函数处理) session.createQuery("from Category s where s.productes.size>3").list(); 注意其中的 size 表示的是 商品类别中产品数量多于3的类别。size用来处理集合中的大小

6,查询至少有一个商品的类别信息(使用exists处理) ?session.createQuery("from Category c where exists( from c.productes) ").list();

7,查询可以提供某种商品的供应商信息(使用elements处理) session.createQuery("from Supply s where :product in elements(s.products) ")..setParameter("product", product).list(); product为 一个 对象 。 product in elements(s.products) 表示这个对象是否在这个集合中

8,使用本地SQL,显示所有商品的商品名、价格以及类别信息,并降序排列。 session.createSQLQuery("select p.PRODUCT_NAME,p.PRODUCT_PRICE ,c.*? from PRODUCTS p ,CATEGORIES c where p.CATEGORY_ID = c.CATEGORY_ID order by p.PRODUCT_PRICE desc") 的到的 集合中是个 Object[];? 如果想返回对象 可以用 命名sql并在配置文件中指定返回的对象类型。

9 分页查询 :将商品按价格升序排列后,取第三页的记录,(每页显示2条记录) ?Query query = session.createQuery("from Product p order by? p.product_price? ") ???????????????????? .setFirstResult(2*(3-1)) ???????????????????? .setMaxResults(2);

?

10,查询所有类别的名字,及该类别包含的商品数量 (使用group by ,count函数) session.createQuery("select max(c.category_name), count(p)? from Category c inner join c.productes p? group by c.category_id?? ") 还有一种简单的方式就是? "select c.category_name, c.products.size?? from Category c "

11,批处理: 将某个类别下的商品修改为现有的另一个类别。 int count = session.createQuery("update Product p? set p.category=:category where p.category.category_id='1'") ??????????????????????????? .setParameter("category",c ).executeUpdate(); c为加载的一个新的类别

12,往数据库中初始化3个名称相同的商品(其他字段自行设置)。 ??? 要求:查询所有商品,如果多个商品的名称相同,则取其中任意一个完整的商品信息

hql? = "from Prodcut pp where pp.product_no in (select max(p.category_id) from Product p group by p.product_name") ; 注意后面的一个 小技巧。 由于group by 后只能包含 group by字段和聚合函数 所以如果我想区别的字段似乎不可能 。但我们不妨将你要取的那个字段也加个聚合函数min 或 max 这样就可以取出你要的 任意字段了。适应sql sql2000中不妨 在pubs 下运行 select max(title_id) as 编号 ,count(type) as? 数量, type from titles group by type 看看结果就知道了 虽然只 group by 了 type但 还是可以得到title_id

?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值