1 package org.javabb.dao.hibernate; 2 3 import java.sql.Connection ; 4 import java.sql.SQLException ; 5 import java.sql.Statement ; 6 import java.text.MessageFormat ; 7 import java.util.List ; 8 9 import net.sf.hibernate.HibernateException; 10 import net.sf.hibernate.Query; 11 import net.sf.hibernate.Session; 12 13 import org.javabb.dao.DAOConstants; 14 import org.javabb.infra.Parser; 15 import org.javabb.vo.VOObject; 16 import org.springframework.dao.DataAccessResourceFailureException; 17 import org.springframework.orm.hibernate.HibernateCallback; 18 import org.springframework.orm.hibernate.support.HibernateDaoSupport; 19 20 35 36 41 public class HibernateDAO extends HibernateDaoSupport implements DAOConstants { 42 43 protected String pathVO = "org.javabb.vo."; 45 46 47 51 public void deleteFrom(String query) { 52 getHibernateTemplate().delete(Parser.replaceHQL(query)); 53 } 54 55 60 protected Integer countRowsOfTable(final String vo, final String index) { 61 return (Integer ) getHibernateTemplate().execute(new HibernateCallback() { 62 public Object doInHibernate(Session session) throws HibernateException, SQLException { 63 return (Integer ) session.iterate( MessageFormat.format("select count({0}) from {1}", new String [] { index, pathVO + vo })).next(); 66 } 67 }); 68 } 69 70 77 protected Integer countRowsByWhere( 78 String nmClass, 79 String index, 80 String [] whereEqualField, 81 String [] whereEqualValue) { 82 83 String sql = MessageFormat.format("select count(tbl.{0}) from {1} as tbl ", 84 new Object [] { index, pathVO + nmClass }); 85 86 89 String where = " where "; 90 91 for (int i = 0; i < whereEqualField.length; i++) { 92 where += (" tbl." + whereEqualField[i] + " = " + whereEqualValue[i] + " and"); 93 } 94 95 where = where.substring(0, where.length() - 3); 96 sql += where; 97 98 final String finalSql = sql; 99 100 return (Integer ) getHibernateTemplate().execute(new HibernateCallback() { 101 public Object doInHibernate(Session session) throws HibernateException, SQLException { 102 return session.iterate(finalSql).next(); 103 } 104 }); 105 } 106 107 112 protected VOObject load(VOObject obj) { 113 return (VOObject) getHibernateTemplate().load(obj.getClass(), obj.getId()); 114 } 115 116 121 protected List getList(String condicao) { 122 final String finalCondicao = Parser.replaceHQL(condicao); 123 return getHibernateTemplate().executeFind(new HibernateCallback() { 124 public Object doInHibernate(Session session) throws HibernateException, SQLException { 125 return session.createQuery(finalCondicao).list(); 126 } 127 }); 128 } 129 130 137 protected List getList(final String condicao, final int firstRes, final int maxRes) { 138 return getHibernateTemplate().executeFind(new HibernateCallback() { 139 public Object doInHibernate(Session session) throws HibernateException, SQLException { 140 String cond = Parser.replaceHQL(condicao); 141 Query q = session.createQuery(cond); 142 q.setFirstResult(firstRes); 143 q.setMaxResults(maxRes); 144 return q.list(); 145 } 146 }); 147 } 148 149 158 protected List findAll( 159 Class nmClass, 160 String [] orderBy, 161 String [] ascDesc, 162 int firstRes, 163 int maxRes) { 164 165 String sql = "from " + nmClass.getName() + " as tbl "; 166 String sqlOrderBy = " order by "; 167 168 for (int i = 0; i < orderBy.length; i++) { 169 sqlOrderBy += (" tbl." + orderBy[i] + " " + ascDesc[i] + ","); 170 } 171 172 sqlOrderBy = sqlOrderBy.substring(0, sqlOrderBy.length() - 1); 173 sql += sqlOrderBy; 174 175 return getList(sql, firstRes, maxRes); 176 } 177 178 186 protected List findAll( 187 Class nmClass, 188 String [] whereEqualField, 189 String [] whereEqualValue, 190 String [] orderBy, 191 String [] ascDesc) { 192 193 String sql = "from " + nmClass.getName() + " as tbl "; 194 195 198 String where = " where "; 199 200 for (int i = 0; i < whereEqualField.length; i++) { 201 where += (" tbl." + whereEqualField[i] + " = " + whereEqualValue[i] + " and"); 202 } 203 204 where = where.substring(0, where.length() - 3); 205 sql += where; 206 207 210 String sqlOrderBy = " order by "; 211 212 for (int i = 0; i < orderBy.length; i++) { 213 sqlOrderBy += (" tbl." + orderBy[i] + " " + ascDesc[i] + ","); 214 } 215 216 sqlOrderBy = sqlOrderBy.substring(0, sqlOrderBy.length() - 1); 217 218 sql += sqlOrderBy; 219 220 return getList(sql); 221 } 222 223 233 protected List findAll( 234 Class nmClass, 235 String [] whereEqualField, 236 String [] whereEqualValue, 237 String [] orderBy, 238 String [] ascDesc, 239 int firstRes, 240 int maxRes) { 241 242 String sql = "from " + nmClass.getName() + " as tbl "; 243 244 247 String where = " where "; 248 249 for (int i = 0; i < whereEqualField.length; i++) { 250 where += (" tbl." + whereEqualField[i] + " = " + whereEqualValue[i] + " and"); 251 } 252 253 where = where.substring(0, where.length() - 3); 254 sql += where; 255 256 259 String sqlOrderBy = " order by "; 260 261 for (int i = 0; i < orderBy.length; i++) { 262 sqlOrderBy += (" tbl." + orderBy[i] + " " + ascDesc[i] + ","); 263 } 264 265 sqlOrderBy = sqlOrderBy.substring(0, sqlOrderBy.length() - 1); 266 267 sql += sqlOrderBy; 268 269 List lst = this.getList(sql, firstRes, maxRes); 270 271 return lst; 272 } 273 274 278 protected void executeSQL(final String sql) { 279 getHibernateTemplate().execute(new HibernateCallback() { 280 public Object doInHibernate(Session session) throws HibernateException, SQLException { 281 session.clear(); 282 Connection cnn = session.connection(); 283 Statement stmt = cnn.createStatement(); 284 stmt.executeUpdate(sql); 285 cnn.commit(); 286 return null; 287 } 288 }); 289 } 290 291 297 protected Object loadByUniqueAttribute( 298 final Class c, 299 final String attributeName, 300 final Object attributeValue) { 301 return getHibernateTemplate().execute(new HibernateCallback() { 302 public Object doInHibernate(Session session) throws HibernateException, SQLException { 303 String queryString = MessageFormat.format("FROM {0} o WHERE {1} = :param", 304 new Object [] { c.getName(), attributeName }); 305 Query query = session.createQuery(queryString); 306 query.setParameter("param", attributeValue); 307 return query.uniqueResult(); 308 } 309 }); 310 } 311 312 318 protected List findByAttribute( 319 final Class c, 320 final String attributeName, 321 final Object attributeValue) { 322 323 String queryString = MessageFormat.format("FROM {0} o WHERE {1} = :param", 324 new Object [] { c.getName(), attributeName }); 325 return getHibernateTemplate().findByNamedParam(queryString, "param", attributeValue); 326 } 327 328 333 protected int countRows(final Class c, final String indexAttributeName) { 334 return ((Integer ) getHibernateTemplate().execute(new HibernateCallback() { 335 public Object doInHibernate(Session session) throws HibernateException, SQLException { 336 String query = MessageFormat.format("SELECT COUNT(o.{0}) FROM {1} o", 337 new String [] { indexAttributeName, c.getName() }); 338 return (Integer ) session.createQuery(query).uniqueResult(); 339 } 340 })).intValue(); 341 } 342 343 350 protected int countRowsWhere( 351 final Class c, 352 final String indexAttributeName, 353 final String whereClause, 354 final Object [] params) { 355 356 return ((Integer ) getHibernateTemplate().execute(new HibernateCallback() { 357 public Object doInHibernate(Session session) throws HibernateException, SQLException { 358 String queryString = MessageFormat.format("SELECT COUNT({0}) FROM {1} o WHERE {2}", new String [] { indexAttributeName, c.getName(), whereClause }); 360 Query query = session.createQuery(queryString); 361 if (params != null) { 362 for (int i = 0; i < params.length; i++) { 363 query.setParameter(i, params[i]); 364 } 365 } 366 367 return (Integer ) query.iterate().next(); 368 } 369 })).intValue(); 370 } 371 372 381 protected List find( 382 final Class c, 383 final String where, 384 final Object [] params, 385 final String orderBy, 386 final int pageNumber, 387 final int itemsPerPage) { 388 389 return getHibernateTemplate().executeFind(new HibernateCallback() { 390 public Object doInHibernate(Session session) throws HibernateException, SQLException { 391 392 String hql = MessageFormat.format("FROM {0} o WHERE {1} ORDER BY {2}", 393 new Object [] { c.getName(), where, orderBy }); 394 395 Query query = session.createQuery(hql); 396 for (int i = 0; i < params.length; i++) { 397 query.setParameter(i, params[i]); 398 } 399 if (pageNumber >= 0 && itemsPerPage > 0) { 400 int firstResult = Math.max(0, pageNumber - 1) * itemsPerPage; 402 query.setFirstResult(firstResult); 403 query.setMaxResults(itemsPerPage); 404 } 405 return query.list(); 406 } 407 }); 408 } 409 410 411 protected List find( 412 final String hql, 413 final Object [] params, 414 final int pageNumber, 415 final int itemsPerPage) { 416 417 return getHibernateTemplate().executeFind(new HibernateCallback() { 418 public Object doInHibernate(Session session) throws HibernateException, SQLException { 419 420 Query query = session.createQuery(hql); 421 for (int i = 0; i < params.length; i++) { 422 query.setParameter(i, params[i]); 423 } 424 if (pageNumber >= 0 && itemsPerPage > 0) { 425 int firstResult = Math.max(0, pageNumber - 1) * itemsPerPage; 427 query.setFirstResult(firstResult); 428 query.setMaxResults(itemsPerPage); 429 } 430 return query.list(); 431 } 432 }); 433 } 434 435 436 443 protected List findAll( 444 final Class c, 445 final String orderBy, 446 final int pageNumber, 447 final int itemsPerPage) { 448 449 return getHibernateTemplate().executeFind(new HibernateCallback() { 450 public Object doInHibernate(Session session) throws HibernateException, SQLException { 451 452 String hql = MessageFormat.format("FROM {0} o ORDER BY {1}", 453 new Object [] { c.getName(), orderBy }); 454 455 Query query = session.createQuery(hql); 456 if (pageNumber >= 0 && itemsPerPage > 0) { 457 query.setFirstResult(Math.max(0, pageNumber - 1) * itemsPerPage); 458 query.setMaxResults(itemsPerPage); 459 } 460 return query.list(); 461 } 462 }); 463 } 464 465 protected String getFieldName(int i) { 466 return ""; 467 } 468 469 474 protected List findAll(Class c, int[] sortingFields) { 475 StringBuffer sb = new StringBuffer (); 476 for (int i = 0; i < sortingFields.length; i++) { 477 if (sortingFields[i] > 0) { 478 sb.append("o."); 479 sb.append(getFieldName(sortingFields[i])); 480 sb.append(" ASC"); 481 } else if (sortingFields[i] < 0) { 482 sb.append("o."); 483 sb.append(getFieldName(-sortingFields[i])); 484 sb.append(" DESC"); 485 } else { 486 throw new IllegalArgumentException (""); 487 } 488 if (i + 1 < sortingFields.length) { 489 sb.append(", "); 490 } 491 } 492 return findAll(c, sb.toString(), ALL_PAGES, ALL_PAGES); 493 } 494 495 496 501 protected Object load(Class cls, Long id) { 502 try{ 503 return getSession().load(cls, id); 504 }catch(Exception e){ 505 System.out.println("Error at load of HibernateDAO:" + e); 506 return null; 507 } 508 } 509 510 511 512 public Object add(Object obj) throws Exception { 513 return getHibernateTemplate().save(obj); 514 } 515 516 public List loadAll(Object obj) throws Exception { 517 return getHibernateTemplate().loadAll(obj.getClass()); 518 } 519 520 public Object load(Object obj, Long id) throws Exception { 521 return getHibernateTemplate().load(obj.getClass(), id); 522 } 523 524 public void delete(Object obj) throws Exception { 525 getHibernateTemplate().delete(obj); 526 } 527 528 } 529 | Popular Tags |