Preparedstatement: Oracle char type query Solution Using hibernate

Source: Internet
Author: User

Run the following SQL statement in Oracle:
Create Table (
Id char (5)
);
Insert into a values ('11 ');

Use the following Java code to query this record

String SQL = "select * from a where id =? ";
PSTM = conn. preparestatement (SQL );
PSTM. setstring (1, "11 ");
Rsw.p;.exe cutequery ();
System. Out. println (Rs. Next ());

Haha... can't you find anything? Please change the SQL to see:

String SQL = "select * from a where trim (ID) =? ";

Did you find it !, But can we see it without trim?
That's okay!
See the Code:

String SQL = "select * from a where id =? ";
PSTM = conn. preparestatement (SQL );
(Oraclepreparedstatement) PSTM). setfixedchar (1, "11 ");
Rsw.ps.exe cutequery ();
System. Out. println (Rs. Next ());

Run the command to check whether the query is successful!
Why? Because my English level is poor, please open the following link to see it!
Http://forums.oracle.com/forums/thread.jspa;jsessionid=8d92200630de527b2f61c7ef4d3296318c855aa88fe1.e34Tb34Lb34PbO0Lb3eTahiPbNyTe0? Messageid = 504702

It can be said that it is a big trouble to query the Oracle char type in hibernate. Because hibernate uses preparestatement internally, the above problems will occur when querying the Oracle char type, my solution is to define a usertype, which automatically fills in spaces for field values. The following is an example of my configuration file:

<ID name = "ID">
<Column name = "ID"/>
<Type name = "test. oraclechartype">
<Param name = "length"> 5 </param>
</Type>
<Generator class = "assigned"/>
</ID>

Test. oraclechartype is my custom usertype class. It automatically adds spaces when the passed id value is less than five characters, so that the id value can satisfy five characters.
In this way, we can get the object by using the following statement.

Session. Get (A. class, "11 ")

Its underlying query statements are converted
Select * from a where id = '11 ';

How about it? I see no. you may say why do you use spaces instead of the TRIM () function to intercept the fields, because the hibernate usertype can only be used for field values (for example: '11' is changed to '11 ').
In this solution, you only need to define a usertype and then configure it in the HBM file.
Of course, this is only one of the solutions. If you don't bother with it, you can change all the queries to the hql + trim () method, which will increase the workload.
You can also add a <SQL-query> </SQL-query> to each PO object. However, this method has never been tried and is feasible!

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.