gpt4 book ai didi

java - hibernate : Using AND combined with OR in a single query

转载 作者:行者123 更新时间:2023-12-01 11:22:03 25 4
gpt4 key购买 nike

我正在开发一个 Spring-MVC 项目,我需要使用给定的参数搜索一些信息。这是我当前的 HQL 查询,它很可能无法获取我需要的数据。 :

 org.hibernate.Query query = session.createQuery("From GroupNotes as " +
"n where n.ownednotes.msectionid=:msectionid and n.noteDisabled=false and n.noteInActive=false order by n.mnoteorder");
query.setParameter("msectionid", msectionid);

Query hiddenNotesQuery = session.createQuery("from GroupNotes as gn where gn.ownednotes.msectionid=:msectionid and gn.privateNoteUser=:username");
hiddenNotesQuery.setParameter("msectionid", msectionid);
hiddenNotesQuery.setParameter("username",person.getUsername());

List<GroupNotes> groupNotesList = new ArrayList<>();

groupNotesList.addAll(query.list());
groupNotesList.addAll(hiddenNotesQuery.list());

我想要这样的东西:

from GroupNotes as n where (n.ownednotes.msectionid=:msectionid and n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:"") or (n.ownednotes.msectionid=:msectionid and n.privateNoteUser=:username n.noteDisabled=false and n.noteInActive=false) 

我希望我正在寻找的查询有意义,所以基本上它应该检索未禁用且未激活的 GroupNotes,但也应该注意它只是检索特定用户的私有(private) GroupNotes,因此在第一个括号是 privateNoteUser 作为“”。我不知道那里应该有什么。

如有任何疑问,请告诉我。我怎样才能形成这个查询。

更新:

@Override
public List<GroupNotes> listGroupNotesBySectionId(int msectionid) {
Person person = this.personService.getCurrentlyAuthenticatedUser();
Session session = this.sessionFactory.getCurrentSession();
org.hibernate.Query query = session.createQuery("From GroupNotes as " +
"n where n.ownednotes.msectionid=:msectionid and ((n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:blank) or (n.privateNoteUser=:username and n.noteDisabled=false and n.noteInActive=false)) order by n.mnoteorder");
query.setParameter("msectionid", msectionid);
query.setParameter("msectionid", msectionid);
query.setParameter("username",person.getUsername());
query.setParameter("blank",null);
List<GroupNotes> groupNoteses = query.list();

}

我像这样创建了查询,但没有返回任何数据。

最佳答案

from GroupNotes as n where n.ownednotes.msectionid=:msectionid  
and (
( n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:"")
or
( n.privateNoteUser=:username n.noteDisabled=false and n.noteInActive=false)
)

关于java - hibernate : Using AND combined with OR in a single query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31115220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com