| Revision | 69 (tree) |
|---|---|
| Time | 2015-08-20 23:45:46 |
| Author | t_nakayama1971 |
(empty log message)
| @@ -18,7 +18,6 @@ | ||
| 18 | 18 | import common.db.dao.DaoConstraintException; |
| 19 | 19 | import common.db.dao.DaoLockException; |
| 20 | 20 | import common.db.dao.DaoPropertyException; |
| 21 | - | |
| 22 | 21 | import core.exception.PhysicalException; |
| 23 | 22 | import core.exception.ThrowableUtil; |
| 24 | 23 |
| @@ -205,10 +204,9 @@ | ||
| 205 | 204 | /** |
| 206 | 205 | * フラッシュ処理 |
| 207 | 206 | * |
| 208 | - * @param session セションオブジェクト | |
| 209 | 207 | */ |
| 210 | 208 | @Override |
| 211 | - protected final void flushSession(final Session session, final Serializable item) { | |
| 209 | + protected final void flushSession(final Serializable item) { | |
| 212 | 210 | return; |
| 213 | 211 | } |
| 214 | 212 | } |
| @@ -173,15 +173,14 @@ | ||
| 173 | 173 | */ |
| 174 | 174 | private <T> T find(final Class<T> cls, final Serializable id, final LockOptions lo) { |
| 175 | 175 | try { |
| 176 | - // セションオブジェクト取得 | |
| 177 | - Session session = getSession(); | |
| 178 | 176 | // トランザクション開始 |
| 179 | 177 | beginTransaction(); |
| 180 | 178 | // 読込 |
| 181 | - Object obj = session.load(cls, id, lo); | |
| 182 | - session.evict(obj); | |
| 179 | + Object obj = getSession().load(cls, id, lo); | |
| 180 | + getSession().evict(obj); | |
| 183 | 181 | return cls.cast(obj); |
| 184 | 182 | } catch (final ObjectNotFoundException ex) { |
| 183 | + LogManager.getLogger().info(ex.getMessage()); | |
| 185 | 184 | return null; |
| 186 | 185 | } catch (final HibernateException ex) { |
| 187 | 186 | ThrowableUtil.error(ex); |
| @@ -200,16 +199,12 @@ | ||
| 200 | 199 | toNull(item); |
| 201 | 200 | try { |
| 202 | 201 | if (isEntity(item.getClass())) { |
| 203 | - // セションオブジェクト取得 | |
| 204 | - Session session = getSession(); | |
| 205 | - if (session.contains(item) && session.isReadOnly(item)) { | |
| 206 | - session.evict(item); | |
| 207 | - } | |
| 202 | + evict(item); | |
| 208 | 203 | // トランザクション開始 |
| 209 | 204 | beginTransaction(); |
| 210 | 205 | // 保存 |
| 211 | - session.save(item); | |
| 212 | - flushSession(session, item); | |
| 206 | + getSession().save(item); | |
| 207 | + flushSession(item); | |
| 213 | 208 | } else { |
| 214 | 209 | setId(item); |
| 215 | 210 | List<Object> param = new ArrayList<>(); |
| @@ -260,16 +255,12 @@ | ||
| 260 | 255 | // NULL可項目NULL化 |
| 261 | 256 | toNull(item); |
| 262 | 257 | try { |
| 263 | - // セションオブジェクト取得 | |
| 264 | - Session session = getSession(); | |
| 265 | - if (session.contains(item) && session.isReadOnly(item)) { | |
| 266 | - session.evict(item); | |
| 267 | - } | |
| 258 | + evict(item); | |
| 268 | 259 | // トランザクション開始 |
| 269 | 260 | beginTransaction(); |
| 270 | 261 | // 更新 |
| 271 | - session.merge(item); | |
| 272 | - flushSession(session, item); | |
| 262 | + getSession().merge(item); | |
| 263 | + flushSession(item); | |
| 273 | 264 | } catch (final LockAcquisitionException ex) { |
| 274 | 265 | LogManager.getLogger().info(ex.getMessage()); |
| 275 | 266 | throw new DaoLockException(ex, this.noWait); |
| @@ -298,16 +289,12 @@ | ||
| 298 | 289 | toNull(item); |
| 299 | 290 | try { |
| 300 | 291 | if (isEntity(item.getClass())) { |
| 301 | - // セションオブジェクト取得 | |
| 302 | - Session session = getSession(); | |
| 303 | - if (session.contains(item) && session.isReadOnly(item)) { | |
| 304 | - session.evict(item); | |
| 305 | - } | |
| 292 | + evict(item); | |
| 306 | 293 | // トランザクション開始 |
| 307 | 294 | beginTransaction(); |
| 308 | 295 | // 更新 |
| 309 | - session.update(item); | |
| 310 | - flushSession(session, item); | |
| 296 | + getSession().update(item); | |
| 297 | + flushSession(item); | |
| 311 | 298 | } else { |
| 312 | 299 | List<Object> param = new ArrayList<>(); |
| 313 | 300 | execute(EntityUtil.toUpdateSql(item, param), |
| @@ -347,16 +334,12 @@ | ||
| 347 | 334 | public final boolean delete(final Serializable item) { |
| 348 | 335 | try { |
| 349 | 336 | if (isEntity(item.getClass())) { |
| 350 | - // セションオブジェクト取得 | |
| 351 | - Session session = getSession(); | |
| 352 | - if (session.contains(item) && session.isReadOnly(item)) { | |
| 353 | - session.evict(item); | |
| 354 | - } | |
| 337 | + evict(item); | |
| 355 | 338 | // トランザクション開始 |
| 356 | 339 | beginTransaction(); |
| 357 | 340 | // 削除処理 |
| 358 | - session.delete(item); | |
| 359 | - flushSession(session, item); | |
| 341 | + getSession().delete(item); | |
| 342 | + flushSession(item); | |
| 360 | 343 | } else { |
| 361 | 344 | List<Object> param = new ArrayList<>(); |
| 362 | 345 | execute(EntityUtil.toDeleteSql(item, param), |
| @@ -380,6 +363,16 @@ | ||
| 380 | 363 | } |
| 381 | 364 | |
| 382 | 365 | /** |
| 366 | + * 分離 | |
| 367 | + * @param item 対象モデル | |
| 368 | + */ | |
| 369 | + private void evict(final Serializable item) { | |
| 370 | + if (getSession().contains(item) && getSession().isReadOnly(item)) { | |
| 371 | + getSession().evict(item); | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** | |
| 383 | 376 | * ネイティブSQLクエリ実行 |
| 384 | 377 | * @param <T> ジェネリックス |
| 385 | 378 | * |
| @@ -391,16 +384,14 @@ | ||
| 391 | 384 | @Override |
| 392 | 385 | public final <T> T select(final Class<T> cls, final String query, final Object... vals) { |
| 393 | 386 | try { |
| 394 | - // セションオブジェクト取得 | |
| 395 | - Session session = getSession(); | |
| 396 | 387 | // トランザクション開始 |
| 397 | 388 | beginTransaction(); |
| 398 | 389 | |
| 399 | - List<T> list = createQuery(session, getRecordClass(cls), | |
| 390 | + List<T> list = createQuery(getSession(), getRecordClass(cls), | |
| 400 | 391 | query, vals).setReadOnly(true).setMaxResults(1).list(); |
| 401 | 392 | if (list != null && !list.isEmpty()) { |
| 402 | 393 | if (isEntity(cls)) { |
| 403 | - session.evict(list.get(0)); | |
| 394 | + getSession().evict(list.get(0)); | |
| 404 | 395 | } else if (cls != null && !isJavaClass(cls)) { |
| 405 | 396 | return EntityUtil.toEntity(Factory.cast(list.get(0)), cls); |
| 406 | 397 | } |
| @@ -408,6 +399,7 @@ | ||
| 408 | 399 | } |
| 409 | 400 | return null; |
| 410 | 401 | } catch (final ObjectNotFoundException | DataException ex) { |
| 402 | + LogManager.getLogger().info(ex.getMessage()); | |
| 411 | 403 | return null; |
| 412 | 404 | } catch (final HibernateException ex) { |
| 413 | 405 | ThrowableUtil.error(ex); |
| @@ -431,17 +423,16 @@ | ||
| 431 | 423 | } |
| 432 | 424 | |
| 433 | 425 | try { |
| 434 | - // セションオブジェクト取得 | |
| 435 | - Session session = getSession(); | |
| 436 | 426 | // トランザクション開始 |
| 437 | 427 | beginTransaction(); |
| 438 | 428 | |
| 439 | - List<T> list = createQuery(session, getRecordClass(cls), addFor(query), vals).list(); | |
| 429 | + List<T> list = createQuery(getSession(), | |
| 430 | + getRecordClass(cls), addFor(query), vals).list(); | |
| 440 | 431 | if (list == null || list.isEmpty()) { |
| 441 | 432 | return null; |
| 442 | 433 | } else if (list.size() == 1) { |
| 443 | 434 | if (isEntity(cls)) { |
| 444 | - session.evict(list.get(0)); | |
| 435 | + getSession().evict(list.get(0)); | |
| 445 | 436 | } else if (cls != null) { |
| 446 | 437 | return EntityUtil.toEntity(Factory.cast(list.get(0)), cls); |
| 447 | 438 | } |
| @@ -450,6 +441,7 @@ | ||
| 450 | 441 | |
| 451 | 442 | throw new IllegalStateException("More than one record is selected."); |
| 452 | 443 | } catch (final ObjectNotFoundException | DataException ex) { |
| 444 | + LogManager.getLogger().info(ex.getMessage()); | |
| 453 | 445 | return null; |
| 454 | 446 | } catch (final LockAcquisitionException ex) { |
| 455 | 447 | LogManager.getLogger().info(ex.getMessage()); |
| @@ -484,19 +476,18 @@ | ||
| 484 | 476 | public final <T> List<T> selectAll(final Class<T> cls, |
| 485 | 477 | final String query, final Object... vals) { |
| 486 | 478 | try { |
| 487 | - // セションオブジェクト取得 | |
| 488 | - Session session = getSession(); | |
| 489 | 479 | // トランザクション開始 |
| 490 | 480 | beginTransaction(); |
| 491 | 481 | |
| 492 | 482 | // String sql = query.replaceAll(" [Ff][Oo][Rr] .*", ""); |
| 493 | - List<T> list = createQuery( | |
| 494 | - session, getRecordClass(cls), query, vals).setReadOnly(true).list(); | |
| 483 | + List<T> list = createQuery(getSession(), | |
| 484 | + getRecordClass(cls), query, vals).setReadOnly(true).list(); | |
| 495 | 485 | if (cls != null && !isEntity(cls) && !isJavaClass(cls)) { |
| 496 | 486 | return EntityUtil.toEntityList(Factory.cast(list), cls); |
| 497 | 487 | } |
| 498 | 488 | return list; |
| 499 | 489 | } catch (final ObjectNotFoundException | DataException ex) { |
| 490 | + LogManager.getLogger().info(ex.getMessage()); | |
| 500 | 491 | return null; |
| 501 | 492 | } catch (final HibernateException ex) { |
| 502 | 493 | ThrowableUtil.error(ex); |
| @@ -522,13 +513,11 @@ | ||
| 522 | 513 | @Override |
| 523 | 514 | public final int execute(final String query, final Object... vals) { |
| 524 | 515 | try { |
| 525 | - // セションオブジェクト取得 | |
| 526 | - Session session = getSession(); | |
| 527 | 516 | // トランザクション開始 |
| 528 | 517 | beginTransaction(); |
| 529 | 518 | |
| 530 | - session.flush(); | |
| 531 | - SQLQuery sql = createQuery(session, null, query, vals); | |
| 519 | + getSession().flush(); | |
| 520 | + SQLQuery sql = createQuery(getSession(), null, query, vals); | |
| 532 | 521 | return sql.executeUpdate(); |
| 533 | 522 | } catch (final LockAcquisitionException ex) { |
| 534 | 523 | LogManager.getLogger().info(ex.getMessage()); |
| @@ -553,9 +542,8 @@ | ||
| 553 | 542 | HibernateJdbcWork.class.cast(work).setDialect(this.config.getDialect()); |
| 554 | 543 | } |
| 555 | 544 | try { |
| 556 | - Session session = getSession(); | |
| 557 | 545 | beginTransaction(); |
| 558 | - session.doWork(new WorkWrap(work)); | |
| 546 | + getSession().doWork(new WorkWrap(work)); | |
| 559 | 547 | } catch (final HibernateException ex) { |
| 560 | 548 | ThrowableUtil.error(ex); |
| 561 | 549 | throw new PhysicalException(ex); |
| @@ -658,10 +646,9 @@ | ||
| 658 | 646 | /** |
| 659 | 647 | * 同期化 |
| 660 | 648 | * |
| 661 | - * @param session セション | |
| 662 | 649 | * @param item 挿入対象モデル |
| 663 | 650 | */ |
| 664 | - protected abstract void flushSession(final Session session, final Serializable item); | |
| 651 | + protected abstract void flushSession(final Serializable item); | |
| 665 | 652 | |
| 666 | 653 | /** |
| 667 | 654 | * 更新用処理確認 |
| @@ -139,12 +139,11 @@ | ||
| 139 | 139 | /** |
| 140 | 140 | * フラッシュ処理 |
| 141 | 141 | * |
| 142 | - * @param session セションオブジェクト | |
| 143 | 142 | */ |
| 144 | 143 | @Override |
| 145 | - protected final void flushSession(final Session session, final Serializable item) { | |
| 146 | - session.flush(); | |
| 147 | - session.evict(item); | |
| 144 | + protected final void flushSession(final Serializable item) { | |
| 145 | + getSession().flush(); | |
| 146 | + getSession().evict(item); | |
| 148 | 147 | } |
| 149 | 148 | |
| 150 | 149 | /** |