Hibernate Comprehensive Inquiry Solution

zhaozj2021-02-12  167

Hibernate query solution

These two weeks have been achieved by using the test tool developed by Struts to implement Struts Hibernate. First, from the mood, always keep a happy peace during the development process, "It turned out to be so happy?" It is high efficiency in development efficiency. Now Sun joins JDOCENTRAL.COM and starts to start JDO2.0. Think about it will be an exciting experience after it is waiting for it. Let us wait and see. It is very simple to use Hibernate to manipulate lasting data. Some simple queries here will have brought it. This article focuses on some of my experience when integrated inquiry and paging. If netizens think that my program is still inadequate, please I discuss, my email: plateau_t@sina.com. The first part: Hibernate provides the query interface or method thereof (this section does not know, please refer to the Hibernate manual) 1. The LOAD method to use to use the Session interface according to the id query. Load (Class Theclass, Serializable ID) LOAD (Class Theclass, Serializable ID, LockMode Lockmode) LOAD (Object Object, Serializable ID) 2. The HQL statement is queried 2.

1 With the Query interface, Query is generated by CreateQuery () in the session 1) Query Query = session.createQuery ("SELECT User from User AS User"); 2) Query Query = session.createQuery ("SELECT User from user as user where user.name =?"); Query.setString (0, name) // Assume Name to pass the parameter query = session.createQuery ( "SELECT User from User AS User Where User.Name =: Name"); Query.setString ("Name" // Assume Name to pass the parameters (multiple parameters to push) using the SESSION interface's Find query Find (String Query) Find (String Query) Find (String Query, Object Value, Type Type) Returns List, List List = Session.Find ("SELECT User from Users AS User WHERE User.Name =? ", name, hibernate.string) list list = session.find (" SELECT User from users as user where user.name =? and user.pw =? ", new object [] {name, PW }, new type [] {hibernate.string, hibernate.string}) {Recommended Query method for query} Part 2: Hibern ATE Comprehensive Query Solution (This section details, if you have any deficiencies, please write to me) Everyone can see that the query with parameters must be used as the Query interface, such as the top: query query = session .createQuery ("SELECT USERS AERS AS WHERE USERE.NAME =?"); Query.setString (0, name) // assumes that Name is the parameter passed, but how can I write a public search method in the system? At first, it seems that it is not possible, because the parameters of each query are different, the number of parameters is different (as follows code), then how do we extract common? Query Query = session.createQuery ("SELECT USERS ASUERS AS USERS where users.name =? And users.pw =?");

Query.setString (0, name) // Assume Name Query.setString (1, pw); first, my solution is to find an exit from the find method from the SEESION interface, as follows for the session interface, Find () One of the methods: find (string query, object [] value, type [] types) where the Object [] is the array of parameter values, Type [] is the array of storage parameter types, their order is and Query " ? "The order is the same. So why don't I use the Find method, because if there is a page, then this method will not apply. The solution to which you want to illustrate below: First I want to create three new objects: Paras.java Paraslist.java (Parameter Set Object) HQuery.java (thank my colleague Camel provides a good code) 1. Paras.java (Parameter Object) package com.ifreeway.homegrown.testing.waf; / ** * *

Title: Define a conditional parameter class of a SQL statement *

Description: Ordered can be used Parameter collection to SQL / HQL statement *

Copyright: Copyright (c) 2003 *

Company: ifreeway * @Author camel * @version 1.0 * / public Class Paras {/ ** * Parameter Name * / Private Object PNAME; / ** * Parameter type encoding, the type in java.sql.types remains consistent * / private int Typeno; public object getpname () {Return PNAME;} Public void setpname (object pname) {this.pname = pname;} public int GETTYPENO () {Return Typeno;} public void settypeno (int typepeno) {this.typeno = typeno;}} 2.

Paraslist.java (Parameter Collection Object) package com.ifreeway.homegrown.testing.waf; import java.util.arrayList; / ** * *

Title: Parameter collection class *

Description: package SQL / HQL parameters to the collection class, easy to process and pass *

Copyright: Copyright (c) 2003 *

company: ifreeway * @Author camel * @ Version 1.0 * / public class paralist extends ArrayList {/ ** * Add a parameter object in the specified location * @Param index: parameter index value * @Param P: Need to join the parameter object * / public void Addparas (int INDEX, Paras p) {super.add (index, p);} / ** * Add a parameter object in the last position of the collection * @Param P: Need to join the parameter object * / public void addparas (Paras P) {Super.Add ( p);} / ** * Take the parameter object of the specified location * @Param Index: The index value of the parameter * @return: Parameter object * / public Paras getParas (int index) {Return (Paras) Super.get (INDEX); } / ** * Obtain the index of the specified parameters * @Param P: Parameter object * @return: Parameter index * / public int indexofparas (paraSPaSpaSPARAS (P) {Return Super.indexof (P);} / ** * From the collection A specified parameter object * @Param Index: Parameter Index * / Publi C void RemoveParas (int index) {super.remove (index);}} 3.

HQuery.java package com.ifreeway.homegrown.testing.waf; / ** * *

Title: HQL's statement package *

description: This object encapsulates HQL query statement, parameter collection, Sort parameters, group parameters, single-page start address *

Copyright: Copyright (c) 2003 *

company: ifreeway * @Author camel * @version 1.0 * / Public class hqueery {/ ** * HQL query statement * / private string queryString; / ** * Parameter collection object * / private paralist paralist; / ** * Sort field * / private string orderby; / ** * Group field * / Private string groupby; / ** * Page start query address * / private int pageStartNo; / ** * get a hibernate Query object * @return: query object * / public string getQueryString () {return querystring;} / * * * Set an HQL query string * @Param queryString: Query string * * / public void setQueryString (string queryString) {this.QueryString = queryString;} / ** * acquisition parameter collection object * @return: parameter collection object * / Public paralist getparalist () {return paraList;} / ** * Setting parameter set object * @Param Paralist: Parameter collection object * / public void setPARALIST (THIST PARALIST) {this.Paralist = paraList;} / ** * gets the order field * @return: Sort field * / public string getorderby () {RETURN ORDERBY;} / ** * Settings Sort field * @Param Orderby * / public void setorderby (String ORDERBY) {this.Orderby = Orderby;} / ** * Get Packet field * @return * / public string getGroupby () {RETURN GROUPY;} / ** * Setting Group field * @Param Groupby * / public void setgroupby (string groupby) {this.groupby =

GrouPy;} / ** * Number page start address * @Return * / public int getpagestNO () {return pagestartNO;} / ** * Settings page start address * @Param Pagestartno * / public void setpagestNO (int pagestartno) { This.pagestartno = PagestartNO;}} The relationship between the three objects is: loading each query parameter parat paras = new paraS (); Paras.SetTypeno (...); Then put it in Paraslist Paraslist = new paraslist (); Paraslist.add (Paras) finally puts the parachaslist after filling to hquery hquery hqueery = new hquery (); hquery.setparalist (paraSlist); first we write a public lookup Method, to achieve our comprehensive query: / ** * * Comprehensive query, first instantiate hquery * @see com.ifreeway.homegrown.testing.common.waf.dbhandler # Find (com.Ifreeway.homegrown.testing.common. waf.HQuery) * / public List find (HQuery _query) throws HibernateException {List itr = null; try {StringBuffer query_str = new StringBuffer (_query.getQueryString ()); // if you want to sort if (_query.getOrderby () =! NULL) {query_str.Append (_Query.getRDerby ());} // Do you want to group if (_Query.getGroupby ()! = NULL) {query_str.Append (_Query.getGroupby ());} session session = getsession (); query query = session.createQuery (query_str.tostring ()); if (_Query.getPARALIST ()! = null) {List List = _Query.getPARALIST (); for (int i = 0; i

Case Types.date: Query.Setdate (i, (java.sql.date) param.getpname ()); Break; Case Types.double: Query.SetDouble (i, ((Double) param.getpname ()). DoubleValue ()); Case Types.Boolean: Query.setBoolean (i, ((boolean) param.getpname ()). BooleanValue ()); Break; Case Types.char: Query.setCharacter (i, ((Character) Param.getpname ()). Charvalue ()); Break; Case Types.java_Object: Query.seTentity (i, (basedmodel) param.getpname ()); Break;}}} // Is there a paging, when _query. getPageStartNo () == 0 is not paged if (_query.getPageStartNo () = 0!) {int pageno = _query.getPageStartNo (); query.setFirstResult ((pageno - 1) * Constants.RECORD_PER_PAGE); query.setMaxResults (( Pageno) * constants.record_per_page);}} itr = query.list (); CloseSession ();} catch (exception e) {

转载请注明原文地址:https://www.9cbs.com/read-6102.html

New Post(0)