In Oracle, the CHAR field value is automatically filled with spaces. Therefore, when it is used as a condition, you may not be able to find the desired data.
If you cannot change the database design, use RTRIM. Example:
DB:
CREATE TABLETEST
(
TEST_IDVARCHAR2(64)NOT NULL,
TEST_CHARCHAR(10),
CONSTRAINTTEST_IDPRIMARY KEY(TEST_ID)
);
INSERT INTOTESTVALUES('1','A');
Test code:
Session session=Factory. getCurrentSession ();
Session. beginTransaction ();
List list=Session. createQuery ("FROM Test where rtrim (TEST_CHAR) = 'A'"). List ();
Session. close ();
AssertTrue (list. size ()> 0);
Output result (ORACLE ):
Hibernate:
Select
Test0 _. TEST_IDAsTEST1_0 _,
Test0 _. TEST_CHARAsTEST4_0 _
From
TEST test0 _
Where
Rtrim(TEST_CHAR)='A'
14:09:53, 171 DEBUG StringType:172 -Returning'1' As Column: TEST1_0 _
14:09:53, 187 DEBUG StringType:172 -Returning'A' As Column: TEST4_0 _
MySQL does not automatically fill in spaces, and the same code can also be used:
Output result(MySQL ):
Hibernate:
Select
Test0 _. TEST_IDAsTEST1_0 _,
Test0 _. TEST_CHARAsTEST4_0 _
From
TEST test0 _
Where
Rtrim(TEST_CHAR)='A'
14:09:01, 828 DEBUG StringType:172 -Returning'1' As Column: TEST1_0 _
14:09:01, 828 DEBUG StringType:172 -Returning'A' As Column: TEST4_0 _