這是我以前BLOG的東東,寫于2005年初做用戶\單位管理模塊的那個時期
單位代碼是由行政區劃+序號組成的,所以不好用序列,但我要取出當前最大的一個單位序號,因此我在TDWDM.HBM.XML中加上這段HSQL:
??? <query name="maxdwdm1">
????? <![CDATA[
??????? select max(substr(tdwdm.ccode,7,2)) from com.sugem.appAdmin.hbm.Tdwdm as tdwdm
????? ]]>
??? </query>
Java類中這樣用:
??? public String findByMaxdw1Code() throws HibernateDaoException {
??????? Session sess = this.getSession();
??????? try {
??????????? Query query = sess.getNamedQuery("maxdwdm1");
??????????? //如果HSQL中有參數的話就這樣用setParameter
??????????? Object results = (Object) query.uniqueResult();
??????????? return (String) results;
??????? } catch (HibernateException he) {
??????????? throw new HibernateDaoException("通過單位id查找單位時出錯" + he.getMessage(),?he);
??????? }
??? }
還有一種情況,我要得到用戶沒有的權限,可以這樣用:
??? public List findOutUserAuthByUserid(String userid) throws AppException {
??????? Session s = this.initSession();
??????? TuserauthorityDao dao = new TuserauthorityDao(s);
??????? try {
??????????? List l = dao.find("from com.sugem.appAdmin.hbm.Tappywlx as ywlx "
????????????????????????????? +"where ywlx.ccode not in "
????????????????????????????? +"(select userauth.cywlx "
????????????????????????????? +"from com.sugem.appAdmin.hbm.Tuserauthority as userauth "
????????????????????????????? + "where userauth.cuserid='" + userid + "')");
??????????? this.closeSession(s);
??????????? return l;
??????? } catch (HibernateDaoException he) {
??????????? this.closeSession(s);
??????????? throw new AppException("查找組數據findOutUserAuthByUserid時出錯:" +
?????????????????????????????????? he.getMessage());
??????? }
??? }
現在看來,hibernateHSQL的確沒有ibatis的方便,現在有時臨時做一些報表我還是喜歡采用以前老早用的org.apache.commons.beanutils.RowSetDynaClass類......