Spring Data JPA Specification Query

Source: Internet
Author: User

 Private specification<vo> createspecification (final map<string, string> filterdata) throws Exception {        Final map<string, string> filteroperators = new hashmap<> ();                Final map<string, object> filterparams = new HashMap ();                Extractfilteroperatorsandparams (Filterdata, filteroperators, filterparams); Specification<vo> spec = new specification<vo> () {@Override public predicate Topredicat  E (root<vo> Root, criteriaquery<?> query, Criteriabuilder CB) {list<predicate> predicates                = new Arraylist<> ();                    For (map.entry<string, object> entry:filterParams.entrySet ()) {//get path to leaf node                    path<?> path = root;                    if (Attr_json_type.equals (Entry.getkey ())) {Predicates.add (Root.type (). In (Entry.getvalue ())); } else{for (String Pk:entry.getKey (). split ("\ \"))                        {Path = Path.get (PK); }//Create predicate Predicates.add (getfieldpredicate (CB, PATH, Filteropera                    Tors.get (Entry.getkey ()), Entry.getvalue ());            }} return Cb.and (Predicates.toarray (New Predicate[predicates.size ()));                } private predicate Getfieldpredicate (Criteriabuilder CB, path<?> Path, String operator, Object value) {                        if (op_equal.equals (operator)) {if (value instanceof Collection) {                    Return path.in ((Collection) value);                } return Cb.equal (path, value);                        } else if (Op_not.equals (operator)) {if (value instanceof Collection) { list<predicate> predicates = new Arraylist<> ();                        For (Object Val: (Collection) value) {Predicates.add (cb.notequal (Path, Val));                    } return Cb.and (Predicates.toarray (New Predicate[predicates.size ()));                    } else {return cb.notequal (path, value); }} else if (Op_like.equals (operator)) {return cb.like (path.as (string.cl                ), value.tostring ()); } else if (Op_notlike.equals (operator)) {return cb.notlike (path.as (string.class), value                . toString ()); } else if (Op_between.equals (operator)) {iterator<?> iter = (collection<?>                    ) value). iterator ();                    Object val1 = Iter.next (), val2 = Iter.next (); if (val1 instanceof Byte) {return Cb.between (path.as (byte.class), (Byte) val1, (byte) val2); } else if (Val1 instanceof short) {return Cb.between (path.as (Short.class), (Sho                    RT) Val1, (short) val2); } else if (Val1 instanceof Integer) {return Cb.between (path.as (Integer.class),                    (integer) val1, (integer) val2);  } else if (Val1 instanceof Long) {return Cb.between (path.as (Long.class), (Long)                    Val1, (Long) val2); } else if (Val1 instanceof Float) {return Cb.between (path.as (Float.class), (Flo                    at) Val1, (Float) val2); } else if (Val1 instanceof Double) {return Cb.between (path.as (Double.class), (D                    ouble) Val1, (Double) val2);                    } else {return null;     }} else {               try {Method m = null; if (op_gt.equals (operator)) {m = Cb.getclass (). GetMethod ("GreaterThan", Expression.class, Comp                        Arable.class); } else if (Op_ge.equals (operator)) {m = Cb.getclass (). GetMethod ("Greate                        Rthanorequalto ", Expression.class, Comparable.class); } else if (Op_lt.equals (operator)) {m = Cb.getclass (). GetMethod ("Lessth                        An ", Expression.class, Comparable.class); } else if (Op_le.equals (operator)) {m = Cb.getclass (). GetMethod ("Lessth                        Anorequalto ", Expression.class, Comparable.class);                                if (M! = null) {if (value instanceof Byte) { return (predicate) M.invoke (CB, Path.as (Byte.class), (Byte) value); } else if (value instanceof short) {return (predicate) M.invoke                            (CB, Path.as (Short.class), (short) value); } else if (value instanceof Integer) {return (predicate) m.invo                            Ke (CB, path.as (Integer.class), (Integer) value); } else if (value instanceof Long) {return (predicate) M.invoke (                            CB, Path.as (Long.class), (Long) value); } else if (value instanceof Float) {return (predicate) M.invoke                            (CB, Path.as (Float.class), (Float) value); } else if (value instanceof Double) {return (predicate) M.invok E (CB, PATH.AS (Double.class),(Double) value); }}} catch (Exception e) {throw new                    RuntimeException (e);                } return null;                }            }        };    return spec; }

Reference:

Http://docs.spring.io/spring-data/data-jpa/docs/1.0.x/api/org/springframework/data/jpa/domain/Specification.html

Http://docs.oracle.com/javaee/5/api/javax/persistence/package-summary.html

Http://wenku.baidu.com/view/190e385c3b3567ec102d8a40.html?re=view

Spring Data JPA Specification Query

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.