面临“尚不支持纯本机标量查询”。我需要运行以查询名称作为参数的计数查询,其中一个查询必须是本机 SQL。能够通过创建假实体来克服:
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();
}
}
然后我设置resultClass = CountDTO.class
@NamedNativeQueries({
@NamedNativeQuery (name="postIdSecurity",
query="select count(...) ...", resultClass = CountDTO.class)
})
并得到计数:
((Number) getEntityManager().createNamedQuery(qryName).
setParameter(...).getSingleResult()).longValue()
学分转到:http: //jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/#comment-1533