suhas Patel
suhas Patel

Reputation: 3

Hibernate storing empty string creates white space in DB.

i am using sybase db and hibernate as ORM tool, I am dealing with Person object, which contains name as String variable, i have used varchar datatype in db side when i create object of Person as

Person p=new Person();
p.setName(null);    
session.saveOrUpdate(p); 

this works fine

but when i put empty string in name it creates white space in db.

Person p=new Person();
p.setName("");
session.saveOrUpdate(p);

why this is happening ?

Any advice would be greatly appreciated..

Upvotes: 0

Views: 2169

Answers (2)

swapy
swapy

Reputation: 1616

Its the database padding that adds the white space and nothing to do with hibernate.

Refering to

Manual link,

"Adaptive Server truncates entries to the specified column length without warning or error, unless you set string_rtruncation on. See the set command in the Reference Manual for more information. The empty string, ""or '', is stored as a single space rather than as NULL. Thus, "abc" + "" + "def" is equivalent to "abc def", not to "abcdef"."

Two things you could do:

  • have your application explicitly insert NULL (which is kind of what it should do since the empty string is not equivalent to NULL)
  • put an insert/update trigger on that table (or tables) which intercepts the empty string and converts it to NULL

Upvotes: 4

Karesh A
Karesh A

Reputation: 1751

Empty is a string value and hibernate think like it is a string with a value empty So it will create it. You can check "".equals(..) that means java treats "" as a value.

Upvotes: 1

Related Questions