Hibernate 限制(和/或)

发布于 2024-09-06 03:09:47 字数 217 浏览 2 评论 0原文

关于限制的小问题。

我有这样的查询:

Where  A in(1,6) and (B not like 'Value%' or B not like 'ValueII%')

问题是字段 B 不包含固定值,可以是一个或多个。

请 (N)Hibernate 专家帮助我解决这个问题,我找不到可行的解决方案。非常感谢任何帮助!

small questions about Restrictions.

i have query like this :

Where  A in(1,6) and (B not like 'Value%' or B not like 'ValueII%')

the problem is the field B not contains a fixed value, can be one or more.

Please, (N)Hibernate experts - help me with this, I cant find a working solution. Any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-09-13 03:09:47

您可以使用析取来处理 OR 情况,如下所示:

IList results = session.CreateCriteria(typeof(MyType))
    .Add(Restrictions.In("A", new [] {1, 6}))
    .Add(Restrictions.Disjunction()
        .Add.Restrictions.Not(Restrictions.Like("B", "Value%"))
        .Add.Restrictions.Not(Restrictions.Like("B", "ValueII%")))
    .List<MyType>();

如果您需要在 for 循环中处理析取条件(根据您的后续操作),您可以使用如下代码:

ICriteria query = session.CreateCriteria(typeof(MyType));
query.Add(Restrictions.In("A", new [] {1, 6}))

Disjunction disjunction = Restrictions.Disjunction();
foreach (var value in values) {
    disjunction.Add(Restrictions.Not(Restrictions.Like(value, "Value%")));
    disjunction.Add(Restrictions.Not(Restrictions.Like(value, "ValueII%")));
}
query.Add(disjunction);

IList results = query.List<MyType>();

话虽这么说,我认为可能有一个你的逻辑有缺陷。如果您测试这些值“不匹配一个模式或不匹配另一模式”,则条件将始终评估为 true(除非模式相同)。您确定要在这里使用 OR 吗?

You can use disjunctions to handle the OR cases, as such:

IList results = session.CreateCriteria(typeof(MyType))
    .Add(Restrictions.In("A", new [] {1, 6}))
    .Add(Restrictions.Disjunction()
        .Add.Restrictions.Not(Restrictions.Like("B", "Value%"))
        .Add.Restrictions.Not(Restrictions.Like("B", "ValueII%")))
    .List<MyType>();

If you need to handle the disjunction conditions in a for loop (per your follow-up), you can use code like this:

ICriteria query = session.CreateCriteria(typeof(MyType));
query.Add(Restrictions.In("A", new [] {1, 6}))

Disjunction disjunction = Restrictions.Disjunction();
foreach (var value in values) {
    disjunction.Add(Restrictions.Not(Restrictions.Like(value, "Value%")));
    disjunction.Add(Restrictions.Not(Restrictions.Like(value, "ValueII%")));
}
query.Add(disjunction);

IList results = query.List<MyType>();

That being said, I think there may be a flaw in your logic. If you are testing these values to "not match one pattern OR not match another pattern", then the condition will always evaluate to true (unless the patterns are the same). Are you sure you want to be using ORs here?

疯狂的代价 2024-09-13 03:09:47

问题是我不知道到底有多少限制,并且我需要在析取中使用 FOR 。

类似这样的:

IList results = session.CreateCriteria(typeof(MyType))
.Add(Restrictions.In("A", new [] {1, 6}))
.Add(Restrictions.Disjunction());
 for(int i = 0, i < value.Lenght;i++){
    .Add.Restrictions.Not(Restrictions.Like("B", "Value%"));
    .Add.Restrictions.Not(Restrictions.Like("B", "ValueII%"));
 }
.List<MyType>();

谢谢你的帮助

the problem is that I don't know exact how many restrictions i have and i need to use FOR in disjunction.

Something like that:

IList results = session.CreateCriteria(typeof(MyType))
.Add(Restrictions.In("A", new [] {1, 6}))
.Add(Restrictions.Disjunction());
 for(int i = 0, i < value.Lenght;i++){
    .Add.Restrictions.Not(Restrictions.Like("B", "Value%"));
    .Add.Restrictions.Not(Restrictions.Like("B", "ValueII%"));
 }
.List<MyType>();

thanks for you help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文