2014-03-07 72 views
1

我在我的項目的其中一個DAO中使用了此方法。
底層的RDBMS是MS SQL Server。
se_subaccountid在DB級別上定義爲bigintHibernate - 如何強制以編程方式返回Long而不是BigInteger?

Hibernate返回給我List<BigInteger>
我不想有BigInteger對象,但長對象。
我如何告訴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); 
} 

回答

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

類似的線程在這裏:
How to specify data type when using native SQL query?

+0

謝謝,這樣做。 –

+2

編輯爲使用StandardBasicTypes.LONG而不是Hibernate.LONG,因爲Hibernate「常量」以前不推薦使用,現在已完全從代碼中刪除。另一種選擇是使用LongType.INSTANCE。 –

2

Hibernate從數據庫元數據中獲取BigInteger。您可以覆蓋:

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

+1

這將使返回整數,不是多頭。我認爲你的意思是LongType.INSTANCE。 –

+0

感謝您的更正 – ben

相關問題