Hibernate / JPA - 变量字段名称是@NamedQuery

时间:2013-12-10 00:46:55

标签: java hibernate jpa hql

是否可以执行以下操作:

@NamedQuery(name=Report.RECORD_BY_TYPE_AND_FIELD_VALUE,query="SELECT r FROM Report r WHERE r.:field <=:value")

我需要能够根据动态列名进行查询。

1 个答案:

答案 0 :(得分:1)

我认为不可能做到这一点,但Eclipse在其自动生成的DAO中生成以下类型的方法

public List<Table> findByProperty(String propertyName, final Object value) {
    EntityManagerHelper.log("finding TableName instance with property: "
            + propertyName + ", value: " + value, Level.INFO, null);
    try {
        final String queryString = "select model from TableName model where model."
                + propertyName + "= :propertyValue";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("propertyValue", value);
        return query.getResultList();
    } catch (RuntimeException re) {
        EntityManagerHelper.log("find by property name failed",
                Level.SEVERE, re);
        throw re;
    }
}