从Java对象中的存储过程检索结果

时间:2017-12-13 10:05:23

标签: java hibernate stored-procedures

我有一个存储过程,它返回4个字段的结果:longitude numericlatitude numericcoor_xy geometry(Point,2154)message character varying。我使用PostgresSQL作为SGBD。

在Java中,我可以检索结果,创建了一个包含4个属性的类ResultCalcul

public class ResultatCalcul {

    private BigDecimal longitude;
    private BigDecimal latitude;
    private Point coordonnees;
    private String message;

    public BigDecimal getLongitude() {
        return longitude;
    }

    public void setLongitude(BigDecimal longitude) {
        this.longitude = longitude;
    }

    public BigDecimal getLatitude() {
        return latitude;
    }

    public void setLatitude(BigDecimal latitude) {
        this.latitude = latitude;
    }

    public Point getCoordonnees() {
        return coordonnees;
    }

    public void setCoordonnees(Point coordonnees) {
        this.coordonnees = coordonnees;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

使用存储过程的函数如下:

public ResultatCalcul extrapolerPosition(SuiviMissionType messageSMission){
    String sqlString = "select longitude as longitude, latitude as latitude, coor_xy as coor, message as message "
            + "from pk_soclelocalisation_enrichi.fu_extrapoler_position(?,?,?,?)";
    Query query = em.createNativeQuery(sqlString);
    org.hibernate.SQLQuery hq = (org.hibernate.SQLQuery) ((org.hibernate.ejb.HibernateQuery) query).getHibernateQuery();
    hq.setString(0, (messageSMission.getNumeroTrain() != null)? messageSMission.getNumeroTrain():"");
    hq.setString(1, messageSMission.getGarePrecedente().getCodeTT20());
    hq.setString(2, (messageSMission.getGareSuivante() != null)?messageSMission.getGareSuivante().getCodeTT20():"");
    hq.setDouble(3, messageSMission.getGarePrecedente().getDistanceGare());
    hq.addScalar("longitude", new BigDecimalType());
    hq.addScalar("latitude", new BigDecimalType());
    hq.addScalar("coor", new GeometryType().INSTANCE);
    hq.addScalar("message", new StringType());
    ResultatCalcul resultat = (ResultatCalcul) hq.uniqueResult();
    return resultat;
}

当我在调试中执行时,行java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to MyPackage.ResultatCalcul

中出现错误ResultatCalcul resultat = (ResultatCalcul) hq.uniqueResult();

我该如何解决?

0 个答案:

没有答案