我的道课上有这个方法:
public List<E> search(String key, Object value) {
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
List result = entityManager.createQuery("SELECT a FROM "+clazz.getSimpleName()+" a WHERE a."+key+" LIKE '"+value+"%'").getResultList();
entityManager.getTransaction().commit();
entityManager.close();
return result;
}
当属性为@Column
或@ OneToOne`时,sql正常工作,但是当它是类似的时候:
@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@OrderColumn
private List<Titulo> nome;
其中类Titulo
具有此属性:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column
private String idioma;
@Column(length=32)
private String conteudo;
导致此错误:
message: left and right hand sides of a binary logic operator were incompatible [java.util.List(org.loja.model.categoria.Categoria.nome) : string]; nested exception is org.hibernate.TypeMismatchException: left and right hand sides of a binary logic operator were incompatible [java.util.List(org.loja.model.categoria.Categoria.nome) : string]
如何更改使两种类型的属性都起作用的方法?
我设法通过以下方法解决此问题,使用java反射来检测尝试查询的字段的类型,并使用适当的sql命令。不知道这样做有多有效;如果有人对此有更好的解决方案,请随时添加另一个答案。