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