How to prevent hibernate from trimming strings?

I am using Oracle 10g and Hibernate 2.1 (old version, but not allowed to upgrade to fix). I have a table with a column with a null value named TIN, varchar2 (9). This column is used to process data loaded from flat files, and therefore can store any line with a length of 9, including 9 spaces (i.e.: '') if the input file had 9 spaces.

I noticed that:

  • Oracle 10g automatically converts empty strings to NULL. Therefore, if you run:

    SELECT NVL('', 'Input string converted to NULL') FROM dual; 
    

    result: "Input string converted to NULL", not ''. I think this relates to the problem.

  • When Hibernate reads a record where the TIN value is 9 spaces (or any number of spaces) without another character, it stores the value in memory as'. Hibernate then seems to be fooling itself into thinking, so to speak, that the meaning has changed from 9 spaces to an empty string. Then, if the record is written back to the database, Hibernate tries to write an empty string, not 9 spaces, and Oracle apparently converts that value to null and then produces an inviscid constraint constraint.

For reference, here is the HBM for this column:

<property
    name="tin"
    type="java.lang.String"
    column="TIN"
    not-null="true"
    length="9">

My question is, how can I make Hibernate not convert values ​​containing only spaces for empty strings?

1: , "", , Hibernate , "" , , . - . .

2: , hbm2java Ant, HBM Java, String. .

,

: .

+3
3

, Hibernate. , , Java POJO HBM hbm2java Ant. , , getters/setters/equals/toString/etc, java.lang.String . , , , , TIN.

+3

- Hibernate UserType. hibernate.org , . , : " hibernate 9 ", .

2 : , ( 9 ), , ''. , .

- . , , "type" HBM.xml (, add type = "my.fully.qualified.HibernateUserType" ).

Java - java.lang.String , , UserType.

+4

I do not know about sleep mode. But I know that Oracle treats empty strings as Null values. And there is no workaround for this.

+2
source

Source: https://habr.com/ru/post/1704351/


All Articles