Hibernate @Where子句 [英] Hibernate @Where clause

查看:1138
本文介绍了Hibernate @Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Where(clause =enabled = 1 and deleted = 0 )
@Fetch(FetchMode.SUBSELECT)
public Set< Group> getGroups(){
返回组;
}

但是当我试图在 Group ,它已经存储在数据库中,不适合 @Where 子句,Hibernate忽略了我。



实际上, @Where 不是我的决定,但是我可以找到解决方法来解决这个问题(创建另一个dao方法来执行一些操作)非常难看。

那么,是否有办法克服这个问题?或者我错过了什么?感谢您的帮助。

解决方案

根据您的类型启用删除字段,您必须以不同的形式编写 @Where 子句。



如果您的字段是数字:

  @Where(clause =enabled = 1 and deleted = 0 )

如果您的字段是字符串:

< pre $ @Where(clause =enabled ='1'and deleted ='0')

如果您的字段是布尔型的:

  @Where(clause =enabled = true and deleted = false)

检查您的类型(在您的Java类和数据库中) @Where 以正确的方式。


I have User class, which has groups.

    @Where(clause = "enabled = 1 and deleted = 0")
    @Fetch(FetchMode.SUBSELECT)
    public Set<Group> getGroups() {
        return groups;
    }

But when I'm trying to make some operation on Group which is already stored in DB and is not suitable for @Where clause, Hibernate just ignores me.

Actually, @Where is not my decision, but solution I could find to overcome this (create another dao method to perform some operation) is so ugly.

So, is there way to overcome this? Or did I miss something? Thanks for the help.

解决方案

Depending on the type of your enabled and deleted fields, you must write your @Where clause in a different form.

If your fields are numerics:

@Where(clause = "enabled = 1 and deleted = 0")

If your fields are Strings:

@Where(clause = "enabled = '1' and deleted = '0'")

If your fields are boolean:

@Where(clause = "enabled = true and deleted = false")

Check your types (in your Java class and in your database too) and use your @Where in the correct way.

这篇关于Hibernate @Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆