I need help with querying QueryDSL. I use this library with Spring Data JPA. My class of service:
@Service("tblActivityService")
public class TblActivityService implements AbstractService<TblActivity> {
@Resource
private TblActivityRepository tblActivityRepository;
@Override
public List<TblActivity> findAll(Predicate predicate) {
return (List<TblActivity>) tblActivityRepository.findAll(predicate);
}
}
I have a dynamic filter list:
@Entity
@Table(name = "sys_filters")
public class SysFilter implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "filter_id")
private Integer filterId;
@JoinColumn(name = "user_id", referencedColumnName = "user_id")
@ManyToOne(fetch = FetchType.EAGER)
private SysUser userId;
@Size(max = 45)
@Column(name = "table_name")
private String tableName;
@Size(max = 45)
@Column(name = "column_name")
private String columnName;
@Size(max = 45)
@Column(name = "condition")
private String condition;
@Size(max = 100)
@Column(name = "value")
private String value;
}
I have a column name (e.g. name) I have a condition (e.g. == ,! =,> = Etc.) - I can store it as characters or words (equal, etc.), AND finally i have value.
The question is how to dynamically generate a predicate for my service? The table contains about 25 fields.
The predicate looks like this:
public BooleanExpression buildFilteredResult(List<SysFilter> filters) {
//TODO do it!
return QTblActivity.tblActivity.title.eq("Value");
// I need to do it dynamically for each filter in the list
}
The problem is how to call columnName by its string value. Do you have any suggestions?
source
share