使用addScala将SQLQuery自定义查询映射到pojo中

SELECT h.*
FROM   Hotel h,
       (SELECT entityId,
               Max(OperationTime)
        FROM   History
        WHERE  managerId = '402881eb3b1ccdcd013b1cce12b80002'
               AND operationType = 11
        GROUP  BY entityId) hi
WHERE  h.id = hi.entityId
       AND h.locationId = '101005001'
----------------------------------------------------------------------
String sql =
    "SELECT h.* " +
    "FROM   Hotel h, " +
           "(SELECT entityId, " +
                   "Max(OperationTime) " +
            "FROM   History " +
            "WHERE  managerId = '402881eb3b1ccdcd013b1cce12b80002' " +
                   "AND operationType = 11 " +
            "GROUP  BY entityId) hi " +
    "WHERE  h.id = hi.entityId " +
           "AND h.locationId = '101005001'";

SQLQuery query = super.getSession().createSQLQuery(sql);
query.addScalar("id", Hibernate.STRING);
query.addScalar("name", Hibernate.STRING);
query.addScalar("englishName", Hibernate.STRING);
query.addScalar("type", Hibernate.INTEGER);
query.addScalar("locationId", Hibernate.STRING);
query.addScalar("characteristic", Hibernate.INTEGER);
query.addScalar("address", Hibernate.STRING);
query.addScalar("description", Hibernate.STRING);
query.addScalar("environment", Hibernate.STRING);
query.addScalar("basicInfo", Hibernate.STRING);
query.addScalar("additional", Hibernate.STRING);
query.addScalar("services", Hibernate.STRING);
query.addScalar("generalFacility", Hibernate.STRING);
query.addScalar("activityFacility", Hibernate.STRING);
query.addScalar("roomFacility", Hibernate.STRING);
query.addScalar("state", Hibernate.INTEGER);

query.setResultTransformer(Transformers.aliasToBean(Hotel.class));
List<Hotel> arr = query.list();
Hotel h = arr.get(0);
System.out.println(h.getName());

你可能感兴趣的:(sql)