我尝试创建本机sql查询并使用参数映射到实体类但失败。
/**
* @return
*/
public List<PoolRuleMapping> getAllPRM() {
Session session = null;
List<PoolRuleMapping> prmList = null;
try {
session = HibernateSessionFactory.getSession();
Query q = session.createSQLQuery("select * from pool_rule_mapping").addEntity(PoolRuleMapping.class);
prmList = q.list();
} catch (HibernateException e) {
} finally {
session.close();
}
return prmList;
}
请举例说明。谢谢。
答案 0 :(得分:1)
您可以尝试以下代码。
session.createSQLQuery("select * from pool_rule_mapping")
.addScalar("ID", Hibernate.LONG)
.addScalar("NAME", Hibernate.STRING)
.addScalar("SOMEDATE", Hibernate.DATE)
.addEntity(PoolRuleMapping.class);
此外,您可以尝试为各个字段指定别名。
select prm.id AS ID, prm.name AS NAME, prm.somedate as SOMEDATE from pool_rule_mapping prm