Hibernate:如何为非实体Sql查询别名列?

所有,

有谁知道如何指导SqlQuery类为非托管实体查询返回的列使用别名?

我正在尝试使用SqlQuery Hibernate类来创建非实体POJO的List,但是在尝试对SQL查询中的列进行别名时遇到了麻烦.当我尝试将别名放入我的sql中时(例如SELECT o.id as orderId,ps.code as prescriptionStatus …),hibernate抱怨它找不到列“x”,其中列“x”是非别名列名(例如“id”而不是“orderId”).

如果我没有别名我的返回列,一切都很好,但我的POJO然后被迫拥有带有非别名字段名称的属性,或者我必须管理POJO中的映射(具有漂亮名称的getter返回非别名字段名称).

这是我的代码

//TODO:  change builder to a name query --jg

    StringBuilder sql = new StringBuilder();

    sql.append("SELECT o.id,\n");
    sql.append("       pet.name,\n");
    sql.append("       o.order_date,\n");
    sql.append("       rx_view.prescription_id,\n");
    sql.append("       rx_view.code\n");
    sql.append("FROM   order_line_item oli\n");
    sql.append("       JOIN order_detail o\n");
    sql.append("         ON o.id = oli.order_id\n");
    sql.append("       JOIN order_line_item_pet olip\n");
    sql.append("         ON olip.order_line_item_id = oli.id\n");
    sql.append("       JOIN pet\n");
    sql.append("         ON pet.id = olip.pet_id\n");
    sql.append("       LEFT JOIN (SELECT olip.order_line_item_id order_line_item_id,\n");
    sql.append("                         olip.prescription_id,\n");
    sql.append("                         ps.code\n");
    sql.append("                  FROM   prescription_order_line_item olip\n");
    sql.append("                         JOIN prescription p\n");
    sql.append("                           ON olip.prescription_id = p.id\n");
    sql.append("                         JOIN prescription_status ps\n");
    sql.append("                           ON p.status_id = ps.id) rx_view\n");
    sql.append("         ON rx_view.order_line_item_id = oli.id\n");
    sql.append("WHERE  oli.order_id IN (SELECT o.id\n");
    sql.append("                        FROM   order_detail o\n");
    sql.append("                               JOIN order_line_item oli\n");
    sql.append("                                 ON o.id = oli.order_id\n");
    sql.append("                               JOIN prescription_order_line_item poli\n");
    sql.append("                                 ON oli.id = poli.order_line_item_id\n");
    sql.append("                               JOIN prescription rx\n");
    sql.append("                                 ON rx.id = poli.prescription_id\n");
    sql.append("                        WHERE  rx.id = :prescriptionId)\n");



    SQLQuery query = baseDao.getSession().createSQLQuery(sql.toString());

    query.setLong("prescriptionId", prescriptionId);


    query.setResultTransformer(Transformers.aliasToBean(RelatedPrescriptionOrderLine.class));

    List results = query.list();

    return results;

谢谢!

您可以使用 addScalar(String columnAlias, Type type)显式别名本机SQL的列:

例如 :

SQLQuery query = baseDao.getSession().createSQLQuery("SELECT o.id as orderId, ps.code as prescriptionStatus...");
 query.setLong("prescriptionId", prescriptionId);
 query.addScalar("orderId",StandardBasicTypes.INTEGER )
 query.addScalar("prescriptionStatus",StandardBasicTypes.STRING )
 query.setResultTransformer(Transformers.aliasToBean(RelatedPrescriptionOrderLine.class))

 List results = query.list();

 return results;

然后变换器将使用名为setPrescriptionId(),setPrescriptionStatus()等的setter查找名为RelatedPrescriptionOrderLine的类,并通过这些setter将结果填充到其实例中.

本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院