Hibernate检索(下)
Posted Mr.Liu’blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate检索(下)相关的知识,希望对你有一定的参考价值。
1.表结构
2.HQL连接查询
Hibernate支持的连接有:
inner join
left outer join
right outer join
inner join只能得到满足连接条件的记录,左右表交换位置,结果不变。
left join 左表中的数据必须出现在结果集中,哪怕不满足连接条件。
right join 右表中的数据必须出现在结果集中,哪怕不满足连接条件。
3.实体类
1 package cn.yunhe.entity; 2 3 import javax.persistence.*; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 @Entity 8 @Table(name = "users") 9 public class Users { 10 private int id; 11 private String loginId; 12 private String loginPwd; 13 private String name; 14 private int age; 15 private String phone; 16 private Set<Orders> orders= new HashSet<Orders>(); 17 18 @Id 19 @GeneratedValue 20 @Column(name = "id") 21 public int getId() { 22 return id; 23 } 24 25 public void setId(int id) { 26 this.id = id; 27 } 28 29 @Column(name = "loginId") 30 public String getLoginId() { 31 return loginId; 32 } 33 34 public void setLoginId(String loginId) { 35 this.loginId = loginId; 36 } 37 38 @Column(name = "loginPwd") 39 public String getLoginPwd() { 40 return loginPwd; 41 } 42 43 public void setLoginPwd(String loginPwd) { 44 this.loginPwd = loginPwd; 45 } 46 47 @Column(name = "name") 48 public String getName() { 49 return name; 50 } 51 52 public void setName(String name) { 53 this.name = name; 54 } 55 56 @Column(name = "age") 57 public int getAge() { 58 return age; 59 } 60 61 public void setAge(int age) { 62 this.age = age; 63 } 64 65 @Column(name = "phone") 66 public String getPhone() { 67 return phone; 68 } 69 70 public void setPhone(String phone) { 71 this.phone = phone; 72 } 73 74 @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "users")//双向时不要@JoinColumn 75 //@JoinColumn(name = "userid") 76 public Set<Orders> getOrders() { 77 return orders; 78 } 79 80 public void setOrders(Set<Orders> orders) { 81 this.orders = orders; 82 } 83 }
1 package cn.yunhe.entity; 2 3 import javax.persistence.*; 4 import java.util.Date; 5 6 /** 7 * Created by Administrator on 2017/7/11. 8 */ 9 @Entity 10 @Table(name = "orders") 11 public class Orders { 12 private int id; 13 private Date orderDate; 14 private double totalPrice; 15 private Users users; 16 17 @Id 18 @GeneratedValue 19 @Column(name = "id") 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 @Column(name = "orderDate") 29 public Date getOrderDate() { 30 return orderDate; 31 } 32 33 public void setOrderDate(Date orderDate) { 34 this.orderDate = orderDate; 35 } 36 37 @Column(name = "totalPrice") 38 public double getTotalPrice() { 39 return totalPrice; 40 } 41 42 public void setTotalPrice(double totalPrice) { 43 this.totalPrice = totalPrice; 44 } 45 46 @ManyToOne(fetch = FetchType.EAGER) 47 @JoinColumn(name = "userid") 48 public Users getUsers() { 49 return users; 50 } 51 52 public void setUsers(Users users) { 53 this.users = users; 54 } 55 }
4.配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration 3 PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--正向工程(自动建表)--> 8 <property name="hbm2ddl.auto">update</property> 9 <!--数据库方言--> 10 <property name="hibernate.dialect">org.hibernate.dialect.mysqlDialect</property> 11 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 12 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 13 <property name="connection.username">root</property> 14 <property name="connection.password">1234</property> 15 <!--显示sql语句--> 16 <property name="show_sql">true</property> 17 18 <mapping class="cn.yunhe.entity.Users"/> 19 <mapping class="cn.yunhe.entity.Orders"/> 20 21 </session-factory> 22 </hibernate-configuration>
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>cn.yunhe</groupId> 5 <artifactId>hibernate5</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>hibernate5 Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>4.12</version> 15 <scope>test</scope> 16 </dependency> 17 18 <dependency> 19 <groupId>javax.servlet</groupId> 20 <artifactId>javax.servlet-api</artifactId> 21 <version>3.1.0</version> 22 <scope>provided</scope> 23 </dependency> 24 25 <dependency> 26 <groupId>mysql</groupId> 27 <artifactId>mysql-connector-java</artifactId> 28 <version>5.1.6</version> 29 </dependency> 30 31 <dependency> 32 <groupId>org.apache.struts</groupId> 33 <artifactId>struts2-core</artifactId> 34 <version>2.3.31</version> 35 </dependency> 36 37 <dependency> 38 <groupId>javax.servlet</groupId> 39 <artifactId>jstl</artifactId> 40 <version>1.2</version> 41 </dependency> 42 43 <dependency> 44 <groupId>org.apache.struts</groupId> 45 <artifactId>struts2-dojo-plugin</artifactId> 46 <version>2.3.16</version> 47 </dependency> 48 49 <dependency> 50 <groupId>org.hibernate</groupId> 51 <artifactId>hibernate-core</artifactId> 52 <version>3.5.0-Final</version> 53 </dependency> 54 55 <dependency> 56 <groupId>org.hibernate</groupId> 57 <artifactId>hibernate-annotations</artifactId> 58 <version>3.5.0-Final</version> 59 </dependency> 60 <dependency> 61 <groupId>org.slf4j</groupId> 62 <artifactId>slf4j-log4j12</artifactId> 63 <version>1.5.8</version> 64 </dependency> 65 66 <dependency> 67 <groupId>org.javassist</groupId> 68 <artifactId>javassist</artifactId> 69 <version>3.13.0-GA</version> 70 </dependency> 71 </dependencies> 72 <build> 73 <finalName>hibernate5</finalName> 74 </build> 75 </project>
5.测试类
1 package cn.yunhe.Demo; 2 3 import cn.yunhe.entity.Books; 4 import cn.yunhe.entity.Orders; 5 import cn.yunhe.entity.Publishers; 6 import cn.yunhe.entity.Users; 7 import org.hibernate.Criteria; 8 import org.hibernate.Query; 9 import org.hibernate.Session; 10 import org.hibernate.Transaction; 11 import org.hibernate.cfg.AnnotationConfiguration; 12 import org.hibernate.cfg.Configuration; 13 import org.hibernate.criterion.Order; 14 import org.hibernate.criterion.Projections; 15 import org.hibernate.criterion.Restrictions; 16 import org.junit.After; 17 import org.junit.Before; 18 import org.junit.Test; 19 20 import java.util.List; 21 22 /** 23 * Created by Administrator on 2017/7/11. 24 */ 25 public class Demo { 26 Session session=null; 27 Transaction tr=null; 28 29 @Before 30 public void init(){ 31 Configuration config =new AnnotationConfiguration().configure(); 32 session= config.buildSessionFactory().openSession(); 33 tr=session.beginTransaction(); 34 } 35 36 @Test //Hibernate内链接 查询订单中的用户的信息 37 public void test1(){ 38 String hql="select o.users from Orders o where o.id=1"; 39 Query query=session.createQuery(hql); 40 List<Users> list= query.list(); 41 session.close();//关闭也可以查出来 42 Users user=list.get(0); 43 System.out.println(user.getName()); 44 } 45 46 @Test //Hibernate内链接 使用交叉连接查询 47 public void test2(){ 48 String hql="select u from Users u,Orders o where u=o.users and o.id=3"; 49 List<Users> list= session.createQuery(hql).list(); 50 for (Users users:list){ 51 System.out.println(users.getName()); 52 } 53 } 54 55 @Test//Hibernate内链接 查询所有购买过书籍的用户,采用子查询 56 public void test3(){ 57 String hql="select u from Users u where u in(select o.users from Orders o)";//子查询 58 //String hql="select distinct u from Users u inner join u.orders";//内连接 59 List<Users> list =session.createQuery(hql).list(); 60 for (Users users:list){ 61 System.out.println(users.getName()); 62 } 63 } 64 65 @Test//Hibernate内链接 抓取内链接 66 public void test4(){ 67 String hql="select distinct u from Users u inner join u.orders"; 68 List<Users> list= session.createQuery(hql).list(); 69 session.close();//关闭session 70 for (Users users:list){ 71 System.out.println(users.getName()+"的订单:"); 72 for (Orders orders:users.getOrders()){ 73 System.out.println(orders.getId()+"\\t"+orders.getTotalPrice()); 74 } 75 } 76 } 77 78 @Test//Hibernate外链接 left outer join(左外链接) 79 //查询没有任何订单的用户 80 //left join中也可以使用fetch,即:left join fetch,表示是否填充左边(Users)中的集合属性 81 public void test5(){ 82 String hql="select u from Users u left join u.orders o where o is null"; 83 List<Users> list= session.createQuery(hql).list(); 84 for (Users users:list){ 85 System.out.println(users.getName()); 86 }以上是关于Hibernate检索(下)的主要内容,如果未能解决你的问题,请参考以下文章