peter.petrov
peter.petrov

Reputation: 39457

Hibernate - How can I force it programmatically to return Long instead of BigInteger?

I have this method in one of the DAOs of my project.
The underlying RDBMS is MS SQL Server.
The se_subaccountid is defined as bigint on the DB level.

Hibernate returns to me List<BigInteger>.
I don't want to have BigInteger objects but Long objects.
How I can tell Hibernate to return to me a List<Long>?
I want to do it programmatically here in this method,
as I don't want my changes to have global impact.
Is that possible and how?

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Long> getAllCheckSubAccounts() {
    Object res = getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {

            String sql =
                    "select " + 
                    " " + 
                    "distinct sa.se_subaccountid as cid " + 
                    " " + 
                    "from  " + 
                    "subaccount sa  " + 
                    "inner join account a on sa.accountid = a.accountid " + 
                    "inner join product m on sa.mb_id = m.mb_id  " + 
                    "where " + 
                    "(a.se_id = 2) " + 
                    "and " + 
                    "(sa.se_subaccountid is not null)  " + 
                    "and " + 
                    "(m.mb_status not in (128, 512)) " + 
                    "order by  " + 
                    "sa.se_subaccountid asc ";

            SQLQuery query = session.createSQLQuery(sql);
            return query.list();
        }
    });

    return ((List<Long>) res);
}

Upvotes: 3

Views: 6820

Answers (2)

Haroon
Haroon

Reputation: 127

sess.createSQLQuery("SELECT * FROM CATS")
    .addScalar("cid", StandardBasicTypes.LONG)

Similar thread here:
How to specify data type when using native SQL query?

Upvotes: 5

ben
ben

Reputation: 168

Hibernate gets BigInteger from the DB metadata. You can override by:

query.addScalar("nextval", LongType.INSTANCE)

Upvotes: 2

Related Questions