Hibernate Query Solution - Part 2

xiaoxiao2021-04-07  385

Part II: Hibernate Comprehensive Inquiry Solution (This section details, if you have any deficiencies, please write to me)

Everyone can see that the query with parameters must be used to use the Query interface, as above:

Query Query = session.createQuery ("SELECT USERS ASUS AS Uses where users.name =?");

Query.setString (0, name) // Assume Name to pass the parameters

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 to pass the parameters

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, the Find () method:

Find (String Query, Object [] VALUES, TYPE [] TYPES

Where Object [] is an array of storage parameter values, Type [] is an array of storage parameter types, and their order is the same as the order in Query "?". 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 explain in the following:

First of all, I want to create three new objects: Paras.java (parameter object) Paraslist.java (parameter collection object) HQuery.java

(Thanks to my colleague Camel to provide a good code)

1. Paras.java (parameter object)

Package com.ifreeway.homegrown.testing.waf;

/ **

*

*

Title: Define the conditional parameter class for a SQL statement

*

Description: You can use an ordered 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.type is 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 typeno) {

THIS.TYPENO = Typeno;

}

}

2. Paraslist.java (Parameter Collection Object) package com.ifreeway.homegrown.testing.waf;

Import java.util.arraylist;

/ **

*

*

Title: Parameter Set 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: The index value of the parameter

* @PARAM P: Parameter objects need to be added

* /

Public void addparas (int index, paras p) {

Super.Add (INDEX, P);

}

/ **

* Add a parameter object to the last position of the collection

* @PARAM P: Parameter objects need to be added

* /

Public void addparas (Paras P) {

Super.Add (p);

}

/ **

* Acquisition of parameters objects in the specified location

* @Param Index: The index value of the parameter

* @Return: Parameter object

* /

Public Paras getParas (int index) {

Return (Paras) Super.get (INDEX);

}

/ **

* Acquisition of the index of the specified parameter

* @Param P: Parameter object

* @Return: Parameter index

* /

Public int indexofparas (Paras P) {

Return super.indexof (p);

}

/ **

* Remove a specified parameter object from the collection

* @Param Index: Parameter Index

* /

Public void removeparas (int index) {

Super.Remove (INDEX);

}

}

3. HQuery.java

Package com.ifreeway.homegrown.testing.waf;

/ **

*

*

Title: HQL's statement package class

*

Description: This object encapsulates HQL query statement, parameter set, sort parameter, packet parameters, single page start address

*

Copyright: CopyRight (c) 2003

*

company: ifreeway

* @Author Camel

* @version 1.0

* /

Public class hquery {

/ **

* HQL query statement

* /

Private string querystring;

/ **

* Parameter collection object

* /

PRIVATE Paralist Paralitis;

/ **

* Sort field

* /

Private string orderby;

/ **

* Group field

* /

Private string groupty;

/ **

* 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;

}

/ **

* Acquired parameter set objects

* @Return: Parameter collection object

* /

Public paralist getparalist () {

Return Paralist;

}

/ **

* Set the parameter collection object

* @Param Paralist: Parameter Collection Object

* /

Public void setParalist (paralist paralist) {

THIS.PARALIST = Paralist;

}

/ **

* Get the sort field

* @Return: Sort field

* /

Public string getorderby () {

Return ORDERBY;

}

/ **

* Set the order field

* @Param ORDERBY

* /

Public void setORDERBY (STRING ORDERBY) {

THIS.Orderby = OrderBy;

}

/ **

* Get the packet field

* @Return

* /

Public string getGroupby () {

Return GrouPy;

}

/ **

* Set the packet field

* @Param GrouPy

* /

Public void setgroupby (string groupy) {

THIS.GROUPBY = GroupBy;

}

/ **

* Get the start address of the page

* @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 above is:

Use Paras to load each query parameter

Paras Paras = New Paras ();

Paras.SetPname (...);

Paras.Settypeno (...);

Then put it in Paraslist

Paraslist Paraslist = New Paraslist ();

Paraslist.Add (Paras)

Finally, put the Paraslist after the filler to HQuery

HQuery hqueery = new hquery ();

HQuery.SetParalist (Paraslist);

Pioneer we write a public search method to achieve our comprehensive query:

/ **

*

* Comprehensive inquiry, first instant 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 ()); // Do not sort

IF (_Query.getRDerby ()! = 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

Paras param = (paras) list.get (i);

Switch (param.gettypeno ()) {// To increase the corresponding "CASE" according to the increase in parameter type

Case Types.varchar:

Query.setString (i, param.getpname (). TOSTRING ());

Break;

Case Types.integer:

Query.setinteger

i,

(Integer) param.getpname ()). INTVALUE ());

Break;

Case Types.Date:

Query.Setdate (i, (java.sql.date) param.getpname ());

Break;

Case Types.double:

Query.setdouble

i,

(Double) param.getpname ()). DoubleValue ());

Break;

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.sentity (i, (basemodel) param.getpname ());

Break;

}

}

}

/ / Is there a paging, when _query.getpagestartNO () == 0 is not page

IF (_Query.getPagestNO ()! = 0) {

INT Pageno = _Query.getPagestNO ();

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-132545.html

New Post(0)