表Municipio:
@Id
@Basic(optional = false)
@NotNull
@Column(name = "Chave")
private Integer chave;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 40)
@Column(name = "Nome")
private String nome;
@Basic(optional = false)
@NotNull
@Column(name = "Cep")
private int cep;
@JoinColumn(name = "Uf", referencedColumnName = "Chave")
@ManyToOne(optional = false)
private Uf uf;
表Uf:
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2)
@Column(name = "Chave")
private String chave;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "Nome")
private String nome;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "uf")
private Collection<Municipio> municipioCollection;
懒豆:
private Query createQuery(Class type, boolean count, Map<String, Object> filters, List<SortMeta> multiSortMeta) {
CriteriaBuilder criteria = em.getCriteriaBuilder();
CriteriaQuery query = criteria.createQuery((count ? Long.class : type));
Root from = query.from(type);
query.select((count ? criteria.count(from) : from));
List filter = new ArrayList();
for (String filterProperty : filters.keySet()) {
String filterValue = (String) filters.get(filterProperty);
Expression literal = criteria.literal((String) filterValue);
filter.add(criteria.like(from.get(filterProperty), literal));
}
query.where((Predicate[]) filter.toArray(new Predicate[filter.size()]));
if (!count) {
if (multiSortMeta != null) {
List<Order> order = new ArrayList();
for (SortMeta sm : multiSortMeta) {
if (!sm.getSortOrder().equals(SortOrder.DESCENDING)) {
order.add(criteria.asc(from.get(sm.getSortField())));
} else {
order.add(criteria.desc(from.get(sm.getSortField())));
}
}
query.orderBy(order);
}
}
return em.createQuery(query);
}
没有关系的过滤列(Nome)。那是对的!
SQL :
SELECT COUNT(Chave) FROM Municipio WHERE Nome LIKE ?
使用关系(Uf)过滤列。那是错的!
字段名称未出现在 WHERE 子句中。
SQL :
SELECT COUNT(t0.Chave) FROM Municipio t0, Uf t1 WHERE ( LIKE ? AND (t1.Chave = t0.Uf))
生成以下错误:
Internal Exception: java.sql.SQLException: Incorrect syntax near the keyword 'LIKE'.
我该怎么办?