欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate使用Native SQL报Column not found 博客分类: Java  

程序员文章站 2024-03-17 09:09:28
...

Map<String, Object> params = new HashMap<String, Object>();

        params.put("startDate", startDate);

        params.put("endDate", endDate);

        StringBuilder sql = new StringBuilder();

        sql.append("SELECT s.id as {ss.id}, ");

        sql.append("s.report_date as {ss.reportDate},");

        sql.append("SUM(s.new_today_count) as {ss.newTodayCount},");

        sql.append("SUM(s.new_last_seven_days_count) as {ss.newLastSevenDaysCount},");

        sql.append("SUM(s.new_last_thirty_days_count) as {ss.newLastThirtyDaysCount},");

        sql.append("SUM(s.total_count) as {ss.totalCount}, ");

        sql.append("s.created_date as {ss.createdDate} ");

        sql.append("FROM stat_hq_store_daily s ");

        sql.append("where s.report_date BETWEEN :startDate AND :endDate ");

        if (provinceId > 0) {

            sql.append("AND province_id = :provinceId ");

            params.put("provinceId", provinceId);

        }

        if (cityId > 0) {

            sql.append("AND city_id = :cityId ");

            params.put("cityId", cityId);

        }

        if (districtId > 0) {

            sql.append("AND district_id = :districtId ");

            params.put("districtId", districtId);

        }

        sql.append("GROUP BY s.report_date");

        Map<String, Class> classes = new HashMap<String, Class>();

        classes.put("ss", StatHqStoreDaily.class);

        return (List<StatHqStoreDaily>)sqlQuery(sql.toString(), params, classes);

 

OR

 

String sql=”select cs.id as {c.id},cs.name as {c.name},cs.age as {c.age} from customer cs where cs.id=’1’ ”;
Query query=session.createSQLQuery(sql,”c”,Customer.class);