0

I have this piece of code:

public String getEventsForCalendar(@RequestParam("userId") Long userId){
  Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
    try{
        session.beginTransaction();
        JSONArray eventsArray = new JSONArray((List<Event>)(session.createCriteria(Event.class)
                .add(Restrictions.eq("ownerid", userId))
                .list()));
        User user = (User)session.createCriteria(User.class)
                .add(Restrictions.eq("id", userId)).uniqueResult();
        for (Event event: (List<Event>) session.createCriteria(Event.class)
                .add(Restrictions.ne("ownerid", userId))
                .list()){
            if (event.getInvited().contains(user)){
                eventsArray.put(new JSONObject(event));
            }
        }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
    return null;
 }

It, at first, should find Events created by User. Next, in loop it should get Events which User is invited. Each Events has List called invited and I think I have to iterate over all list and check if User is in invited list, but can I do something like?

List<Event> list = session(...)
    .add(Restrictions.eq("invited", user);

In the other words, can I get from database Object which in list contains object? I will be very happy if anybody answers my question - thank you in advance.

Radek Anuszewski
  • 1,812
  • 8
  • 34
  • 62
  • What you are asking is fundamentally not possible. You can choose to right a query and execute. using hibernate session. :) – dharam May 14 '14 at 18:52

1 Answers1

0

You would use the Restrictions.in("colName", collection) method.

Edit: If you want to do the inverse. If column is not in the collection then you can do:

Restrictions.not(Restrictions.in("colName", collection))

See http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/criterion/Restrictions.html#in(java.lang.String, java.util.Collection)

Mark Carpenter
  • 433
  • 3
  • 9
  • Thank you for help, but I have a little question - it checks if collection are equal or if element is in collection? I am asking because it takes collection, not element as argument. – Radek Anuszewski May 14 '14 at 18:51
  • He wants the other way round, I believe :) – dharam May 14 '14 at 18:51