3

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?

2
  • At least take some time to paste the question here instead of linking to another site. Commented Nov 7, 2012 at 22:11
  • In this case it seems redundant.. Commented Oct 8, 2013 at 13:50

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

3

You can actually do this the following way:

@NamedNativeQueries({
        @NamedNativeQuery(
                name = "countAll",
                query = "select count(*) from MyTable"
        )
})
public class MyTable ...


// execute like this:
((BigInteger) manager.createNamedQuery("countAll").getSingleResult()).intValue();

1 Comment

It was java.math.BigDecimal in my case.
2

I did a work around using createQuery() instead: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html#objectstate-querying

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.