如何映射从存储过程返回的自定义表

时间:2016-02-08 18:32:03

标签: java hibernate postgresql

我使用postgresql和hibernate,我有一个返回自定义表的存储过程,我如何将java bean映射到这个自定义表?我写了java类,但我不确定是否应该使用注释。

我的SP:

CREATE OR REPLACE FUNCTION reporte_gas_federal()
  RETURNS TABLE(tipologia text, "Número de clientes" bigint, "Saturación de mercado" text, "Distribución clientes nivel federal" text) AS
$BODY$ 

WITH cruce_tipo_gas
     AS (SELECT wise_ageb.id_ageb,
                wise_ageb.cvgeo_ageb,
                wise_ageb.tipo,
                nombre,tipologias_ageb.clave,
                wise_ageb_geom.geom
         FROM   wise_ageb
                JOIN wise_ageb_geom
                  ON wise_ageb.id_ageb = wise_ageb_geom.id_ageb
                JOIN tipologias_ageb
                  ON tipologias_ageb.id_ageb = wise_ageb.id_ageb
                JOIN tipologias
                  ON tipologias_ageb.clave = tipologias.clave),
     conteo_parcial
     AS (SELECT DISTINCT nombre,
                         Count(nombre) AS conteo
         FROM   gasnatural_clientes
                INNER JOIN cruce_tipo_gas
                        ON St_intersects(cruce_tipo_gas.geom,
                           gasnatural_clientes.geom)
         GROUP  BY( nombre )
         ORDER  BY nombre ASC),
   cruce_manzana
     AS (--obtiene cruce de las manzanas para pobtnener datos de pob
        SELECT id_manzana,
                 clave
         FROM   wise_manzana
                JOIN cruce_tipo_gas using(id_ageb)),
     pob_tipologia --poblacio total x tipologia
     AS (SELECT Sum(Cast(pobtot AS INT))AS poblacion,
                clave
         FROM   wise_manzana_poblacion
                JOIN cruce_manzana using (id_manzana)
         GROUP  BY clave),

     conteo_total_pob --poblacion total en la region
     AS (SELECT Sum(poblacion) AS pobtot
         FROM   pob_tipologia),
     conteo_total
     AS (SELECT Sum(conteo) AS total
         FROM   conteo_parcial)
SELECT
    nombre as "Tipología",conteo as "Número de clientes",
       CONCAT( ROUND(( ( conteo / total ) * 100 ),4),'%') AS    "Saturación de mercado",
       Concat(Round(( Cast(conteo AS DECIMAL) / pobtot ) * 100, 4), '%') AS "Distribución clientes nivel federal"

FROM   conteo_parcial
       CROSS JOIN conteo_total
        CROSS JOIN conteo_total_pob order by conteo desc; 


       $BODY$
  LANGUAGE sql VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION reporte_gas_federal()
  OWNER TO postgres;

1 个答案:

答案 0 :(得分:1)

如果您有RowMapper,则可以使用JdbcTemplate

public CustomResult getCustomResult(){
    final String sql = "CALL reporte_gas_federal";
    final CustomResult customResult = (CustomResult) jdbcTemplate.queryForObject(sql, new CustomResultRowMapper());

    return customResult;
}

public class CustomResultRowMapper implements RowMapper {
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        CustomResult customResult = new CustomResult();
        customResult.set(...
        ...
        return customResult;
    }
}

否则您可以使用ResultRansformer例如:

CustomResult customResult = (CustomResult)session.createSQLQuery("CALL reporte_gas_federal")
    .setResultTransformer(new BasicTransformerAdapter() {
            private static final long   serialVersionUID    = 1L;

            @Override
            public Object transformTuple(Object[] tuple, String[] aliases){
                final CustomResult customResult = new CustomResult();
                CustomResult customResult = new CustomResult();
                customResult.set(...
                ...
                return customResult;
            }
    }).uniqueResult();

甚至是.setResultTransformer(Transformers.aliasToBean(CustomResult.class))而不是自定义转换器。