|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractHibernateDAO.java | - | 8.3% | 13.8% | 8.8% |
|
||||||||||||||
| 1 |
/**
|
|
| 2 |
* @(#)AbstractHibernateDAO.java
|
|
| 3 |
*
|
|
| 4 |
* JFoxSOAF, Service-Oriented Application Framework
|
|
| 5 |
*
|
|
| 6 |
* Copyright(c) JFoxSOAF Team
|
|
| 7 |
*
|
|
| 8 |
* Licensed under the GNU LGPL, Version 2.1 (the "License");
|
|
| 9 |
* you may not use this file except in compliance with the License.
|
|
| 10 |
* You may obtain a copy of the License at
|
|
| 11 |
*
|
|
| 12 |
* http://www.gnu.org/copyleft/lesser.html
|
|
| 13 |
*
|
|
| 14 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 17 |
* See the License for the specific language governing permissions and
|
|
| 18 |
* limitations under the License.
|
|
| 19 |
*
|
|
| 20 |
* For more information, please visit:
|
|
| 21 |
* http://www.jfox.cn/confluence/display/JFoxSOAF/Home
|
|
| 22 |
* http://www.huihoo.org/jfox/jfoxsoaf
|
|
| 23 |
*/
|
|
| 24 |
|
|
| 25 |
package org.huihoo.jfox.soaf.services.dao;
|
|
| 26 |
|
|
| 27 |
import java.io.Serializable;
|
|
| 28 |
import java.util.Iterator;
|
|
| 29 |
import java.util.List;
|
|
| 30 |
|
|
| 31 |
import net.sf.hibernate.HibernateException;
|
|
| 32 |
import net.sf.hibernate.LockMode;
|
|
| 33 |
import net.sf.hibernate.Session;
|
|
| 34 |
import net.sf.hibernate.type.Type;
|
|
| 35 |
|
|
| 36 |
import org.huihoo.jfox.soaf.container.ServiceFactory;
|
|
| 37 |
import org.huihoo.jfox.soaf.exception.DAOException;
|
|
| 38 |
import org.huihoo.jfox.soaf.services.persistence.HibernateService;
|
|
| 39 |
|
|
| 40 |
/**
|
|
| 41 |
* <p>
|
|
| 42 |
* Abstract Hibernate DAO Template.
|
|
| 43 |
* </p>
|
|
| 44 |
*
|
|
| 45 |
* @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
|
|
| 46 |
* @version $Revision: 1.1 $ $Date: 2005/05/22 06:49:10 $
|
|
| 47 |
* @version Revision: 1.0
|
|
| 48 |
*/
|
|
| 49 |
|
|
| 50 |
public abstract class AbstractHibernateDAO extends AbstractDAO { |
|
| 51 |
|
|
| 52 |
private HibernateService hibernateService;
|
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* Default Construector.
|
|
| 56 |
*/
|
|
| 57 | 4 |
public AbstractHibernateDAO() {
|
| 58 | 4 |
hibernateService = (HibernateService) ServiceFactory.getInstance() |
| 59 |
.getService(HibernateService.class);
|
|
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Check if this instance is associated with this Session
|
|
| 64 |
*
|
|
| 65 |
* @param object an instance of a persistent class
|
|
| 66 |
* @return true if the given instance is associated with this Session
|
|
| 67 |
* @throws HibernateException
|
|
| 68 |
* @throws DAOException
|
|
| 69 |
*/
|
|
| 70 | 0 |
public boolean contains(Object object) throws HibernateException, |
| 71 |
DAOException {
|
|
| 72 | 0 |
boolean isContains = false; |
| 73 | 0 |
Session session; |
| 74 | 0 |
try {
|
| 75 | 0 |
session = hibernateService.openSession(); |
| 76 | 0 |
session.contains(object); |
| 77 | 0 |
hibernateService.commit(); |
| 78 |
} catch (Exception e) {
|
|
| 79 | 0 |
hibernateService.rollback(); |
| 80 | 0 |
throw new DAOException(e); |
| 81 |
} finally {
|
|
| 82 | 0 |
session = null;
|
| 83 | 0 |
hibernateService.closeSession(); |
| 84 |
} |
|
| 85 | 0 |
return isContains;
|
| 86 |
} |
|
| 87 |
|
|
| 88 |
/**
|
|
| 89 |
* Remove a persistent instance from the datastore.
|
|
| 90 |
*
|
|
| 91 |
* @param object
|
|
| 92 |
* @throws HibernateException
|
|
| 93 |
*/
|
|
| 94 | 0 |
public void delete(Object object) throws HibernateException, DAOException { |
| 95 | 0 |
Session session; |
| 96 | 0 |
try {
|
| 97 | 0 |
session = hibernateService.openSession(); |
| 98 | 0 |
session.delete(object); |
| 99 | 0 |
hibernateService.commit(); |
| 100 |
} catch (Exception e) {
|
|
| 101 | 0 |
hibernateService.rollback(); |
| 102 | 0 |
throw new DAOException(e); |
| 103 |
} finally {
|
|
| 104 | 0 |
session = null;
|
| 105 | 0 |
hibernateService.closeSession(); |
| 106 |
} |
|
| 107 |
} |
|
| 108 |
|
|
| 109 |
/**
|
|
| 110 |
* Remove a persistent instance from the datastore with lock mode.
|
|
| 111 |
*
|
|
| 112 |
* @param object
|
|
| 113 |
* @param lockMode
|
|
| 114 |
* @throws HibernateException
|
|
| 115 |
*/
|
|
| 116 | 0 |
public void delete(Object object, LockMode lockMode) |
| 117 |
throws HibernateException, DAOException {
|
|
| 118 | 0 |
Session session; |
| 119 | 0 |
try {
|
| 120 | 0 |
session = hibernateService.openSession(); |
| 121 | 0 |
session.lock(object, lockMode); |
| 122 | 0 |
session.delete(object); |
| 123 | 0 |
hibernateService.commit(); |
| 124 |
} catch (Exception e) {
|
|
| 125 | 0 |
hibernateService.rollback(); |
| 126 | 0 |
throw new HibernateException(e); |
| 127 |
} finally {
|
|
| 128 | 0 |
session = null;
|
| 129 | 0 |
hibernateService.closeSession(); |
| 130 |
} |
|
| 131 |
} |
|
| 132 |
|
|
| 133 |
/**
|
|
| 134 |
* Delete all objects returned by the query. Return the number of objects
|
|
| 135 |
* deleted.
|
|
| 136 |
*
|
|
| 137 |
* @param query
|
|
| 138 |
* @return the number of instances deleted
|
|
| 139 |
* @throws HibernateException
|
|
| 140 |
* @throws DAOException
|
|
| 141 |
*/
|
|
| 142 | 0 |
public int delete(String query) throws HibernateException, DAOException { |
| 143 | 0 |
int instancesCount = 0;
|
| 144 | 0 |
Session session; |
| 145 | 0 |
try {
|
| 146 | 0 |
session = hibernateService.openSession(); |
| 147 | 0 |
instancesCount = session.delete(query); |
| 148 | 0 |
hibernateService.commit(); |
| 149 |
} catch (Exception e) {
|
|
| 150 | 0 |
hibernateService.rollback(); |
| 151 | 0 |
throw new DAOException(e); |
| 152 |
} finally {
|
|
| 153 | 0 |
session = null;
|
| 154 | 0 |
hibernateService.closeSession(); |
| 155 |
} |
|
| 156 | 0 |
return instancesCount;
|
| 157 |
} |
|
| 158 |
|
|
| 159 |
/**
|
|
| 160 |
* Delete all objects returned by the query. Return the number of objects
|
|
| 161 |
* deleted.
|
|
| 162 |
*
|
|
| 163 |
* @param query
|
|
| 164 |
* @return the number of instances deleted
|
|
| 165 |
* @throws HibernateException
|
|
| 166 |
* @throws DAOException
|
|
| 167 |
*/
|
|
| 168 | 0 |
public int delete(String query, Object value, Type type) |
| 169 |
throws HibernateException, DAOException {
|
|
| 170 | 0 |
int instancesCount = 0;
|
| 171 | 0 |
Session session; |
| 172 | 0 |
try {
|
| 173 | 0 |
session = hibernateService.openSession(); |
| 174 | 0 |
instancesCount = session.delete(query, value, type); |
| 175 | 0 |
hibernateService.commit(); |
| 176 |
} catch (Exception e) {
|
|
| 177 | 0 |
hibernateService.rollback(); |
| 178 | 0 |
throw new DAOException(e); |
| 179 |
} finally {
|
|
| 180 | 0 |
session = null;
|
| 181 | 0 |
hibernateService.closeSession(); |
| 182 |
} |
|
| 183 | 0 |
return instancesCount;
|
| 184 |
} |
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* Delete all objects returned by the query. Return the number of objects
|
|
| 188 |
* deleted.
|
|
| 189 |
*
|
|
| 190 |
* @param query
|
|
| 191 |
* @return the number of instances deleted
|
|
| 192 |
* @throws HibernateException
|
|
| 193 |
* @throws DAOException
|
|
| 194 |
*/
|
|
| 195 | 0 |
public int delete(String query, Object[] values, Type[] types) |
| 196 |
throws HibernateException, DAOException {
|
|
| 197 | 0 |
int instancesCount = 0;
|
| 198 | 0 |
Session session; |
| 199 | 0 |
try {
|
| 200 | 0 |
session = hibernateService.openSession(); |
| 201 | 0 |
instancesCount = session.delete(query, values, types); |
| 202 | 0 |
hibernateService.commit(); |
| 203 |
} catch (Exception e) {
|
|
| 204 | 0 |
hibernateService.rollback(); |
| 205 | 0 |
throw new DAOException(e); |
| 206 |
} finally {
|
|
| 207 | 0 |
session = null;
|
| 208 | 0 |
hibernateService.closeSession(); |
| 209 |
} |
|
| 210 | 0 |
return instancesCount;
|
| 211 |
} |
|
| 212 |
|
|
| 213 |
/**
|
|
| 214 |
* Remove this instance from the session cache. Changes to the instance will
|
|
| 215 |
* not be synchronized with the database. This operation cascades to
|
|
| 216 |
* associated instances if the association is mapped with cascade="all" or
|
|
| 217 |
* cascade="all-delete-orphan".
|
|
| 218 |
*
|
|
| 219 |
* @param object object - a persistent instance
|
|
| 220 |
* @throws HibernateException
|
|
| 221 |
* @throws DAOException
|
|
| 222 |
*/
|
|
| 223 | 0 |
public void evict(Object object) throws HibernateException, DAOException { |
| 224 | 0 |
Session session; |
| 225 | 0 |
try {
|
| 226 | 0 |
session = hibernateService.openSession(); |
| 227 | 0 |
session.evict(object); |
| 228 | 0 |
hibernateService.commit(); |
| 229 |
} catch (Exception e) {
|
|
| 230 | 0 |
hibernateService.rollback(); |
| 231 | 0 |
throw new DAOException(e); |
| 232 |
} finally {
|
|
| 233 | 0 |
session = null;
|
| 234 | 0 |
hibernateService.closeSession(); |
| 235 |
} |
|
| 236 |
} |
|
| 237 |
|
|
| 238 |
/**
|
|
| 239 |
* Execute a query.
|
|
| 240 |
*
|
|
| 241 |
* @param query a query expressed in Hibernate's query language
|
|
| 242 |
* @return a distinct list of instances (or arrays of instances)
|
|
| 243 |
* @throws HibernateException
|
|
| 244 |
* @throws DAOException
|
|
| 245 |
*/
|
|
| 246 | 1 |
public List find(String query) throws HibernateException, DAOException { |
| 247 | 1 |
List list; |
| 248 | 1 |
Session session; |
| 249 | 1 |
try {
|
| 250 | 1 |
session = hibernateService.openSession(); |
| 251 | 0 |
list = session.find(query); |
| 252 | 0 |
hibernateService.commit(); |
| 253 |
} catch (Exception e) {
|
|
| 254 | 1 |
hibernateService.rollback(); |
| 255 | 1 |
throw new DAOException(e); |
| 256 |
} finally {
|
|
| 257 | 1 |
session = null;
|
| 258 | 1 |
hibernateService.closeSession(); |
| 259 |
} |
|
| 260 | 0 |
return list;
|
| 261 |
} |
|
| 262 |
|
|
| 263 |
/**
|
|
| 264 |
* Execute a query with bind parameters. Bind a value to a "?" parameter in
|
|
| 265 |
* the query string.
|
|
| 266 |
*
|
|
| 267 |
* @param query
|
|
| 268 |
* @param value
|
|
| 269 |
* @param type
|
|
| 270 |
* @return a distinct list of instances (or arrays of instances)
|
|
| 271 |
* @throws HibernateException
|
|
| 272 |
* @throws DAOException
|
|
| 273 |
*/
|
|
| 274 | 0 |
public List find(String query, Object value, Type type)
|
| 275 |
throws HibernateException, DAOException {
|
|
| 276 | 0 |
List list; |
| 277 | 0 |
Session session; |
| 278 | 0 |
try {
|
| 279 | 0 |
session = hibernateService.openSession(); |
| 280 | 0 |
list = session.find(query, value, type); |
| 281 | 0 |
hibernateService.commit(); |
| 282 |
} catch (Exception e) {
|
|
| 283 | 0 |
hibernateService.rollback(); |
| 284 | 0 |
throw new DAOException(e); |
| 285 |
} finally {
|
|
| 286 | 0 |
session = null;
|
| 287 | 0 |
hibernateService.closeSession(); |
| 288 |
} |
|
| 289 | 0 |
return list;
|
| 290 |
} |
|
| 291 |
|
|
| 292 |
/**
|
|
| 293 |
* Execute a query with bind parameters. Binding an array of values to "?"
|
|
| 294 |
* parameters in the query string.
|
|
| 295 |
*
|
|
| 296 |
* @param query
|
|
| 297 |
* @param values
|
|
| 298 |
* @param types
|
|
| 299 |
* @return a distinct list of instances
|
|
| 300 |
* @throws HibernateException
|
|
| 301 |
* @throws DAOException
|
|
| 302 |
*/
|
|
| 303 | 0 |
public List find(String query, Object[] values, Type[] types)
|
| 304 |
throws HibernateException, DAOException {
|
|
| 305 | 0 |
List list; |
| 306 | 0 |
Session session; |
| 307 | 0 |
try {
|
| 308 | 0 |
session = hibernateService.openSession(); |
| 309 | 0 |
list = session.find(query, values, types); |
| 310 | 0 |
hibernateService.commit(); |
| 311 |
} catch (Exception e) {
|
|
| 312 | 0 |
hibernateService.rollback(); |
| 313 | 0 |
throw new DAOException(e); |
| 314 |
} finally {
|
|
| 315 | 0 |
session = null;
|
| 316 | 0 |
hibernateService.closeSession(); |
| 317 |
} |
|
| 318 | 0 |
return list;
|
| 319 |
} |
|
| 320 |
|
|
| 321 |
/**
|
|
| 322 |
* Return the persistent instance of the given entity class with the given
|
|
| 323 |
* identifier, or null if there is no such persistent instance.
|
|
| 324 |
*
|
|
| 325 |
* @param clazz
|
|
| 326 |
* @param id
|
|
| 327 |
* @return a persistent instance or null
|
|
| 328 |
* @throws HibernateException
|
|
| 329 |
* @throws DAOException
|
|
| 330 |
*/
|
|
| 331 | 1 |
public Object get(Class clazz, Serializable id) throws HibernateException, |
| 332 |
DAOException {
|
|
| 333 | 1 |
Object object; |
| 334 | 1 |
Session session; |
| 335 | 1 |
try {
|
| 336 | 1 |
session = hibernateService.openSession(); |
| 337 | 0 |
object = session.get(clazz, id); |
| 338 | 0 |
hibernateService.commit(); |
| 339 |
} catch (Exception e) {
|
|
| 340 | 1 |
hibernateService.rollback(); |
| 341 | 1 |
throw new DAOException(e); |
| 342 |
} finally {
|
|
| 343 | 1 |
session = null;
|
| 344 | 1 |
hibernateService.closeSession(); |
| 345 |
} |
|
| 346 | 0 |
return object;
|
| 347 |
} |
|
| 348 |
|
|
| 349 |
/**
|
|
| 350 |
* Return the persistent instance of the given entity class with the given
|
|
| 351 |
* identifier, or null if there is no such persistent instance. Obtain the
|
|
| 352 |
* specified lock mode if the instance exists.
|
|
| 353 |
*
|
|
| 354 |
* @param clazz
|
|
| 355 |
* @param id
|
|
| 356 |
* @param lockMode
|
|
| 357 |
* @return a persistent instance or null
|
|
| 358 |
* @throws HibernateException
|
|
| 359 |
* @throws DAOException
|
|
| 360 |
*/
|
|
| 361 | 0 |
public Object get(Class clazz, Serializable id, LockMode lockMode)
|
| 362 |
throws HibernateException, DAOException {
|
|
| 363 | 0 |
Object object; |
| 364 | 0 |
Session session; |
| 365 | 0 |
try {
|
| 366 | 0 |
session = hibernateService.openSession(); |
| 367 | 0 |
object = session.get(clazz, id, lockMode); |
| 368 | 0 |
hibernateService.commit(); |
| 369 |
} catch (Exception e) {
|
|
| 370 | 0 |
hibernateService.rollback(); |
| 371 | 0 |
throw new DAOException(e); |
| 372 |
} finally {
|
|
| 373 | 0 |
session = null;
|
| 374 | 0 |
hibernateService.closeSession(); |
| 375 |
} |
|
| 376 | 0 |
return object;
|
| 377 |
} |
|
| 378 |
|
|
| 379 |
/**
|
|
| 380 |
* Execute a query and return the results in an iterator. If the query has
|
|
| 381 |
* multiple return values, values will be returned in an array of type
|
|
| 382 |
* Object[]. Entities returned as results are initialized on demand. The
|
|
| 383 |
* first SQL query returns identifiers only. So iterate() is usually a less
|
|
| 384 |
* efficient way to retrieve objects than find().
|
|
| 385 |
*
|
|
| 386 |
* @param query
|
|
| 387 |
* @return an iterator
|
|
| 388 |
* @throws HibernateException
|
|
| 389 |
* @throws DAOException
|
|
| 390 |
*/
|
|
| 391 | 0 |
public Iterator iterate(String query) throws HibernateException, |
| 392 |
DAOException {
|
|
| 393 | 0 |
Iterator iter; |
| 394 | 0 |
Session session; |
| 395 | 0 |
try {
|
| 396 | 0 |
session = hibernateService.openSession(); |
| 397 | 0 |
iter = session.iterate(query); |
| 398 | 0 |
hibernateService.commit(); |
| 399 |
} catch (Exception e) {
|
|
| 400 | 0 |
hibernateService.rollback(); |
| 401 | 0 |
throw new DAOException(e); |
| 402 |
} finally {
|
|
| 403 | 0 |
session = null;
|
| 404 | 0 |
hibernateService.closeSession(); |
| 405 |
} |
|
| 406 | 0 |
return iter;
|
| 407 |
} |
|
| 408 |
|
|
| 409 |
/**
|
|
| 410 |
* Execute a query and return the results in an iterator. Write the given
|
|
| 411 |
* value to "?" in the query string. If the query has multiple return
|
|
| 412 |
* values, values will be returned in an array of type Object[]. Entities
|
|
| 413 |
* returned as results are initialized on demand. The first SQL query
|
|
| 414 |
* returns identifiers only. So iterate() is usually a less efficient way to
|
|
| 415 |
* retrieve objects than find().
|
|
| 416 |
*
|
|
| 417 |
* @param query - the query string
|
|
| 418 |
* @param value - a value to be witten to a "?" placeholder in the query
|
|
| 419 |
* string
|
|
| 420 |
* @param type - the hibernate type of value
|
|
| 421 |
* @return an iterator
|
|
| 422 |
* @throws HibernateException
|
|
| 423 |
* @throws DAOException
|
|
| 424 |
*/
|
|
| 425 | 0 |
public Iterator iterate(String query, Object value, Type type)
|
| 426 |
throws HibernateException, DAOException {
|
|
| 427 | 0 |
Iterator iter; |
| 428 | 0 |
Session session; |
| 429 | 0 |
try {
|
| 430 | 0 |
session = hibernateService.openSession(); |
| 431 | 0 |
iter = session.iterate(query, value, type); |
| 432 | 0 |
hibernateService.commit(); |
| 433 |
} catch (Exception e) {
|
|
| 434 | 0 |
hibernateService.rollback(); |
| 435 | 0 |
throw new DAOException(e); |
| 436 |
} finally {
|
|
| 437 | 0 |
session = null;
|
| 438 | 0 |
hibernateService.closeSession(); |
| 439 |
} |
|
| 440 | 0 |
return iter;
|
| 441 |
} |
|
| 442 |
|
|
| 443 |
/**
|
|
| 444 |
* Execute a query and return the results in an iterator. Write the given
|
|
| 445 |
* values to "?" in the query string. If the query has multiple return
|
|
| 446 |
* values, values will be returned in an array of type Object[]. Entities
|
|
| 447 |
* returned as results are initialized on demand. The first SQL query
|
|
| 448 |
* returns identifiers only. So iterate() is usually a less efficient way to
|
|
| 449 |
* retrieve objects than find().
|
|
| 450 |
*
|
|
| 451 |
* @param query
|
|
| 452 |
* @param values
|
|
| 453 |
* @param types
|
|
| 454 |
* @return
|
|
| 455 |
* @throws HibernateException
|
|
| 456 |
* @throws DAOException
|
|
| 457 |
*/
|
|
| 458 | 0 |
public Iterator iterate(String query, Object[] values, Type[] types)
|
| 459 |
throws HibernateException, DAOException {
|
|
| 460 | 0 |
Iterator iter; |
| 461 | 0 |
Session session; |
| 462 | 0 |
try {
|
| 463 | 0 |
session = hibernateService.openSession(); |
| 464 | 0 |
iter = session.iterate(query, values, types); |
| 465 | 0 |
hibernateService.commit(); |
| 466 |
} catch (Exception e) {
|
|
| 467 | 0 |
hibernateService.rollback(); |
| 468 | 0 |
throw new DAOException(e); |
| 469 |
} finally {
|
|
| 470 | 0 |
session = null;
|
| 471 | 0 |
hibernateService.closeSession(); |
| 472 |
} |
|
| 473 | 0 |
return iter;
|
| 474 |
} |
|
| 475 |
|
|
| 476 |
/**
|
|
| 477 |
* Return the persistent instance of the given entity class with the given
|
|
| 478 |
* identifier, assuming that the instance exists. You should not use this
|
|
| 479 |
* method to determine if an instance exists (use find() instead). Use this
|
|
| 480 |
* only to retrieve an instance that you assume exists, where non-existence
|
|
| 481 |
* would be an actual error.
|
|
| 482 |
*
|
|
| 483 |
* @param clazz
|
|
| 484 |
* @param id
|
|
| 485 |
* @return the persistent instance or proxy
|
|
| 486 |
* @throws HibernateException
|
|
| 487 |
* @throws DAOException
|
|
| 488 |
*/
|
|
| 489 | 0 |
public Object load(Class clazz, Serializable id) throws HibernateException, |
| 490 |
DAOException {
|
|
| 491 | 0 |
Object object; |
| 492 | 0 |
Session session; |
| 493 | 0 |
try {
|
| 494 | 0 |
session = hibernateService.openSession(); |
| 495 | 0 |
object = session.load(clazz, id); |
| 496 | 0 |
hibernateService.commit(); |
| 497 |
} catch (Exception e) {
|
|
| 498 | 0 |
hibernateService.rollback(); |
| 499 | 0 |
throw new DAOException(e); |
| 500 |
} finally {
|
|
| 501 | 0 |
session = null;
|
| 502 | 0 |
hibernateService.closeSession(); |
| 503 |
} |
|
| 504 | 0 |
return object;
|
| 505 |
} |
|
| 506 |
|
|
| 507 |
/**
|
|
| 508 |
* Return the persistent instance of the given entity class with the given
|
|
| 509 |
* identifier, obtaining the specified lock mode, assuming the instance
|
|
| 510 |
* exists.
|
|
| 511 |
*
|
|
| 512 |
* @param clazz
|
|
| 513 |
* @param id
|
|
| 514 |
* @param lockMode
|
|
| 515 |
* @return the persistent instance or proxy
|
|
| 516 |
* @throws HibernateException
|
|
| 517 |
* @throws DAOException
|
|
| 518 |
*/
|
|
| 519 | 0 |
public Object load(Class clazz, Serializable id, LockMode lockMode)
|
| 520 |
throws HibernateException, DAOException {
|
|
| 521 | 0 |
Object object; |
| 522 | 0 |
Session session; |
| 523 | 0 |
try {
|
| 524 | 0 |
session = hibernateService.openSession(); |
| 525 | 0 |
object = session.load(clazz, id, lockMode); |
| 526 | 0 |
hibernateService.commit(); |
| 527 |
} catch (Exception e) {
|
|
| 528 | 0 |
hibernateService.rollback(); |
| 529 | 0 |
throw new DAOException(e); |
| 530 |
} finally {
|
|
| 531 | 0 |
session = null;
|
| 532 | 0 |
hibernateService.closeSession(); |
| 533 |
} |
|
| 534 | 0 |
return object;
|
| 535 |
} |
|
| 536 |
|
|
| 537 |
/**
|
|
| 538 |
* Read the persistent state associated with the given identifier into the
|
|
| 539 |
* given transient instance.
|
|
| 540 |
*
|
|
| 541 |
* @param object
|
|
| 542 |
* @param id
|
|
| 543 |
* @throws HibernateException
|
|
| 544 |
* @throws DAOException
|
|
| 545 |
*/
|
|
| 546 | 0 |
public void load(Object object, Serializable id) throws HibernateException, |
| 547 |
DAOException {
|
|
| 548 | 0 |
Session session; |
| 549 | 0 |
try {
|
| 550 | 0 |
session = hibernateService.openSession(); |
| 551 | 0 |
session.load(object, id); |
| 552 | 0 |
hibernateService.commit(); |
| 553 |
} catch (Exception e) {
|
|
| 554 | 0 |
hibernateService.rollback(); |
| 555 | 0 |
throw new DAOException(e); |
| 556 |
} finally {
|
|
| 557 | 0 |
session = null;
|
| 558 | 0 |
hibernateService.closeSession(); |
| 559 |
} |
|
| 560 |
} |
|
| 561 |
|
|
| 562 |
/**
|
|
| 563 |
* Obtain the specified lock level upon the given object.
|
|
| 564 |
*
|
|
| 565 |
* @param object - a persistent or transient instance
|
|
| 566 |
* @param lockMode - the lock level
|
|
| 567 |
* @throws HibernateException
|
|
| 568 |
* @throws DAOException
|
|
| 569 |
*/
|
|
| 570 | 0 |
public void lock(Object object, LockMode lockMode) |
| 571 |
throws HibernateException, DAOException {
|
|
| 572 | 0 |
Session session; |
| 573 | 0 |
try {
|
| 574 | 0 |
session = hibernateService.openSession(); |
| 575 | 0 |
session.lock(object, lockMode); |
| 576 | 0 |
hibernateService.commit(); |
| 577 |
} catch (Exception e) {
|
|
| 578 | 0 |
hibernateService.rollback(); |
| 579 | 0 |
throw new DAOException(e); |
| 580 |
} finally {
|
|
| 581 | 0 |
session = null;
|
| 582 | 0 |
hibernateService.closeSession(); |
| 583 |
} |
|
| 584 |
} |
|
| 585 |
|
|
| 586 |
/**
|
|
| 587 |
* Update the persistent instance with the identifier of the given transient
|
|
| 588 |
* instance. If there is a persistent instance with the same identifier, an
|
|
| 589 |
* exception is thrown. If the given transient instance has a null
|
|
| 590 |
* identifier, an exception will be thrown.
|
|
| 591 |
*
|
|
| 592 |
* @param object - a transient instance containing updated state
|
|
| 593 |
* @throws HibernateException
|
|
| 594 |
* @throws DAOException
|
|
| 595 |
*/
|
|
| 596 | 0 |
public void update(Object object) throws HibernateException, DAOException { |
| 597 | 0 |
Session session; |
| 598 | 0 |
try {
|
| 599 | 0 |
session = hibernateService.openSession(); |
| 600 | 0 |
session.update(object); |
| 601 | 0 |
hibernateService.commit(); |
| 602 |
} catch (Exception e) {
|
|
| 603 | 0 |
hibernateService.rollback(); |
| 604 | 0 |
throw new DAOException(e); |
| 605 |
} finally {
|
|
| 606 | 0 |
session = null;
|
| 607 | 0 |
hibernateService.closeSession(); |
| 608 |
} |
|
| 609 |
} |
|
| 610 |
|
|
| 611 |
/**
|
|
| 612 |
* Update the persistent state associated with the given identifier. An
|
|
| 613 |
* exception is thrown if there is a persistent instance with the same
|
|
| 614 |
* identifier in the current session.
|
|
| 615 |
*
|
|
| 616 |
* @param object - a transient instance containing updated state
|
|
| 617 |
* @param id - identifier of persistent instance
|
|
| 618 |
* @throws HibernateException
|
|
| 619 |
* @throws DAOException
|
|
| 620 |
*/
|
|
| 621 | 0 |
public void update(Object object, Serializable id) |
| 622 |
throws HibernateException, DAOException {
|
|
| 623 | 0 |
Session session; |
| 624 | 0 |
try {
|
| 625 | 0 |
session = hibernateService.openSession(); |
| 626 | 0 |
session.update(object, id); |
| 627 | 0 |
hibernateService.commit(); |
| 628 |
} catch (Exception e) {
|
|
| 629 | 0 |
hibernateService.rollback(); |
| 630 | 0 |
throw new DAOException(e); |
| 631 |
} finally {
|
|
| 632 | 0 |
session = null;
|
| 633 | 0 |
hibernateService.closeSession(); |
| 634 |
} |
|
| 635 |
} |
|
| 636 |
|
|
| 637 |
/**
|
|
| 638 |
* Copy the state of the given object onto the persistent object with the
|
|
| 639 |
* same identifier. If there is no persistent instance currently associated
|
|
| 640 |
* with the session, it will be loaded. Return the persistent instance. If
|
|
| 641 |
* the given instance is unsaved or does not exist in the database, save it
|
|
| 642 |
* and return it as a newly persistent instance. Otherwise, the given
|
|
| 643 |
* instance does not become associated with the session.
|
|
| 644 |
*
|
|
| 645 |
* @param object
|
|
| 646 |
* @return an updated persistent instance
|
|
| 647 |
* @throws HibernateException
|
|
| 648 |
* @throws DAOException
|
|
| 649 |
*/
|
|
| 650 | 0 |
public Object saveOrUpdateCopy(Object object) throws HibernateException, |
| 651 |
DAOException {
|
|
| 652 | 0 |
Session session; |
| 653 | 0 |
Object obj; |
| 654 | 0 |
try {
|
| 655 | 0 |
session = hibernateService.openSession(); |
| 656 | 0 |
obj = session.saveOrUpdateCopy(object); |
| 657 | 0 |
hibernateService.commit(); |
| 658 |
} catch (Exception e) {
|
|
| 659 | 0 |
hibernateService.rollback(); |
| 660 | 0 |
throw new DAOException(e); |
| 661 |
} finally {
|
|
| 662 | 0 |
session = null;
|
| 663 | 0 |
hibernateService.closeSession(); |
| 664 |
} |
|
| 665 | 0 |
return obj;
|
| 666 |
} |
|
| 667 |
|
|
| 668 |
/**
|
|
| 669 |
* Copy the state of the given object onto the persistent object with the
|
|
| 670 |
* given identifier. If there is no persistent instance currently associated
|
|
| 671 |
* with the session, it will be loaded. Return the persistent instance. If
|
|
| 672 |
* there is no database row with the given identifier, save the given
|
|
| 673 |
* instance and return it as a newly persistent instance. Otherwise, the
|
|
| 674 |
* given instance does not become associated with the session.
|
|
| 675 |
*
|
|
| 676 |
* @param object - a persistent or transient instance with state to be
|
|
| 677 |
* copied
|
|
| 678 |
* @param id - the identifier of the instance to copy to
|
|
| 679 |
* @return an updated persistent instance
|
|
| 680 |
* @throws HibernateException
|
|
| 681 |
* @throws DAOException
|
|
| 682 |
*/
|
|
| 683 | 0 |
public Object saveOrUpdateCopy(Object object, Serializable id)
|
| 684 |
throws HibernateException, DAOException {
|
|
| 685 | 0 |
Session session; |
| 686 | 0 |
Object obj; |
| 687 | 0 |
try {
|
| 688 | 0 |
session = hibernateService.openSession(); |
| 689 | 0 |
obj = session.saveOrUpdateCopy(object, id); |
| 690 | 0 |
hibernateService.commit(); |
| 691 |
} catch (Exception e) {
|
|
| 692 | 0 |
hibernateService.rollback(); |
| 693 | 0 |
throw new DAOException(e); |
| 694 |
} finally {
|
|
| 695 | 0 |
session = null;
|
| 696 | 0 |
hibernateService.closeSession(); |
| 697 |
} |
|
| 698 | 0 |
return obj;
|
| 699 |
} |
|
| 700 |
|
|
| 701 |
/**
|
|
| 702 |
* Re-read the state of the given instance from the underlying database. It
|
|
| 703 |
* is inadvisable to use this to implement long-running sessions that span
|
|
| 704 |
* many business tasks. This method is, however, useful in certain special
|
|
| 705 |
* circumstances.
|
|
| 706 |
*
|
|
| 707 |
* @param object a persistent or transient instance
|
|
| 708 |
* @throws HibernateException
|
|
| 709 |
* @throws DAOException
|
|
| 710 |
*/
|
|
| 711 | 0 |
public void refresh(Object object) throws HibernateException, DAOException { |
| 712 | 0 |
Session session; |
| 713 | 0 |
try {
|
| 714 | 0 |
session = hibernateService.openSession(); |
| 715 | 0 |
session.refresh(object); |
| 716 | 0 |
hibernateService.commit(); |
| 717 |
} catch (Exception e) {
|
|
| 718 | 0 |
hibernateService.rollback(); |
| 719 | 0 |
throw new DAOException(e); |
| 720 |
} finally {
|
|
| 721 | 0 |
session = null;
|
| 722 | 0 |
hibernateService.closeSession(); |
| 723 |
} |
|
| 724 |
} |
|
| 725 |
|
|
| 726 |
/**
|
|
| 727 |
* Re-read the state of the given instance from the underlying database,
|
|
| 728 |
* with the given LockMode. It is inadvisable to use this to implement
|
|
| 729 |
* long-running sessions that span many business tasks. This method is,
|
|
| 730 |
* however, useful in certain special circumstances.
|
|
| 731 |
*
|
|
| 732 |
* @param object a persistent or transient instance
|
|
| 733 |
* @param lockMode the lock mode to use
|
|
| 734 |
* @throws HibernateException
|
|
| 735 |
* @throws DAOException
|
|
| 736 |
*/
|
|
| 737 | 0 |
public void refresh(Object object, LockMode lockMode) |
| 738 |
throws HibernateException, DAOException {
|
|
| 739 | 0 |
Session session; |
| 740 | 0 |
try {
|
| 741 | 0 |
session = hibernateService.openSession(); |
| 742 | 0 |
session.refresh(object, lockMode); |
| 743 | 0 |
hibernateService.commit(); |
| 744 |
} catch (Exception e) {
|
|
| 745 | 0 |
hibernateService.rollback(); |
| 746 | 0 |
throw new DAOException(e); |
| 747 |
} finally {
|
|
| 748 | 0 |
session = null;
|
| 749 | 0 |
hibernateService.closeSession(); |
| 750 |
} |
|
| 751 |
} |
|
| 752 |
|
|
| 753 |
/**
|
|
| 754 |
* Persist the given transient instance, first assigning a generated
|
|
| 755 |
* identifier.
|
|
| 756 |
*
|
|
| 757 |
* @param object - - a transient instance of a persistent class
|
|
| 758 |
* @return the generated identifier
|
|
| 759 |
* @throws HibernateException
|
|
| 760 |
*/
|
|
| 761 | 0 |
public Serializable save(Object object) throws HibernateException, |
| 762 |
DAOException {
|
|
| 763 | 0 |
Serializable seriaObj; |
| 764 | 0 |
Session session; |
| 765 | 0 |
try {
|
| 766 | 0 |
session = hibernateService.openSession(); |
| 767 | 0 |
seriaObj = session.save(object); |
| 768 | 0 |
hibernateService.commit(); |
| 769 |
} catch (Exception e) {
|
|
| 770 | 0 |
hibernateService.rollback(); |
| 771 | 0 |
throw new DAOException(e); |
| 772 |
} finally {
|
|
| 773 | 0 |
session = null;
|
| 774 | 0 |
hibernateService.closeSession(); |
| 775 |
} |
|
| 776 | 0 |
return seriaObj;
|
| 777 |
} |
|
| 778 |
|
|
| 779 |
/**
|
|
| 780 |
* Persist the given transient instance, using the given identifier.
|
|
| 781 |
*
|
|
| 782 |
* @param object - a transient instance of a persistent class
|
|
| 783 |
* @param id - an unused valid identifier
|
|
| 784 |
* @throws HibernateException
|
|
| 785 |
* @throws DAOException
|
|
| 786 |
*/
|
|
| 787 | 0 |
public void save(Object object, Serializable id) throws HibernateException, |
| 788 |
DAOException {
|
|
| 789 | 0 |
Session session; |
| 790 | 0 |
try {
|
| 791 | 0 |
session = hibernateService.openSession(); |
| 792 | 0 |
session.save(object, id); |
| 793 | 0 |
hibernateService.commit(); |
| 794 |
} catch (Exception e) {
|
|
| 795 | 0 |
hibernateService.rollback(); |
| 796 | 0 |
throw new DAOException(e); |
| 797 |
} finally {
|
|
| 798 | 0 |
System.out.println("save close session");
|
| 799 | 0 |
session = null;
|
| 800 | 0 |
hibernateService.closeSession(); |
| 801 |
} |
|
| 802 |
} |
|
| 803 |
|
|
| 804 |
/**
|
|
| 805 |
* Either save() or update() the given instance, depending upon the value of
|
|
| 806 |
* its identifier property. By default the instance is always saved. This
|
|
| 807 |
* behaviour may be adjusted by specifying an unsaved-value attribute of the
|
|
| 808 |
* identifier property mapping.
|
|
| 809 |
*
|
|
| 810 |
* @param object - a transient instance containing new or updated state
|
|
| 811 |
* @throws HibernateException
|
|
| 812 |
* @throws DAOException
|
|
| 813 |
*/
|
|
| 814 | 2 |
public void saveOrUpdate(Object object) throws HibernateException, |
| 815 |
DAOException {
|
|
| 816 | 2 |
Session session; |
| 817 | 2 |
try {
|
| 818 | 2 |
session = hibernateService.openSession(); |
| 819 | 0 |
session.saveOrUpdate(object); |
| 820 | 0 |
hibernateService.commit(); |
| 821 |
} catch (Exception e) {
|
|
| 822 | 2 |
hibernateService.rollback(); |
| 823 | 2 |
throw new DAOException(e); |
| 824 |
} finally {
|
|
| 825 | 2 |
session = null;
|
| 826 | 2 |
hibernateService.closeSession(); |
| 827 |
} |
|
| 828 |
} |
|
| 829 |
|
|
| 830 |
} |
|
||||||||||