Hibernate : Using AND combined with OR in a single query

Question!

I am working on a Spring-MVC project where I am required to search for some information with given parameters. Here is my current HQL query, which most likely wont get me data I require. :

 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());

I would like something like this :

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) 

I hope the query I am looking for makes sense, so basically it should retrieve GroupNotes which are not disabled and not active, but also it should it should pay attention that it is only retrieving Private GroupNotes for the specific user and thus in the first bracker there is privateNoteUser as "". I don't know what I should have there.

If any doubts, kindly let me know. How can I form this query.

Update :

@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();

}

I created the query like this, but I am getting back no data.



Answers
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)
) 
By : Paul John


This video can help you solving your question :)
By: admin