Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Multi-table Association query in Hibernate

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to implement Hibernate multi-table association query". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction to Hibernate Hibernate is a JDO tool

It works by establishing a mapping relationship between value objects and database tables through files (there are generally two kinds of files: xml files and properties files). In this way, we only need to manipulate these value objects and some basic classes provided by Hibernate to achieve the purpose of using the database. For example, a query using Hibernate can directly return a list containing a value object (List) without having to load the data of the result set into a value object one by one like the traditional JDBC access method, saving a lot of time for coding. HQL provided by Hibernate is a kind of SQL language, which provides object-oriented database query like EJBQL, but HQL is very close to the standard SQL in function and usage.

II. The difference between Hibernate and JDBC

1. Hibernate is the lightweight object encapsulation of JDBC, it is an independent object persistence layer framework, and App Server, and EJB are not necessarily related. Hibernate can be used in any situation where JDBC can be used. In a sense, Hibernate replaces JDBC.

2. Hibernate is a framework closely related to JDBC, so the compatibility of Hibernate has something to do with JDBC driver and database, but it has nothing to do with Java programs and App Server, and there is no compatibility problem.

3. Hibernate appears as a substitute for JDBC and can not be directly compared with Entity Bean.

3. Hibernate multi-table association query

When doing an Hibernate multi-table association query, the query result is the Cartesian product of multiple tables, or "cross" join. For example: from Student, Book from Student as stu, Book as boo from Student stu, Book boo Note: let the Student and Book in the query be the class names corresponding to the tables student and book, and its name must be the same as the class name, including the case of letters. It is a good habit that aliases should obey the rule of lowercase, which is consistent with Java's naming conventions for local variables.

The following is a complete example to illustrate the Hibernate multi-table association query (where bold is what we should pay special attention to. The structure of the corresponding tables tBookInfo and BookSelection and their corresponding hbm.xml and class files are not listed one by one. For friends 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 ())

}

This is the end of the content of "how to implement Hibernate multi-table association query". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report