2

Code example with Hibernate which evaluate possible parameters:

  String query = "SELECT * FROM instances";
  String where = "";
  if(userName!=null) {
    where+="AND username = '" + userName + "'";
  }
  if(componentName!=null) {
    where+="AND componentname = '" + componentName + "'";
  }
  if(componentAlias!=null) {
    where+="AND componentalias = '" + componentAlias + "'";
  }
  if(!where.equalsIgnoreCase("")) {
    where = where.substring(3);
    where = " WHERE " + where;
  }
  query = query + where;
  LOGGER.info("Query: " + query);
 
  Statement s = (Statement) conn.createStatement();
  ResultSet rs = s.executeQuery(query);

How can I do that with Speedment ORM Filters?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Manuel
  • 205
  • 1
  • 4
  • 17

1 Answers1

1

It is very similar in Speedment. The Stream interface returns a new Stream every time a filter is added. You can simply store that in a variable and use if-statements as in your code.

Stream stream = instances.stream();
if (userName != null) {
    stream = stream.filter(Instance.USERNAME.equal(userName));
}

if (componentName != null) {
    stream = stream.filter(Instance.USERNAME.equal(componentName));
}

if (componentAlias != null) {
    stream = stream.filter(Instance.USERNAME.equal(componentAlias));
}

List<Instance> result = stream.collect(toList());
Emil Forslund
  • 549
  • 4
  • 12