Chinese garbled characters in spring + hibernate hql statements converted by hibernet

Source: Internet
Author: User
  1. Package Cait. CNAs. Dao. impl;
  2. Import java. util. List;
  3. Import org. hibernate. query;
  4. Import org. springframework. Dao. dataaccessexception;
  5. Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
  6. Import Cait. CNAs. Bo. setapplytype;
  7. Import Cait. CNAs. Dao. isetapplytypedao;
  8. /**
  9. * @ Author linlw
  10. *
  11. */
  12. Public class setapplytypedaoimpl extends hibernatedaosupport implements isetapplytypedao {
  13. /*
  14. * Schedule the process number as planned
  15. */
  16. Public String findtypename (string type ){
  17. List resultlist;
  18. String typename = "";
  19. String strhql = "select P from setapplytype P where P. name like: Name"; // execute ③.
  20. String strhql1 = "select P from setapplytype P where P. name like? "; // Execute ① respectively.
  21. String strhql2 = "select P from setapplytype P where P. name like" + type; // use the hql concatenation method to execute ②. Although no exception is thrown,
  22. // But resultlist. Size () = 0
  23. Try {
  24. Resultlist = This. gethibernatetemplate (). Find (strhql, type); // ①
  25. Resultlist = This. gethibernatetemplate (). Find (strhql2); // ②
  26. Query query = This. gethibernatetemplate (). getsessionfactory (). opensession (). createquery (strhql); // ③
  27. Query. setstring ("name", type );
  28. Resultlist = query. List ();
  29. If (resultlist. Size ()> 0 ){
  30. Setapplytype = (setapplytype) resultlist. Get (0 );
  31. Typename = setapplytype. getcode ();
  32. }
  33. } Catch (dataaccessexception t ){
  34. T. printstacktrace ();
  35. Throw T;
  36. }
  37. Return typename;
  38. }
  39. }

Reply to the post posted on the Internet:

 

There is a bug called the SQL injection vulnerability.
That is to say, the concatenated SQL you wrote.

 

 

Solution 1) If you want to write hql to query Chinese characters, the Chinese parameters must be provided to the query in the form of parameters;
The reason seems to be: the level of Chinese placeholder is a problem.
You can solve this problem by binding parameters. Use a placeholder and setstring. Do not write Chinese directly in hql. That is, it corresponds to ③ above.

2) The problem of anlr is solved temporarily.
Set the hibernate. query. factory_class attribute to org. hibernate. hql. Classic. classicquerytranslatorfactory. Yes, but you cannot use the batch update or delete function (you do not know whether the update or delete function is correct or not. It is to be checked ).

 

As follows:

  1. <Bean id = "sessionfactory"
  2. Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
  3. <Property name = "datasource">
  4. <Ref bean = "datasource"/>
  5. </Property>
  6. <Property name = "hibernateproperties">
  7. <Props>
  8. <Prop key = "hibernate. dialect"> org. hibernate. dialect. sqlserverdialect </prop>
  9. <Prop key = "hibernate. show_ SQL"> true </prop>
  10. <Prop key = "hibernate. query. factory_class"> org. hibernate. hql. Classic. classicquerytranslatorfactory </prop>
  11. </Props>
  12. </Property>
  13. <Property name = "mappingresources">
  14. <List>
  15. <Value> Cait/CNAs/bo/busiuserop. HBM. xml </value>
  16. <Value> Cait/CNAs/bo/appperson. HBM. xml </value>
  17. <Value> Cait/CNAs/bo/orgbaseinfo. HBM. xml </value>
  18. <Value> Cait/CNAs/bo/pnumber. HBM. xml </value>
  19. </List>
  20. </Property>
  21. </Bean>

3) again, the problem is solved. Previously, Hibernate was used as Hibernate-version: 3.1.2, and hibernate-version: 3.0.5 was used this time. Change the package to 3.1.2! This is a bug in hibernate3.02-3.05. The solution is to upgrade to hibernate3.1 RC2! You only need to replace the file!

Yes. I had this problem with hibernate3.05. Later I downloaded the tar.gz package of hibernate3.1 RC2 from www.hibernate.org. replace the hibernate3.jar in this package. I use all other dependent packages in spring1.2.4. if you can't do this, you can't do it, but I can do it!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.