Hibernate存储过程结果问题

时间:2011-08-24 22:33:46

标签: hibernate stored-procedures

我正在尝试使用Hibernate调用存储过程,但我一直得到以下异常:

org.postgresql.util.PSQLException: The column name plan_id was not found in this ResultSet.

存储过程作为辅助数据库对象添加到一个hbm.xml中(实际上这是我唯一的XML映射文件,其他一切都是通过注释完成的):

<hibernate-mapping>
<database-object>
    <create>
CREATE OR REPLACE FUNCTION copy_plan(p_plan_id bigint, p_plan_for_year varchar, p_copy_pp boolean)
RETURNS REFCURSOR
AS $$
DECLARE
    result REFCURSOR;
    // some more declarations
BEGIN
    // a lot of logic which works fine

    OPEN result FOR 
    SELECT  plans.plan_id as plan_id,
            plans.plan_for_year as plan_for_year, 
            plans.plan_plan_id as plan_plan_id, 
            plans.plan_f_type as plan_f_type, 
            plans.plan_enabled as plan_enabled
    FROM rgt_plans plans WHERE plans.plan_id = v_plan_id;

    RETURN result;

END;
$$ LANGUAGE PLPGSQL
    </create>
    <drop>DROP FUNCTION copy_plan</drop>
</database-object>

实体类:

@Entity
@Table(name = "rgt_plans")
@NamedNativeQueries({ @NamedNativeQuery(name = "copyPlan", query = "select copy_plan(:planId, :newYear, :copy)", resultClass=Plan.class) })
public class Plan implements Serializable {

private static final long serialVersionUID = -8448579977833577641L;

@Id
@GeneratedValue(generator = "IdGenerator", strategy = GenerationType.TABLE)
@TableGenerator(name = "IdGenerator", pkColumnValue = "rgt_plans", table = "Sequence_Table", allocationSize = 1)
@Column(name = "plan_id", columnDefinition = "int4")
private Long id;

@Column(name = "plan_for_year")
@NotNull
private String forYear;

@Column(name = "plan_plan_id")
@Basic
private Long plan_id;

@Column(name = "plan_f_type", length = 1)
@Basic
@NotNull
private String type;

@Column(name = "plan_enabled")
@Basic
@NotNull
private Boolean enabled;

// getters, setters, equals, hashcode

}

这就是我如何从我的DAO中调用它:

public List<Plan> copyPlan(Long currentPlanId, String newYear, Boolean copy) {
    return this.getHibernateTemplate()
            .findByNamedQueryAndNamedParam("copyPlan",
                    new String[] { "planId", "newYear", "copy" },
                    new Object[] { currentPlanId, newYear, copy });

}

正如你所看到我正在使用PostgreSQL 8.任何想法为什么它不起作用?我的意思是,即使有标签,我也会从那张桌子上归还所有东西......

1 个答案:

答案 0 :(得分:2)

经过大量谷歌搜索和诅咒后,我开始运行。

我不得不将@NamedNativeQuery更改为:

@NamedNativeQueries({ @NamedNativeQuery(name = "copyPlan", query = "{? = call copy_plan(:planId, :newYear, :copy)}", resultClass = Plan.class, hints = { @QueryHint(value = "true", name = "org.hibernate.callable") }) })

所以我添加了@QueryHint并更改了查询本身。

我还必须在DAO层的方法中询问@Transactional。