Hibernate onetoone hql Multi-table query

Source: Internet
Author: User

Operators yesterday to ask a question, the table's fuzzy query only support the current page, the query needs a page page of the turn, the user experience needs to be improved. The
technical level is because the query invokes the public pagination Pagequery (final criterion[] criterions, final integer startLine, final integer PageS ize, Final String by, final Boolean Isasc) {} method, which is good for single table query support, adding query conditions to criterion array, executing pagequery can make various combination queries, However, there is no excessive description of multiple table queries.
Using the Jd-gui decompile tool, I found the public pagination pagequery (final String hql, final integer startLine, final integer pageSize) {} method, Can dynamically assemble HQL query statements, although troublesome, but the flexibility is relatively good. So I refactor the original code and use HQL to implement the multiple table query.
We have a master table app that has an ID, a name two fields, a appextend from the table, an ID, appId, phoneurl three fields. What we want to achieve is appextend through the AppID Field Association app table, fuzzy Query app Name field, and Appextend phoneurl field is not empty.
First we use the Onetoone Declaration and app relationship in Appextend:

@Entity (name = "V4_app_extend")
@Data the public
class Appextend extends Basepojo {

    @Id
    @GeneratedValue ( Generator= "ididentity")
    @GenericGenerator (name= "ididentity", strategy= "identity")
    private Integer ID;

    @Column (name = "app_id")
    private Integer appId;

    @Column (name = "Phone_url")
    private String phoneurl;

    @OneToOne
    @JoinColumn (name = "app_id", referencedcolumnname = "id", nullable = True, Insertable = false, updatable = False)
    private app app;

}

Joincolumn's name refers to the Appextend foreign key, referencedcolumnname refers to the primary key associated with the app.
Here we demonstrate how to assemble the HQL code as follows:

    @RequestMapping (value = "/getappextend", produces = "text/plain;charset=utf-8") @ResponseBody public String ge
        Tappextend (HttpServletRequest request) throws Exception {int recordstotal = 0;
        Pagrequestinfo info = paginfoutil.getpaginfo1 (request);
        String hql = null; if (Stringutils.isnotblank (Info.getkeyword ())) {hql = "from Com.letv.gc.opman.mutilplatform.pojo.AppExtend ex  Tend left join Extend.app app where app.name like '% ' + info.getkeyword () + '% ' and Extend.phoneurl!=
        ''"; else {hql = "from Com.letv.gc.opman.mutilplatform.pojo.AppExtend extend left join Extend.app app where exte
        Nd.phoneurl!= ' "; } pagination P = appextendmanager.pagequery (HQL, Integer.parseint (Info.getstart ()), Integer.parse

        Int (Info.getlength ()));
        Recordstotal = P.gettotalcount ();

        Jsonarray Jsonarray = new Jsonarray (); for (int index = 0; index < p.getreCords (). Size ();
            index++) {object[] objects = (object[)) p.getrecords (). get (index);
            Appextend appextend = (appextend) objects[0];
            App app = Appmanager.finduniqueby ("id", appextend.getappid ());
        Jsonarray.add (Jsonobject.fromobject (APP));
    Return Paginfoutil.returnpageresult (Jsonarray, Recordstotal, Info.getflag ()); }

Because the return value of P.getrecords (). Get (Index) contains Appextend and app two objects, so we use the object[] array to take a strong turn and then obtain appextend.
in the above explanation, we use annotation onetoone and joincolumn to declare the correlation field, dynamically assemble HQL according to the business need, realize the multiple table query function, improve the user experience degree.
want to share to help everyone.

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.