Hibernate - 如何以编程方式强制它返回Long而不是BigInteger?

时间:2014-03-07 11:28:32

标签: java hibernate casting

我在我项目的一个DAO中有这个方法 底层RDBMS是MS SQL Server se_subaccountid在数据库级别定义为bigint

Hibernate返回给我List<BigInteger> 我不想拥有BigInteger对象而是Long对象 我怎么能告诉Hibernate给我一个List<Long>? 我想在此方法中以编程方式执行此操作,
因为我不希望我的变化产生全球影响 这可能吗?怎么样?

@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);
}

2 个答案:

答案 0 :(得分:4)

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

类似的主题:
How to specify data type when using native SQL query?

答案 1 :(得分:2)

Hibernate从数据库元数据中获取BigInteger。您可以通过以下方式覆盖:

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