Hibernate 空字符串用于 PostgreSQL 8.3 和 Oracle 11g 的相等限制
我遇到了如何检查 Oracle 11g 和 PostgreSQL 之间的空字符串的问题 使用 Hibernate 限制
对于我的特定应用程序, PostgreSQL 使用 2 个单引号 '' 保存空值 while Oracle 将空值保存为 NULL
检查值是否为空的代码是
public void AssureNotEmpty() { ...
//For Oracle 11g
valueRestriction = Restrictions.isNotNull(propertyValueAlias);
//For PostgreSQL
valueRestriction = Restrictions.and(Restrictions.isNotNull(propertyValueAlias),
Restrictions.ne(propertyValueAlias, ""));
Restrictions.ne(propertyValueAlias, "") 当值不为空时在 Oracle 上失败,因为它将被解释为 value != NULL (这将始终为 false),因为 Hibernate 使 Oracle 将“”解释为 NULL。
那么如何使函数与数据库平台无关(因为现在我必须检测正在运行的是哪个数据库,PostgreSQL 还是 Oracle)?
或者我完全误解了什么?
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
For 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存储值时使用
nullif(val, '')
强制 Postgres 表现得像 OracleUse
nullif(val, '')
when storing the values to coerce Postgres to behave like Oracle