From: http: //dev.9cbs.net/develop/Article/63/63043.shtm
Hibernate is a typical OPM tool that makes each physical form mapping into objects (Object), which exerts object-oriented advantages, allowing design and developers to manage the database from object-oriented perspectives. When designed to multi-table operation, Hibernate provides object mapping relationships corresponding to the database table relationship, one-on-one, one-to-many and multi-to-many, all over here, can be implemented by Hibernate object mapping relationships (SET, etc.). This provides a convenient way for multi-table operations in general. There is a lot of introductions in this regard, and it is no longer repeated here.
However, in some cases, multi-table operations, such as a statistical customer's total amount of SQL operations in 2005, as follows: Select B.Name, Count (A.CHARGEEGINTIME) Max (a.chargeendtime) From Charge A, Customer B Where A.idcustomer = B. IDCUSTOMER AND A.CHARGEBEGINTIME> = '2005-01-01' and a.chargendtime <'2005-12-31' Gourp By A. IDCUSTOMERCUSTOMER table and the Charge structure is as follows: Customer Table Structure: ------------ ----------- ---- ----- --- ---- ------- | Field | Type | Null | Key | Default | Extra | ------------ -------- --- ------ ----- --------- ------ | IDCUSTOMER | VARCHAR (32) | | Pri | || Name VARCHAR (30) | | | | | ------------ ------------ ------ --- ------- ----- Charge Table Structure: ---------------- -------- ----- ------ ---- --------- ------- | Field | Type | NULL | Key | Default | Extra | ----------------------------- ---- ----- ----- | IDCHARGE | VARCHAR (32) | | Pri | | || Fee | Double | Yes | | Null | || Chargetimeb EGIN | DATETIME | YES | | NULL | || ChargetimeEnd | DateTime | YES | | Null | | -------------------------------------------- --- ------ ---- --------- ----- There is a multi-table inquiry operation in Hibernate's own belt documentation Tip: "SELECT New OjBectc (Field1, Field2, ...) from Objecta A, ObjectB B ...", you can see that this operation has two shortcomings: 1) must declare and create class Objectc, according to Hibernate Features, you need to write an ObjectC.hbm.xml PO mapping. When you use new objects that create query results, this mapping can be a virtual, that is, there is no real database table and Objectc,
However, such a virtual logic obviously violates Hibernate's ideological original intention; 2) This method can only query but the results record and can only create a single ObjectC object, which is very limited, so in some cases You cannot use (such as this example). Therefore, for this example, the above method is inventory. In fact, take a closer look at Hibernate's API, you will find this problem very well. In the net.sf.hibernate package, there are three interfaces that are very useful: 1. Interface Scrollableresults This interface is similar to the RESULTSET in JDBC, providing traversal and field access methods for returning results, such as: public boolean next () Clap after moving public boolean previous () Cursor forward PUBLIC Boolean Scroll (INT i) cursor moves to the specified unknown public void beforefirst () cursor in the first record PUBLIC VOID AFTERLAST () Cursor after the last record, PUBLIC Object [] Get () Returns public object GET (INT i) in an Object Object Architecture, Returns the specific field value of the current record in the form of the current record in the object object, and the current record's specific field value is in an Integer object. Returns Public long getlong (int co) Returns public string gettext (int COL) to return the current record with a specific field value with a TEXT object with a LONG object. The value returns in String objects ... Wait 2, the Interface Query Query interface encapsulates the operation of the database for the database, here, what we use it is that its scroll () method can return a scrollableresults instance: Public ScrollableResults Scroll () returns the results in the ScrollableResults instance, but you need to pay attention to the result of the query returned. In fact, it is only some ID. When needed (for example, when we use the scrollableresults.next () method, this time you need to use it. The record will be real initialization (this technology can be called: delay initialization) Uery createQuery (String querystring) creates a Query instance with the HQL query string, understand the three interfaces above, and the problem can be solved very well.
The following documents are required: Customer.Java PO objects Totalcharge.java Totalcharge.java is used to save statistics beancustomer.hbm.xml po mappings Charge.hbm.xml PO mapping TotalChargedao.java statistics DAO definition TotalChargedaoImpl.java statistics DAO definition Implement the DAOFACTORY.JAVA DAO factory HibernateSessionFactory.java session factory, because the main discussion here is the joint query for Customer and Charge, so Customer.java, Charge.java, Customer.hbm.xml, Charge.hbm.xml four files, Totalchargedao.java, DAOFACTORY.JAVA, HibernateSessionFactory.java source code is omitted here. TotalCharge.java source code: package test.bean; / ** * Author: Sun Star ** / public class TotalCharge {private String name; private Double fee; private java.util.Date chargeTimeBegin; private java.util.Date chargeTimeEnd ; public TotalCharge () {} public String getName () {return name;} public TotalCharge (String name, Double fee, java.util.Date chargeTimeBegin, java.util.Date chargeTimeEnd) {this.name = name; this.fee = fee; this.chargeTimeBegin = chargeTimeBegin; this.chargeTimeEnd = chargeTimeEnd;} public void setName (String name) {this.name = name;} public Double getFee () {return fee;} public void setFee (Double fee) {this .fee = fee;} public java.util.Date getChargeTimeBegin () {return chargeTimeBegin;} public void setChargeTimeBegin (java.util.Date chargeTimeBegin) {this.chargeTimeBegin = chargeTimeBegin;} public java.util.Date getChargeTimeEnd () {return Public void setchargetimend (java.util). Date ChargetimeEnd) {this.chargetimeend = chargetimend;}}
TotalchargedaoImpl.java code:
package test.dao.impl; import java.util *; import test.bean *; import test.dao *; import net.sf.hibernate *; / ** * Author:.... Sun Star ** / public class TotalChargeDaoImple extends TotalChargeDao {// following method from the integrated TotalChargeDao public List statTotalCharge (Date statTimeBegin, Date statTimeEnd) throws DaoException {List res = new Vector (); // save the result set for storing Session session = null; ScrollableResults srs = Null; try {session = hibernaterationFactory.opensesis (); // Get a hibernate session // Create an anonymous query instance and call it scroll () method Returns the query result organized by ScrollableResults SRS = session.createQuery ("SELECT B.Name, Count (A.Fee) Max (a.chargeendtime) from Charge A, Customer B Where A.idCustomer = B.IDCustomer and a.chargebeGintime> =? and a.chargeendtime Gourp By a.idcustomer "). setdate (0, stattimebegin) .SETDATE (1, stattimeeend). Scroll (); // Put the query result Save while (srs.next ()) {res. named (New Totalcharge). Srs.getstring (0), SRS , getDouble (1), srs.getdate (2), srs.getdate (3)));}}}}}} catch (hibernateException He) {; // Loging Err ..... if (srs! = null) {TRY { Srs.close ();} catch (exception e) {;}}} finally}}} finally}}} finally ();} catch (exception e) {;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}