In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
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 realize multi-table query in Hibernate". 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. Www_bitscn_com of China Network Management Alliance
2. Differences between Hibernate and JDBC the main differences between Hibernate and JDBC are as follows:
◆ Hibernate is JDBC's lightweight object wrapper, which is an independent object persistence layer framework that has nothing to do with App Server or EJB. Hibernate can be used in any situation where JDBC can be used. In a sense, Hibernate replaces JDBC.
◆ 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 the Java program that uses it, App Server, and there is no compatibility problem. China Network Management Forum bbs.bitsCN.com
◆ Hibernate appears as a substitute for JDBC and cannot be directly compared with Entity Bean.
3. Hibernate makes multi-table association query when Hibernate queries multiple tables, 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.
Here is a complete example to illustrate the Hibernate multi-table query
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) {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 ());} "how to implement multi-table query in Hibernate" ends here. Thank you for 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.