Result in Spring+hibernate object returned from Hibernatecallback isn ' t a List

Source: Internet
Author: User

Ok the problem is so for Executefind () The return type was list....so there is no-to-use Uniqueresult () within the CA Llback from Executefind () ... May is we should use execute ()

The above words come from http://forum.springframework.org/showthread.php?t=58370

When using the Executefind method, an exception occurs if the data returned is not of the list type

Cases:
Public Integer findcount (final Class clazz,final String str) {
Return Integer.parseint (Gethibernatetemplate (). Executefind(New Hibernatecallback ()
{

@Override
Public Object Doinhibernate (session session)
Throws Hibernateexception, SQLException {
StringBuilder sb=new StringBuilder ();
Sb.append ("SELECT count (*) from");
Sb.append (Clazz.getname ());
Sb.append ("");
Sb.append (str);
Log.info (Sb.tostring ());
Return Session.createquery (Sb.tostring ()). Uniqueresult ();
}

}). ToString ());
}

The workaround is to change the Executefind method to the Execute method

Public Integer findcount (final Class clazz,final String str) {
Return Integer.parseint (Gethibernatetemplate (). Execute(new Hibernatecallback ()
{

@Override
Public Object Doinhibernate (session session)
Throws Hibernateexception, SQLException {
StringBuilder sb=new StringBuilder ();
Sb.append ("SELECT count (*) from");
Sb.append (Clazz.getname ());
Sb.append ("");
Sb.append (str);
Log.info (Sb.tostring ());
Return Session.createquery (Sb.tostring ()). Uniqueresult ();
}

}). ToString ());
}

Result in Spring+hibernate object returned from Hibernatecallback isn ' t a List

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.