【发布时间】:2015-06-29 12:03:50
【问题描述】:
我正在开发一个 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,但它也应该注意它只是检索特定用户的私有 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();
}
我创建了这样的查询,但我没有返回任何数据。
【问题讨论】:
标签: java spring hibernate postgresql spring-mvc