博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JPA api 学习
阅读量:4992 次
发布时间:2019-06-12

本文共 10060 字,大约阅读时间需要 33 分钟。

1. brand类 package pr.cgl.entity; import javax.persistence.*; import java.util.Set; /**  * Created by CGL on 2015/9/16.  */ @Entity @Table(name = "xx_brand") public class Brand {
@Id @GeneratedValue private Long id; private String name; @OneToMany(targetEntity = Product.class, fetch = FetchType.LAZY) private Set
products; public Long getId() {
return id; } public void setId(Long id) {
this.id = id; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public Set
getProducts() {
return products; } public void setProducts(Set
products) {
this.products = products; } @Override public String toString() {
return "Brand{" + "id=" + id + ", name='" + name + '\'' + ", products=" + products + '}'; } } 2. product 类
package pr.cgl.entity; import javax.persistence.*; import java.util.Set; /**  * Created by LL on 2015/9/16.  */ @Entity @Table(name ="xx_product") public class Product {
@Id @GeneratedValue private Long id; private String name; @ManyToMany(targetEntity = Promotion.class) @JoinTable(name = "xx_promotion_product", joinColumns = {@JoinColumn(name = "products")},inverseJoinColumns = {@JoinColumn(name="promotions")}) private Set
promotions; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "b_id",insertable = true) private Brand brand; public Brand getBrand() {
return brand; } public void setBrand(Brand brand) {
this.brand = brand; } public Long getId() {
return id; } public void setId(Long id) {
this.id = id; } public String getName() {
return name; } public void setName(String name) {
this.name = name; } public Set
getPromotions() {
return promotions; } public void setPromotions(Set
promotions) {
this.promotions = promotions; } @Override public String toString() {
return "Product{" + "id=" + id + ", name='" + name + '\'' + ", promotions=" + promotions + // ", brand=" + brand + '}'; } } 3. 练习
package pr.cgl.test; import org.junit.Test; import pr.cgl.entity.Brand; import pr.cgl.entity.Product; import pr.cgl.entity.Promotion; import javax.persistence.*; import javax.persistence.criteria.*; import javax.sql.rowset.Predicate; import java.util.*; /**  * Created by LL on 2015/9/16.  */ public class TestMain {
static EntityManagerFactory emp = null; static EntityManager em = null; static CriteriaBuilder cb = null; static {
emp = Persistence.createEntityManagerFactory("jpaUnit"); em = emp.createEntityManager(); cb = emp.getCriteriaBuilder(); } public static void main(String[] args) {
Map properties = new HashMap(); properties.put("hibernate.show_sql", "true"); EntityManagerFactory emp = Persistence.createEntityManagerFactory("jpaUnit"); EntityManager em = emp.createEntityManager(); CriteriaBuilder cb = emp.getCriteriaBuilder(); CriteriaQuery
c = cb.createQuery(Product.class); Root
root = c.from(Product.class); c.select(root); Join
innerJoin = root.join("promotions", JoinType.LEFT); Join
rightJoin = root.join("brand", JoinType.LEFT); List
criteria = new ArrayList
(); ParameterExpression
productId = cb.parameter(Long.class, "id"); ParameterExpression
bId = cb.parameter(Long.class, "bId"); // criteria.add(cb.equal(root.get("id"), productId)); // criteria.add(cb.equal(root.get("brand").get("id"), bId)); // c.where(cb.and(criteria.toArray(new Predicate[0]))); // c.where(criteria.get(1)); TypedQuery
q = em.createQuery(c); System.out.println(q.toString()); q.setParameter("id", 855L); q.setParameter("bId", 2L); List
productList = q.getResultList(); for(Product pp: productList){ System.out.println("id="+pp.getId()+" name="+pp.getName()); Set
promotionSet = pp.getPromotions(); /* for(Promotion promotion: promotionSet){ System.out.println(promotion.getName()); }*/ } /* System.out.println(emp); EntityManager em = emp.createEntityManager(); Query query = em.createNativeQuery("select id, name from xx_product", Product.class); List
productList = query.getResultList(); for(Product p: productList){ System.out.println("id="+p.getId()+" name="+p.getName()); }*/ } //练习conjuction @Test public void conjunction(){ CriteriaQuery
q = cb.createQuery(Product.class); Root
productRoot = q.from(Product.class); Root
brandRoot = q.from(Brand.class); javax.persistence.criteria.Predicate predicate = cb.conjunction(); predicate = cb.and(predicate, cb.equal(productRoot.get("brand").get("id"), brandRoot.get("id"))); q.select(productRoot); q.where(predicate); TypedQuery
query = em.createQuery(q); List
list = query.getResultList(); for(Product p: list){ System.out.println(p.toString()); } } //练习join 1.inner join @Test public void join1(){ CriteriaQuery
q = cb.createQuery(Product.class); Root
productRoot = q.from(Product.class); // Root
brandRoot = q.from(Brand.class); productRoot.join("brand", JoinType.INNER); TypedQuery
query = em.createQuery(q); List
list = query.getResultList(); for(Product p: list){ System.out.println(p.toString()); } } //练习join 2.left join @Test public void join2(){ CriteriaQuery
q = cb.createQuery(Product.class); Root
productRoot = q.from(Product.class); // Root
brandRoot = q.from(Brand.class); productRoot.join("brand", JoinType.LEFT); TypedQuery
query = em.createQuery(q); List
list = query.getResultList(); for(Product p: list){ System.out.println(p.toString()); } } //练习join 3.left join 重要 一关联多 @Test public void join3(){ CriteriaQuery
q = cb.createQuery(Brand.class); // Root
productRoot = q.from(Product.class); Root
brandRoot = q.from(Brand.class); SetJoin
productJoin = brandRoot.joinSet("products", JoinType.LEFT); TypedQuery
query = em.createQuery(q); List
list = query.getResultList(); for(Brand b: list){ System.out.println(b.toString()); } } @Test public void subQuery(){ CriteriaQuery
criteriaQuery = cb.createQuery(Product.class); Root
from = criteriaQuery.from(Product.class); Path
path = from.get("brand"); // field to map with sub-query /*from.fetch("name"); from.fetch("id");*/ CriteriaQuery
select = criteriaQuery.select(from); Subquery
subquery = criteriaQuery.subquery(Brand.class); Root fromBrand = subquery.from(Brand.class); // ParameterExpression
name = cb.parameter(String.class, "name"); subquery.select(fromBrand.get("id")); // field to map with main-query subquery.where(cb.and(cb.equal(fromBrand.get("name"), "brand1"), cb.equal(fromBrand.get("id"),1L))); select.where(cb.in(path).value(subquery)); TypedQuery
typedQuery = em.createQuery(select); List
list = typedQuery.getResultList(); for(Product p: list){ System.out.println(p.toString()); } } @Test public void subQuery2(){ CriteriaQuery
criteriaQuery = cb.createQuery(Product.class); Root
root = criteriaQuery.from(Product.class); criteriaQuery.where(cb.and(cb.in(root.get("id")).value(1L).value(2L))); TypedQuery
typedQuery = em.createQuery(criteriaQuery); List
list = typedQuery.getResultList(); for(Product p: list){ System.out.println(p.toString()); } } // 查询多个字段 @Test public void test3(){ CriteriaQuery
cq = cb.createTupleQuery(); Root
root = cq.from(Product.class); cq.select(cb.tuple(root.get("id").alias("id"), root.get("name").alias("name"))); cq.where(cb.and(cb.in(root.get("id")).value(888L).value(899L))); TypedQuery
typedQuery = em.createQuery(cq); List
list = typedQuery.getResultList(); for(Tuple p: list){ System.out.println("id="+p.get("id")+" name="+p.get("name")); } } // count 函数 @Test public void test4(){ CriteriaQuery
cq = cb.createTupleQuery(); Root
root = cq.from(Product.class); cq.select(cb.tuple(root.get("id").alias("id"), root.get("name").alias("name"), cb.count(root.get("id")).alias("priceAll"), cb.sum(root.
get("id")).alias("sum") )); cq.where(cb.and(cb.in(root.get("id")).value(888L).value(899L))); TypedQuery
typedQuery = em.createQuery(cq); List
list = typedQuery.getResultList(); for(Tuple p: list){ System.out.println("id="+p.get("id")+" name="+p.get("name")+" priceAll="+p.get("priceAll")+" sum="+p.get("sum")); } } // 子查询 @Test public void test5(){ CriteriaQuery
brandQuery = cb.createQuery(Brand.class); Root
brandRoot = brandQuery.from(Brand.class); javax.persistence.criteria.Predicate predicate = cb.gt(brandRoot.
get("id"), 2L); brandQuery.where(predicate); TypedQuery
typedQuery = em.createQuery(brandQuery); List
list = typedQuery.getResultList(); for(Brand p: list){ System.out.println("id="+p.getId()+" name="+p.getName()+" sum="+p.toString()); } } // expression min max // 打印sql @Test public void test6(){ CriteriaQuery
brandQuery = cb.createTupleQuery(); Root
brandRoot = brandQuery.from(Brand.class); Expression min = cb.min(brandRoot.
get("id")); Expression max = cb.max(brandRoot.
get("id")); brandQuery.select(cb.tuple(min.alias("id1"), max.alias("id2"))); TypedQuery
typedQuery = em.createQuery(brandQuery); List
list = typedQuery.getResultList(); System.out.println("a:"+typedQuery.unwrap(org.hibernate.Query.class).getQueryString()); for(Tuple p: list){ System.out.println("id="+p.get("id2")+" "+p.get(1)); } } @Test public void test7(){ String sql = "select p.id, p.name, p.b_id from xx_product p"; Query query = em.createNativeQuery(sql, Product.class); List
list = query.getResultList(); System.out.println(list); for(Product p: list){ if(p.getBrand() != null){ System.out.println("id="+p.getId()+" name="+p.getName()+" promotions="+p.getPromotions()+" brand="+p.getBrand()); } } } }

转载于:https://www.cnblogs.com/cglWorkBook/p/4953544.html

你可能感兴趣的文章
Mysql 基础3
查看>>
smartctl工具应用(转载整理)
查看>>
控件数据绑定总结
查看>>
HTTP协议
查看>>
Vue 框架-09-初识组件的应用
查看>>
.Net core 在类库中获取配置文件Appsettings中的值
查看>>
[转载]sublime用法精华
查看>>
《甄嬛传》影评(整理)
查看>>
数的位数
查看>>
MySQL合并多行
查看>>
[openstack] RDO Quickstart
查看>>
[转载]struts2 中的 addActionError 、addFieldEr
查看>>
[转载]我的PMP复习备考经验谈(上篇)—— 一本关于PMP备考的小指南
查看>>
Mysql命令集
查看>>
记java应用linux服务单个CPU使用率100%分析
查看>>
将文件字节输出流写入到文本中
查看>>
Linux编程之给你的程序开后门
查看>>
Ubuntu下Hadoop的安装和配置
查看>>
VS2010中生成遇到的 web.config 问题
查看>>
Nginx安装部署(反向代理与负载均衡)
查看>>