Showing posts with label playframework-2.3. Show all posts
Showing posts with label playframework-2.3. Show all posts

Friday, March 11, 2016

putting condition on a List field using Ebean and Play framework

Leave a Comment

I have A Device class and a Event class like this:

    @Entity     public class Device extends Model {          @Id         public Long id;          @OneToMany(mappedBy = "device")         public List<Event> events = new ArrayList<Event>();          ...     }      @Entity     public class Event extends Model {          @Id         public Long id;          @Constraints.Required         @Formats.DateTime(pattern = "dd-MM-yyyy HH:mm")         public Date start;          @Formats.DateTime(pattern = "dd-MM-yyyy HH:mm")         public Date end;          @ManyToOne         public Device device;          ...      } 

I want to get a list (Page actually) of all devices that have NO events between a given Date from and Date to. well sounds simple but I somehow cannot get what I want using Ebean, this is how I think should work, but the devices I'm getting are not right,

    Model.Finder<Long, Device> find = new Model.Finder<>(Long.class, Device.class);     Page<Device> devicePage = find.where()         .or(             Expr.lt("events.end", from),             Expr.gt("events.start", to)         )         .orderBy("id asc")         .findPagingList(10)         .setFetchAhead(false)         .getPage(0); 

1 Answers

Answers 1

.or(     Expr.and(         Expr.lt("events.start", from),         Expr.lt("events.end", from)     ),     Expr.and(         Expr.gt("events.start", to),         Expr.gt("events.end", to)     ) ) 

The idea is: event's start & end date have to be less than FROM date OR have to be greater than END date.

Read More