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!