用hibernate @NamedNativeQuery返回一个数值 [英] Returning a numeric value with hibernate @NamedNativeQuery

查看:709
本文介绍了用hibernate @NamedNativeQuery返回一个数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题:

...如果查询只是返回一个数字(即,查询就像'select count(id)where ...)我反对这个错误


$ b org.hibernate.cfg.NotYetImplementedException:纯原生标量查询尚未支持



欲了解更多详情,请参阅: http://atechnicaljourney.wordpress.com/2012/09/30/hibernate-pure-native-scalar-queries-are-not-yet-supported/



我不想拥有一个包装类,特别是不需要额外的表。这样做的正确方法是什么?

解决方案

面对纯原生标量查询尚未支持。我需要运行计数查询的查询名称作为参数,其中一个查询必须是原生SQL。
能够通过创建假实体来克服:

  import javax.persistence.Column; 
import javax.persistence.Entity;
import javax.persistence.Id;

/ **
*简单的包装实体可以解决有限的hibernate纯原生查询
*支持。在HQL查询不可能的情况下
* /
@SuppressWarnings(serial)
@Entity
public class CountDTO extends Number {

@Id
@Column(name =COUNT)
私有长计数;

@Override
public double doubleValue(){
return count.doubleValue();


@Override
public float floatValue(){
return count.floatValue();
}

@Override
public int intValue(){
return count.intValue();


@Override
public long longValue(){
return count.longValue();
}

}

然后我将 resultClass = CountDTO.class

  @NamedNativeQueries({
@NamedNativeQuery =postIdSecurity,
query =select count(...)...,resultClass = CountDTO.class)
})
$ b $

并获得计数:

 ((Number)getEntityManager() .createNamedQuery(qryName)。
setParameter(...)。getSingleResult())。longValue()

积分转到: http:// jdevelopment。 nl / hibernates-pure-native-scalar-queries-supported /#comment-1533


I've this question:

"... if the query just returns a number (i.e., the query is something like ‘select count(id) where…..) I came up against this error

org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported"

For more details see: http://atechnicaljourney.wordpress.com/2012/09/30/hibernate-pure-native-scalar-queries-are-not-yet-supported/

I don't want to have a wrapper class and especially not some extra table. What is the proper way of doing this?

解决方案

Faced "Pure native scalar queries are not yet supported". I need to run count queries having a name of the query as parameter and one of the queries must be native SQL. Was able to overcome by creating fake entity:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * Simple wrapper entity work around for the limited hibernate pure native query
 * support. Where an HQL query is not possible
 */
@SuppressWarnings("serial")
@Entity
public class CountDTO  extends Number {

    @Id
    @Column(name = "COUNT")
    private Long count;

    @Override
    public double doubleValue() {
        return count.doubleValue();
    }

    @Override
    public float floatValue() {
        return count.floatValue();
    }

    @Override
    public int intValue() {
        return count.intValue();
    }

    @Override
    public long longValue() {
        return count.longValue();
    }

}

Then I set resultClass = CountDTO.class

@NamedNativeQueries({
    @NamedNativeQuery (name="postIdSecurity",
            query="select count(...) ...", resultClass = CountDTO.class)
})

And get count:

    ((Number) getEntityManager().createNamedQuery(qryName).
setParameter(...).getSingleResult()).longValue()

Credits go to: http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/#comment-1533

这篇关于用hibernate @NamedNativeQuery返回一个数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆