Hibernate multi-Table query Solution

Source: Internet
Author: User

Because the primary key of each table is set at the beginning of the project design, but the foreign key cannot be set because of the project requirements, so there is no foreign key, when you use criteria to create a searcher

Failed. You cannot create a table by using multi-table join. An error is prompted as follows:

Criteria ir_a = This. getsession (). createcriteria (annualverificationfile. Class );
Ir_a.createalias ("housebaseinfo", "housebaseinfo ");

Here, the first housebaseinfo is estimated to be a foreign key factor. It is not generated in my ing file, and I don't know whether to manually modify the class ing relationship. Finally, I came up with another method.

Create a view of the associated table in the database, and then create a filter to retrieve the view.

The method is as follows:

 

Code
Criteria ir_vid =   This . Getsession (). createcriteria (viewannualverificationfile. Class );
Ir_vid.setfirstresult (BI );
Ir_vid.setmaxresults (Limit );

If (Vannu. getdeliverycodename (). intvalue () ! = 0 )
Ir_vid.add (expression. eq ( " Id. deliverycodename " , Vannu. getdeliverycodename ()));
If (Vannu. getyearreportmaturity (). intvalue () ! = 0 )
Ir_vid.add (expression. eq ( " Id. yearreportmaturity " , Vannu. getyearreportmaturity ()));

If ( ! Vannu. getwarehouseno (). Equals ( "" ))
Ir_vid.add (expression. eq ( " Id. warehouseno " , Vannu. getwarehouseno ()));
If (Vannu. getfileclassify (). intvalue () ! = 0 )
Ir_vid.add (expression. eq ( " Id. fileclassify " , Vannu. getfileclassify ()));

List page_query = Ir_vid.list ();
// List page_query = This. gethibernatetemplate (). Find (querystring );
System. Out. println ( " Filter count: " + Page_query.size ());
If (Page_query.size () > 0 )
{
/** * *********************** Test for query result! OK ~~ : O
System. Out. println ("queied! ");
Java. util. iterator = page_query.iterator ();

While (iterator. hasnext ()){
Viewannualverificationfile ur = (viewannualverificationfile) iterator. Next ();
System. Out. println (Ur. GETID (). getwarehouseno () +
"\ T" + ur. GETID (). getfilename () +
"/" + Ur. GETID (). getfileclassify ());
}*/
Return Page_query;
}

Return   Null ;

 

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.