使用Hibernate SQLquery實現(xiàn)動態(tài)表
在實際的項目應(yīng)用中,有時會設(shè)計出這樣的一種數(shù)據(jù)表,每個時間段產(chǎn)生一個新表,例如是按年或月或日。相同類型的表中,所有的字段結(jié)構(gòu)都是一樣的。而 Hibernate 提供的類與表的映射,是只能映射到一個具體表的,在程序的運行過程中,很難去動態(tài)修改一個 hbm 對應(yīng)的表名。我在網(wǎng)上也有看到一實現(xiàn),但是很復(fù)雜,并且不符合我的要求。
因此我就想到直接用 jdbc 去操作數(shù)據(jù)庫,這樣的做法是繞過 Hibernate 了。方法是從 Hibernate 的 session 中,直接取得數(shù)據(jù)庫 connection ,然后就直接 jdbc 了。
后來在升級了 proxool 到 9.0RC3 后,發(fā)現(xiàn)居然出現(xiàn)了數(shù)據(jù)庫連接無法釋放的問題。為了解決這個問題,我查閱了 Hibernate doc。我發(fā)現(xiàn)原來用Hibernate SQLQuery 可以更好的解決,并且可以重新用于 Hibernate hbm 機制。以下舉例說明。
例如我有一個 pojo 是 ReadInfo,用來記錄閱讀信息的。由于數(shù)據(jù)量寵大,所以我的思路是按月劃分,每個月一張表。所以只是表名不同,而字段是完全相同的。
ReadInfo.java 是這樣的,其中 userId, year, month, day 是聯(lián)合主鍵:
private Integer userId;
private Integer year;
private Integer month;
private Integer day;
private Integer point ;
那么相應(yīng)的 ReadInfo.hbm.xml 的片段是
- < class name= "ReadInfo" table= "tblReadInfo " mutable = "false" >
- < composite-id>
- < key-property name= "userId" column= "userId" type= "integer" / >
- < key-property name= "year" column= "year" type= "integer" / >
- < key-property name= "month" column= "month" type= "integer" / >
- < key-property name= "day" column= "day" type= "integer" / >
- < / composite-id>
- < property name= "point" column= "point" type= "integer" / >
- < / class>
上面的xml,注意 2 個細節(jié)
1. pojo 所映射的 table tblReadInfo 實際上是不存在的。實際的表是 tblRead200710 之類的;
2. mutable 要設(shè)置為 false,即是說,關(guān)閉 Hibernate 對這個 pojo 的任何持久化操作,以避免 Hibernate 把數(shù)據(jù)寫到 tblReadInfo 中(這個表是不存在的嘛)。因此,所有的持久化操作,都是需要自己通過Hibernate SQLQuery 來處理。
現(xiàn)在可以看一下 ado 中的操作了,先看一個 select 操作
- public ReadInfo selectReadInfo( Integer userId, Integer year,
- Integer month, Integer day) throws HibernateException
- {
- ReadInfo readInfo = null ;
- Session session = getSession ( ) ;
- Transaction tx = session. beginTransaction( ) ;
- try
- {
- String sql = "select * from tblRead"
- + Misc. formatMoon( year, month)
- + " where userId=? and day=?" ;
- SQLQuery query = session. createSQLQuery( sql ) ;
- query . addEntity( ReadInfo. class ) ;
- query . setLong ( 0, userId) ;
- query . setInteger( 1, day) ;
- readInfo = ( ReadInfo) query . uniqueResult( ) ;
- tx. commit ( ) ;
- }
- catch ( HibernateException e)
- {
- log . error ( "catch exception:" , e) ;
- if ( tx ! = null )
- {
- tx. rollback ( ) ;
- }
- throw e;
- }
- return readInfo;
- }
上面的代碼,關(guān)鍵是以下幾點:
1. 通過函數(shù)參數(shù)的 year, month 來確定要操作的表名,我自己寫了一個 Misc.formatMoon(year, month) 來生成 "yyyyMM" 格式的字串;
2. 使用了 SQLQuery ,再通過 query.addEntity(ReadInfo.class); 建立與 ReadInfo 的映射關(guān)系;
3. query.setXxx() 與 PreparedStatement 的類似,不過索引是從 0 開始;
4. 其它的就跟一般的 Query 操作類似的了。
再看一個 insert 操作
- public void insertReadInfo( ReadInfo readInfo) throws HibernateException
- {
- Session session = getSession ( ) ;
- Transaction tx = session. beginTransaction( ) ;
- try
- {
- String sql = "insert into tblRead"
- + Misc. formatMoon( readInfo. getYear ( ) , readInfo. getMonth ( ) )
- + " (userId, year, month, day, point) values (?, ?, ?, ?, ?)" ;
- SQLQuery query = session. createSQLQuery( sql ) ;
- query . setLong ( 0, readInfo. getUserId( ) ) ;
- query . setInteger( 1, readInfo. getYear ( ) ) ;
- query . setInteger( 2, readInfo. getMonth ( ) ) ;
- query . setInteger( 3, readInfo. getDay ( ) ) ;
- query . setInteger( 4, readInfo. getPoint ( ) ) ;
- query . executeUpdate ( ) ;
- tx. commit ( ) ;
- }
- catch ( HibernateException e)
- {
- log . error ( "catch exception:" , e) ;
- if ( tx ! = null )
- {
- tx. rollback ( ) ;
- }
- throw e;
- }
- }
同理,update, delete 等操作也是這樣通過Hibernate SQLquery來實現(xiàn)的。
這種Hibernate SQLquery處理方式的麻煩的地方是需要手工寫 SQL,因此要盡量寫通用的標(biāo)準(zhǔn)SQL,不然在數(shù)據(jù)庫兼容方面會有問題。當(dāng)然,有時是會出現(xiàn)無法兼容的情況,那么可以考慮把 SQL寫到配置文件中,根據(jù)不同的數(shù)據(jù)庫,裝載相應(yīng)的配置文件。
【編輯推薦】