HibernateCRUD Basic Framework (2)-HQL statement Constructor (HqlQueryBuilder, HqlUpdateBuild

Source: Internet
Author: User
Tags addgroup

The previous section describes the most basic entity classes. This section describes the HQL statement constructor, including query and update.

Advantages: It is faster to construct HQL statements in an object-oriented manner and does not require manual HQL concatenation.

Disadvantages: Encapsulation may reduce performance and only supports common and simple HQL structures.

Some functions are not complete and need to be developed.

1. HQL statement Constructor

Package cn. fansunion. hibernate. SQL; import org. apache. commons. lang. text. strBuilder; import cn. fansunion. hibernate. SQL. entity. from; import cn. fansunion. hibernate. SQL. entity. groupBy; import cn. fansunion. hibernate. SQL. entity. orderGroup; import cn. fansunion. hibernate. SQL. entity. searchCondition;/*** HQL statement constructor. *

* Currently, it is only applicable to one table. Only query conditions support placeholders. We recommend that you use fully constructed SQL statements (whether SQL injection is generated, to be studied ). ** @ Author LeiWen@FansUnion.cn */public class HqlQueryBuilder extends ConstantBase {// query condition combination policy // ------------------------------------- // *********** From ** * ************** // ------------------------------------------- private String select; private From from; // --------------------------------- // ************** Where ******************//----------------------------------- private SearchCondition s EarchCondition; // groups // *************** Group by *************** // ----------------------------- private GroupBy groupBy; // --------------------------------- // ************** Order ******************// ----------------------------------- private OrderGroup orderGroup; // ----------------------------------- // create a query constructor through the constructor. There are too many construction methods for various field combinations. Only three common methods are provided. // We recommend that you use the chained structure or setter method to set attributes. // ------------------------------------- Public HqlQueryBuilder () {} public HqlQueryBuilder (From from) {this. from = from;} public HqlQueryBuilder (From from, SearchCondition searchCondition, GroupBy groupBy, OrderGroup orderGroup) {this. from = from; this. searchCondition = searchCondition; this. groupBy = groupBy; this. orderGroup = orderGroup;} public HqlQueryBuilder select (String select) {this. select = select; return this;} // ------------------------------------- // The chain usage is supported, currently, it is not very good to support ************ // ------------------------------------- public HqlQueryBuilder from (From from) {this. from = from; return this;} public HqlQueryBuilder from (String model) {doFrom (model, ""); return this;} public HqlQueryBuilder from (String model, String alias) {doFrom (model, alias); return this;} public HqlQueryBuilder from (Class Clazz) {doFrom (clazz. getSimpleName (); return this;} public HqlQueryBuilder from (Class Clazz, String alias) {doFrom (clazz. getSimpleName (), alias); return this;} private void doFrom (String model) {doFrom (model, null);} private void doFrom (String model, String alias) {this. from = new From (model, alias);} public HqlQueryBuilder searchCodition (SearchCondition searchCondition) {this. searchCondition = searchCondition; return this;} public HqlQueryBuilder groupBy (GroupBy groupBy ){ This. groupBy = groupBy; return this;} public HqlQueryBuilder orderBy (OrderGroup orderGroup) {this. orderGroup = orderGroup; return this;}/*** convert to HQL statement */public String toHql () {StrBuilder builder = new StrBuilder (); if (select! = Null) {builder. append (select). append (EMPTY);} if (from! = Null) {builder. append (from);} if (searchCondition! = Null) {builder. append (searchCondition);} if (groupBy! = Null) {builder. append (groupBy);} if (orderGroup! = Null) {builder. append (orderGroup) ;}return builder. toString ();}}


2. Hql update statement Constructor

The following is the constructor used to construct and update HQL statements.

Package cn. fansunion. hibernate. SQL. update; import java. util. hashMap; import java. util. map; import cn. fansunion. hibernate. util. pair;/*** Hql update statement constructor. (TODO to be improved) ** @ author LeiWen@FansUnion.cn */public class HqlUpdateBuilder {private Map
 
  
Params; private String model; public HqlUpdateBuilder () {params = new HashMap
  
   
();} Public HqlUpdateBuilder (String model) {} public HqlUpdateBuilder (Class
   Model) {this. model = model. getSimpleName ();} public HqlUpdateBuilder model (String model) {this. model = model; return this;} public HqlUpdateBuilder model (Class
   Model) {this. model = model. getSimpleName (); return this;} public HqlUpdateBuilder param (String key, Object value) {params. put (key, value); return this;} public HqlUpdateBuilder param (Pair... pair) {for (Pair p: pair) {params. put (p. getKey (), p. getValue ();} return this;} public HqlUpdateBuilder param (Map
   
    
Params) {this. params. putAll (params); return this;} public String toHql () {String hql = "update" + model + "set"; for (Map. Entry
    
     
Entry: params. entrySet () {String key = entry. getKey (); hql + = key + "=:" + key + "" ;}return hql ;}}
    
   
  
 


3. Native SQL statement Constructor

Similarly, sometimes the HQL of Hibernate may not be used, but the native SQL is used.

At this time, you can write the SQL version corresponding to HqlQueryBuilder and HqlUpdateBuilder.

Package cn. fansunion. hibernate. SQL; import cn. fansunion. hibernate. SQL. entity. Limit;/*** native SQL statement constructor. (TODO to be improved) ** @ author LeiWen@FansUnion.cn */public class SqlQueryBuilder {// limit only applies to nativeSQL // ------------------------------------- // *********** limit **** * ******** // --------------------------------------------- private Limit limit; /*** convert to an SQL statement */public String toSql () {// TODO return null ;}}


4. Example of using the HQL query Constructor

/*** Example of using the HQL query constructor. ** @ Author LeiWen@FansUnion.cn */public class HqlQueryBuilderTest extends ConstantBase {@ Test public void test () {From = new from (HqlQueryBuilder. class); SearchCondition searchCondition = createSearchCondtion (); OrderGroup orderGroup = createOrderGroup (); GroupBy groupBy = condition (); HqlQueryBuilder = new condition (from, searchCondition, groupBy, orderGroup ); // print directly without using "assertion" println (builder. toHql ();} private GroupBy createGroupBy () {GroupBy groupBy = new GroupBy (); groupBy. addGroup ("name"); groupBy. addGroup ("id"); return groupBy;} private OrderGroup createOrderGroup () {Order order1 = new Order ("id", false); Order order2 = new Order ("name ", "asc"); OrderGroup orderGroup = new OrderGroup (order1, order2); return orderGroup;} private SearchCondition createSearchCondtion () {GroupCondition groupCondition1 = oneGroupCondition (); GroupCondition groupCondition2 = condition (); // String groupStr1 = groupCondition1.toString (); // String groupStr2 = groupCondition2.toString (); // System. out. println (groupStr1); // System. out. println (groupStr2); SearchCondition searchCondition = new SearchCondition (); searchCondition. addGroupCondition (groupCondition1); searchCondition. addGroupCondition (groupCondition2, true); // String searchStr = searchCondition. toString (); // System. out. println (searchStr); return searchCondition;} private GroupCondition oneGroupCondition () {// =, Integer String age = "age"; Integer ageValue = 24; Condition condition2 = new Condition (age, operator. EQUALS, ageValue); String str2 = condition2.toString (); Assert. assertEquals (str2, age + EQUALS_WITH_BLANK + ageValue); // =, String name = "name"; String nameValue = "LeiWen@FansUnion.cn"; Condition condition = new Condition (name, Operator. EQUALS, nameValue); String str = condition. toString (); Assert. assertEquals (str, name + s_s_with_blank + buildQuota (nameValue); // =, Date String date = "date"; Date dateValue = new Date (); condition condition3 = new Condition (date, Operator. EQUALS, dateValue); String str3 = condition3.toString (); Assert. assertEquals (str3, date + EQUALS_WITH_BLANK + buildQuota (dateValue); GroupCondition groupCondition1 = new GroupCondition (); condition (condition); condition (condition2, true); condition (condition3, false); return groupCondition1 ;}}


5. output results

From HqlQueryBuilder where (name = 'leiwen @ FansUnion.cn 'or age = 24 and date = 'mon Dec 30 16:57:21 CST 2013 ')

Or (name = 'leiwen @ FansUnion.cn 'or age = 24 and date = 'mon Dec 30 16:57:21 CST 2013 ')

Group by name, id order by id desc, name asc

Original article link: Http://blog.fansunion.cn/articles/3622 (Xiao Lei blog-blog.fansunion.cn)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.