开发者

Hibernate empty string for an equality restriction for PostgreSQL 8.3 and Oracle 11g

Extending on How does hibernate use an empty string for an equality restriction?

I am having a problem about how to check for an empty string between Oracle 11g and PostgreSQL using Hibernate Restriction

Fo开发者_运维百科r my specific application, PostgreSQL saves empty value using 2 single quotes '' while Oracle saves empty value as NULL

The code to check whether the value is empty of not is

 public void AssureNotEmpty() { ...

     //For Oracle 11g    
     valueRestriction = Restrictions.isNotNull(propertyValueAlias);


     //For PostgreSQL
     valueRestriction = Restrictions.and(Restrictions.isNotNull(propertyValueAlias),
                        Restrictions.ne(propertyValueAlias, ""));

Restrictions.ne(propertyValueAlias, "") fails on Oracle when the value is NOT empty because it will be interpreted as value != NULL (which will ALWAYS be false) as Hibernate makes Oracle interprets "" as NULL.

So how can I make the function Database-platform agnostic (because now I have to detect which database is running, PostgreSQL or Oracle)?

Or am I completely misunderstanding something?


Use nullif(val, '') when storing the values to coerce Postgres to behave like Oracle

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜