?? hibernatetemplate.java
字號:
/**
* Return the maximum number of rows specified for this HibernateTemplate.
*/
public int getMaxResults() {
return this.maxResults;
}
public Object execute(HibernateCallback action) throws DataAccessException {
return doExecute(action, false, false);
}
public List executeFind(HibernateCallback action) throws DataAccessException {
Object result = doExecute(action, false, false);
if (result != null && !(result instanceof List)) {
throw new InvalidDataAccessApiUsageException(
"Result object returned from HibernateCallback isn't a List: [" + result + "]");
}
return (List) result;
}
/**
* Execute the action specified by the given action object within a
* new {@link org.hibernate.Session}.
* <p>This execute variant overrides the template-wide
* {@link #isAlwaysUseNewSession() "alwaysUseNewSession"} setting.
* @param action callback object that specifies the Hibernate action
* @return a result object returned by the action, or <code>null</code>
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
*/
public Object executeWithNewSession(HibernateCallback action) {
return doExecute(action, true, false);
}
/**
* Execute the action specified by the given action object within a
* native {@link org.hibernate.Session}.
* <p>This execute variant overrides the template-wide
* {@link #isExposeNativeSession() "exposeNativeSession"} setting.
* @param action callback object that specifies the Hibernate action
* @return a result object returned by the action, or <code>null</code>
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
*/
public Object executeWithNativeSession(HibernateCallback action) {
return doExecute(action, false, true);
}
/**
* Execute the action specified by the given action object within a Session.
* @param action callback object that specifies the Hibernate action
* @param enforceNativeSession whether to enforce exposure of the native
* Hibernate Session to callback code
* @return a result object returned by the action, or <code>null</code>
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
* @deprecated as of Spring 2.5, in favor of {@link #executeWithNativeSession}
*/
public Object execute(HibernateCallback action, boolean enforceNativeSession) throws DataAccessException {
return doExecute(action, false, enforceNativeSession);
}
/**
* Execute the action specified by the given action object within a Session.
* @param action callback object that specifies the Hibernate action
* @param enforceNewSession whether to enforce a new Session for this template
* even if there is a pre-bound transactional Session
* @param enforceNativeSession whether to enforce exposure of the native
* Hibernate Session to callback code
* @return a result object returned by the action, or <code>null</code>
* @throws org.springframework.dao.DataAccessException in case of Hibernate errors
*/
protected Object doExecute(HibernateCallback action, boolean enforceNewSession, boolean enforceNativeSession)
throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Session session = (enforceNewSession ?
SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()) : getSession());
boolean existingTransaction = (!enforceNewSession &&
(!isAllowCreate() || SessionFactoryUtils.isSessionTransactional(session, getSessionFactory())));
if (existingTransaction) {
logger.debug("Found thread-bound Session for HibernateTemplate");
}
FlushMode previousFlushMode = null;
try {
previousFlushMode = applyFlushMode(session, existingTransaction);
enableFilters(session);
Session sessionToExpose =
(enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session));
Object result = action.doInHibernate(sessionToExpose);
flushIfNecessary(session, existingTransaction);
return result;
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
catch (SQLException ex) {
throw convertJdbcAccessException(ex);
}
catch (RuntimeException ex) {
// Callback code threw application exception...
throw ex;
}
finally {
if (existingTransaction) {
logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
disableFilters(session);
if (previousFlushMode != null) {
session.setFlushMode(previousFlushMode);
}
}
else {
// Never use deferred close for an explicitly new Session.
if (isAlwaysUseNewSession()) {
SessionFactoryUtils.closeSession(session);
}
else {
SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
}
}
}
}
/**
* Return a Session for use by this template.
* <p>Returns a new Session in case of "alwaysUseNewSession" (using the same
* JDBC Connection as a transactional Session, if applicable), a pre-bound
* Session in case of "allowCreate" turned off, and a pre-bound or new Session
* otherwise (new only if no transactional or otherwise pre-bound Session exists).
* @return the Session to use (never <code>null</code>)
* @see SessionFactoryUtils#getSession
* @see SessionFactoryUtils#getNewSession
* @see #setAlwaysUseNewSession
* @see #setAllowCreate
*/
protected Session getSession() {
if (isAlwaysUseNewSession()) {
return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor());
}
else if (isAllowCreate()) {
return SessionFactoryUtils.getSession(
getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
}
else {
try {
return getSessionFactory().getCurrentSession();
}
catch (HibernateException ex) {
throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex);
}
}
}
/**
* Create a close-suppressing proxy for the given Hibernate Session.
* The proxy also prepares returned Query and Criteria objects.
* @param session the Hibernate Session to create a proxy for
* @return the Session proxy
* @see org.hibernate.Session#close()
* @see #prepareQuery
* @see #prepareCriteria
*/
protected Session createSessionProxy(Session session) {
Class[] sessionIfcs = null;
Class mainIfc = (session instanceof org.hibernate.classic.Session ?
org.hibernate.classic.Session.class : Session.class);
if (session instanceof EventSource) {
sessionIfcs = new Class[] {mainIfc, EventSource.class};
}
else if (session instanceof SessionImplementor) {
sessionIfcs = new Class[] {mainIfc, SessionImplementor.class};
}
else {
sessionIfcs = new Class[] {mainIfc};
}
return (Session) Proxy.newProxyInstance(
getClass().getClassLoader(), sessionIfcs,
new CloseSuppressingInvocationHandler(session));
}
//-------------------------------------------------------------------------
// Convenience methods for loading individual objects
//-------------------------------------------------------------------------
public Object get(Class entityClass, Serializable id) throws DataAccessException {
return get(entityClass, id, null);
}
public Object get(final Class entityClass, final Serializable id, final LockMode lockMode)
throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
if (lockMode != null) {
return session.get(entityClass, id, lockMode);
}
else {
return session.get(entityClass, id);
}
}
});
}
public Object get(String entityName, Serializable id) throws DataAccessException {
return get(entityName, id, null);
}
public Object get(final String entityName, final Serializable id, final LockMode lockMode)
throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
if (lockMode != null) {
return session.get(entityName, id, lockMode);
}
else {
return session.get(entityName, id);
}
}
});
}
public Object load(Class entityClass, Serializable id) throws DataAccessException {
return load(entityClass, id, null);
}
public Object load(final Class entityClass, final Serializable id, final LockMode lockMode)
throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
if (lockMode != null) {
return session.load(entityClass, id, lockMode);
}
else {
return session.load(entityClass, id);
}
}
});
}
public Object load(String entityName, Serializable id) throws DataAccessException {
return load(entityName, id, null);
}
public Object load(final String entityName, final Serializable id, final LockMode lockMode)
throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
if (lockMode != null) {
return session.load(entityName, id, lockMode);
}
else {
return session.load(entityName, id);
}
}
});
}
public List loadAll(final Class entityClass) throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = session.createCriteria(entityClass);
prepareCriteria(criteria);
return criteria.list();
}
});
}
public void load(final Object entity, final Serializable id) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.load(entity, id);
return null;
}
});
}
public void refresh(final Object entity) throws DataAccessException {
refresh(entity, null);
}
public void refresh(final Object entity, final LockMode lockMode) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
if (lockMode != null) {
session.refresh(entity, lockMode);
}
else {
session.refresh(entity);
}
return null;
}
});
}
public boolean contains(final Object entity) throws DataAccessException {
Boolean result = (Boolean) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) {
return (session.contains(entity) ? Boolean.TRUE : Boolean.FALSE);
}
});
return result.booleanValue();
}
public void evict(final Object entity) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.evict(entity);
return null;
}
});
}
public void initialize(Object proxy) throws DataAccessException {
try {
Hibernate.initialize(proxy);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
public Filter enableFilter(String filterName) throws IllegalStateException {
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -