- Package Cait. CNAs. Dao. impl;
- Import java. util. List;
- Import org. hibernate. query;
- Import org. springframework. Dao. dataaccessexception;
- Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;
- Import Cait. CNAs. Bo. setapplytype;
- Import Cait. CNAs. Dao. isetapplytypedao;
- /**
- * @ Author linlw
- *
- */
- Public class setapplytypedaoimpl extends hibernatedaosupport implements isetapplytypedao {
- /*
- * Schedule the process number as planned
- */
- Public String findtypename (string type ){
- List resultlist;
- String typename = "";
- String strhql = "select P from setapplytype P where P. name like: Name"; // execute ③.
- String strhql1 = "select P from setapplytype P where P. name like? "; // Execute ① respectively.
- String strhql2 = "select P from setapplytype P where P. name like" + type; // use the hql concatenation method to execute ②. Although no exception is thrown,
- // But resultlist. Size () = 0
- Try {
- Resultlist = This. gethibernatetemplate (). Find (strhql, type); // ①
- Resultlist = This. gethibernatetemplate (). Find (strhql2); // ②
- Query query = This. gethibernatetemplate (). getsessionfactory (). opensession (). createquery (strhql); // ③
- Query. setstring ("name", type );
- Resultlist = query. List ();
- If (resultlist. Size ()> 0 ){
- Setapplytype = (setapplytype) resultlist. Get (0 );
- Typename = setapplytype. getcode ();
- }
- } Catch (dataaccessexception t ){
- T. printstacktrace ();
- Throw T;
- }
- Return typename;
- }
- }
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:
- <Bean id = "sessionfactory"
- Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
- <Property name = "datasource">
- <Ref bean = "datasource"/>
- </Property>
- <Property name = "hibernateproperties">
- <Props>
- <Prop key = "hibernate. dialect"> org. hibernate. dialect. sqlserverdialect </prop>
- <Prop key = "hibernate. show_ SQL"> true </prop>
- <Prop key = "hibernate. query. factory_class"> org. hibernate. hql. Classic. classicquerytranslatorfactory </prop>
- </Props>
- </Property>
- <Property name = "mappingresources">
- <List>
- <Value> Cait/CNAs/bo/busiuserop. HBM. xml </value>
- <Value> Cait/CNAs/bo/appperson. HBM. xml </value>
- <Value> Cait/CNAs/bo/orgbaseinfo. HBM. xml </value>
- <Value> Cait/CNAs/bo/pnumber. HBM. xml </value>
- </List>
- </Property>
- </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!