Hibernate Native SQL Mapping to Entity with Selection Example

做~自己de王妃 提交于 2019-12-01 11:47:53

问题


I tried to create native sql query and map to entity class with parameter but fail.

/**
     * @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;
    }

Please provide an example. Thanks.


回答1:


You can try the below code.

session.createSQLQuery("select * from pool_rule_mapping")
 .addScalar("ID", Hibernate.LONG)
 .addScalar("NAME", Hibernate.STRING)
 .addScalar("SOMEDATE", Hibernate.DATE)
 .addEntity(PoolRuleMapping.class);

Also, you can try specifying alias for individual fields.

select prm.id AS ID, prm.name AS NAME, prm.somedate as SOMEDATE from pool_rule_mapping prm


来源:https://stackoverflow.com/questions/15150739/hibernate-native-sql-mapping-to-entity-with-selection-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!