Is there also a way to intercept all queries and saves?
... we add the company id to almost every query to limit the load on the database and prevent companies from seeing each others data.
Just adding a BeanQueryAdapter which will allow you to do this.
/** * Return true if this adapter is interested in queries for the given entity * type. */ public boolean isRegisterFor(Class<?> cls); /** * Returns an int to to control the order in which BeanQueryAdapter are * executed when there is multiple of them registered for a given entity * type (class). */ public int getExecutionOrder(); /** * Modify the associated query prior to it being executed. */ public void preQuery(BeanQueryRequest<?> request);
So typically usage would be to register for any/all bean types that you want to adapt the queries for.
public void preQuery(BeanQueryRequest<?> request) { Query<?> query = request.getQuery(); // can get the type of query Bean, List, RowCount, Id's etc //Type queryType = query.getType(); // typically add extra predicates based on the current user // (probably get the current user from a TheadLocal) // ... for this example add a silly 1=1 predicate query.where().raw("1=1"); }
The code to implement this is in head.
Refer to com.avaje.ebean.event.BeanQueryAdapter