Hibernate query for multiple table associations

Source: Internet
Author: User

As a result of the company's project needs, I have hibernate on a number of related query research, now summarized as follows, for friends reference.

Introduction to Hibernate Hibernate is an JDO tool. It works by creating a mapping relationship between a value object and a database table through a file (typically two types: XML files and properties files). In this way, we can achieve the purpose of using a database by manipulating these value objects and some of the basic classes provided by Hibernate. For example, using a hibernate query, you can return a list of a value object directly, without having to load the data of the result set into a single value object in the same way as traditional JDBC access, saving a lot of time for coding. The HQL provided by Hibernate is a class-SQL language that provides an object-oriented database query like EJBQL, but HQL is very close to standard SQL in terms of functionality and usage.

Second, the difference between hibernate and JDBC the main differences between Hibernate and JDBC are as follows:

1, Hibernate is a lightweight object encapsulation of JDBC, it is an independent object persistence layer framework, and app Server, and EJB have no inevitable connection. Hibernate can be used in any situation where JDBC can be used and, in a sense, hibernate to replace JDBC in any situation.

2, Hibernate is a framework closely related to JDBC, so hibernate compatibility and JDBC driver, and database have a certain relationship, but with its Java program, and app server has no relationship, there is no compatibility issues.

3. Hibernate is a substitute for JDBC and cannot be used to compare directly with entity beans.

Three, Hibernate to carry on the Multiple Table association query Hibernate query to multiple tables, the query result is the Cartesian product of multiple tables, or "cross" connection. For example: From Student, book from Student as Stu, book as Boo to Student Stu, book Boo Note: Let the Student and book in the query be the class name corresponding to the table Student and the book. Its name must be the same as the name of the class, including the case of letters. It is a good practice for aliases to be subject to the first-letter lowercase rules, which is consistent with Java's naming conventions for local variables.

The following is a complete example to illustrate the hibernate of multiple tables (where bold is our special attention, the corresponding table tbookinfo and bookselection structure and its corresponding hbm.xml, class files are not enumerated, For those who need more information, please contact me:

String sTest = "from tBookInfo book, BookSelection sel where book.id = sel.bookId";
Collection result = new ArrayList();
Transaction tx = null;
try {
 Session session = HibernateUtil.currentSession();
 tx = session.beginTransaction();
 Query query = session.createQuery(sql);
 result = query.list();
 tx.commit();
} catch (Exception e) {
 throw e;
} finally {
 HibernateUtil.closeSession();
}
ArrayList sList = (ArrayList) result;
Iterator iterator1 = sList.iterator();
while (iterator1.hasNext()) {
 Object[] o = (Object[]) iterator1.next();
 tBookInfo bookInfo = (tBookInfo) o[0];
 BookSelection bookSelect = (BookSelection) o[1];
 System.out.println("BookInfo-Title: " + bookInfo.getTitle());
 System.out.println("BookSelection-BookSelectionId: " + bookSelect.getId());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.