【Hibernate】Hibernate的多表查詢

來源:互聯網
上載者:User

標籤:create   href   persist   upd   net   成員變數   tco   mat   基本   

在《【Hibernate】Hibernate的聚類查詢、分組查詢、排序與時間之差》(點擊開啟連結)一文中已經講述過怎樣利用HQL語句代替SQL語句。進行聚類查詢、分組查詢、排序與時間之差的查詢,同一時候指出hql能代替sql語句做不論什麼事情。我原本以為hql語句的多表查詢,要先對裡面的實體做Java與xml的改動,事實上並不須要,相同是一句HQL語句就能搞定的事情。SQL的多表查詢已經在《【Mysql】利用內串連與巢狀查詢實現多表查詢。主鍵、外鍵的基本概念》(點擊開啟連結)講過。

比方例如以下的SQL語句:

select t1.Title,t1.Contentfrom blog as t1 ,usertable as t2where t1.userid=t2.id and t2.username=‘a‘

轉換成HQL語句則例如以下:

String hql="select t1.title,t1.content from Blog as t1,Usertable as t2 where t1.userId=t2.id and t2.username=‘a‘"  List<Object> resultList = session.createQuery(hql).list();  for (int i = 0; i < resultList.size(); i++) {      Object[] obj = (Object[])resultList.get(i);      System.out.println(obj[0]+","+obj[1]);  }

核心思想,是把sql語句中的表,寫成Hibernate的實體。sql語句中的欄位、列,寫成Hibernate的實體的成員變數,同一時候必須把表使用as進行t1,t2等標記,不能使用Blog.XX欄位來簡化。如上,就是Blog,Usertable兩張錶轉化為t1,t2標記,把sql的欄位、列,表相應轉化為Hibernate的實體成員變數,實體進行查詢。

Hibernate查詢出來的結果是一個存放Object數組的List,也就是說List的每項都是一個Object數組,Object數組的第n項相應查詢結果的第n項。

能夠再進行下一步的處理。

以下用一個範例,來說明HQL語句的多表查詢。

,Blog記錄了使用者發表的部落格,usertable記錄了使用者的基本資料。

Blog表中的userid與usertable的主鍵id形成參照完整性。


這兩張表在Hibernate的Javaproject種分別相應例如以下實體:

Blog.java

import javax.persistence.*;@Entity@Table(name = "blog")public class Blog {private int id;private String title;private String content;private int userId;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name = "Title")public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}@Column(name = "Content")public String getContent() {return content;}public void setContent(String content) {this.content = content;}@Column(name = "userid")public int getUserId() {return userId;}public void setUserId(int userId) {this.userId = userId;}@Overridepublic String toString() {return id + "," + title + "," + content + "," + userId;}}
Usertable.java

import javax.persistence.*;@Entity@Table(name = "usertable")public class Usertable {private int id;private String username;private String password;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}@Column(name = "username")public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}@Column(name = "password")public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return id + "," + username + "," + password;}}

同一時候,hibernate.cfg.xml做例如以下的配置:

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE hibernate-configuration PUBLIC      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><!--所用的資料庫驅動 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><!--所用的資料庫登入password --><property name="hibernate.connection.password">admin</property><!--所用的資料庫名稱為test,依據實際更改 --><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property><!--所用的資料庫使用者名稱 --><property name="hibernate.connection.username">pc</property><!--所用的資料庫方言,與所用資料庫驅動一樣,能夠在網上查到。這裡是mysql --><property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property><property name="hibernate.format_sql">true</property><!--假設是update表明Hibernate將保留原來的資料記錄,插入時把新記錄加入到已有的表, --><!--假設是create。則總是建立新的表,假設原來資料庫已有的這個表。則這個表的記錄會被所有清洗 --><property name="hibernate.hbm2ddl.auto">update</property><!--羅列Testtable表與Java檔案的映射 --><!--將資料庫中的usertable表,blog表映射到Usertable.java與Blog.java兩個實體 --><mapping class="Usertable" /><mapping class="Blog" /></session-factory></hibernate-configuration>  
能夠發現,這沒有進行不論什麼的參照完整性的指定。

以下,要查詢usertable中username為a的使用者。發表的Blog。

我們知道先要查詢username為a的使用者的id。之後利用這個查出來的id到Blog表中查詢。

用sql語句完畢這個多表查詢,則這樣寫:

select t1.Title,t1.Contentfrom blog as t1 ,usertable as t2where t1.userid=t2.id and t2.username=‘a‘
其查詢結果例如以下:


這使用Hibernate則這樣寫,在HibernateMultiTableTest.java中的代碼例如以下:

import java.util.List;import org.hibernate.*;import org.hibernate.cfg.*;class dbDAO {private Session session;// 建構函式,初始化Session,相當於串連資料庫public dbDAO() {// new Configuration().configure()是吧hibernate.cfg.xml中的全部配置讀取進來// .buildSessionFactory().openSession()是建立Session工廠並執行個體化sessionthis.session = new Configuration().configure().buildSessionFactory().openSession();}// 執行查詢public Query query(String hql) {return session.createQuery(hql);}// 運行插入、改動public void save(Object object) {Transaction transaction = session.beginTransaction();session.save(object);transaction.commit();}// 運行刪除public void delete(Object object) {Transaction transaction = session.beginTransaction();session.delete(object);transaction.commit();}// 解構函式,中斷Session,相當於中斷資料庫的串連protected void finalize() throws Exception {if (session.isConnected() || session != null) {session.close();}}}@SuppressWarnings("unchecked")public class HibernateMultiTableTest {public static void main(String args[]) {dbDAO db = new dbDAO();List<Object> resultList = db.query("select t1.title,t1.content from Blog as t1,Usertable as t2 where t1.userId=t2.id and t2.username=‘a‘").list();//HQL的多表查詢System.out.println("usertable中username為a的使用者,發表的內容例如以下:");System.out.println();for (int i = 0; i < resultList.size(); i++) {Object[] obj = (Object[]) resultList.get(i);System.out.println("標題:" + obj[0]);System.out.println("內容:" + obj[1]);System.out.println();}}}
其運行結果例如以下:

【Hibernate】Hibernate的多表查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.