找不到spring-hibernate.jar的源代码,所以请教一下大家
这个方法里传的Object[]应该是hql语句中占位符的参数了吧,这里是不是任何类型都可以赋值进去,尤其包括日期时间类型,Date,TimeStamp等
注:我知道Query接口里的setParameter(String, Object),如果是日期类型就要指定Hibernate的Type
[b]问题补充:[/b]
setParameter里如果参数是Date、TimeStamp等类型,要指定Hibernate的Type吧,既然find里就是用的query.setParameter实现的,那它是不是不完善啊
[b]问题补充:[/b]
拜托请先看我问题补充,有的参数必须要指定类型,比如日期或时间的,因为有很多种类型sql.Date,util.Date,TimeStamp等
[b]问题补充:[/b]
lovewhzlq 我真服了你了 说点有用的行吗
没看人家楼上的已经把源码给出来了吗 不用你往这充数来 最鄙视这种人
queryObject.setParameter(i, values[i]);
这个类型并不会映射到数据库类型而是在Hibernate中的POJO的类型。
如果底层做好了映射,这个Parameter可以是任意类型。
这个方法里传的Object[]是hql语句中占位符的参数对应的实际值,
任务类型都可以的
这个是反编译的,难看了点,不过看得出来,是根据数组的位置来填充参数,参数的类型不定
[code="java"]public List find(final String queryString, final Object values[])
throws DataAccessException
{
return (List)executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException
{
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if(values != null)
{
for(int i = 0; i < values.length; i++)
queryObject.setParameter(i, values[i]);
}
return queryObject.list();
}
{
super();
}
}
);
}[/code]
不是非要指定参数的类型的,还有一个setObject的方法啊,运行时能动态识别出具体类型的
源码给你,自己研究下吧
[code="java"]
/*
package org.springframework.orm.hibernate3;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.ReplicationMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.event.EventSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.util.Assert;
/**
org.springframework.dao
exception hierarchy. *The central method is execute
, supporting Hibernate access code
Can be used within a service implementation via direct instantiation
NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can
This class can be considered as direct alternative to working with the raw
SessionFactory.getCurrentSession()
).{@link LocalSessionFactoryBean} is the preferred way of obtaining a reference
Note that operations that return an Iterator (i.e. iterate
)
Lazy loading will also just work with an open Hibernate Session,
contains
, evict
, lock
,flush
, clear
. *@see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
*/
public class HibernateTemplate extends HibernateAccessor implements HibernateOperations {
private boolean allowCreate = true;
private boolean alwaysUseNewSession = false;
private boolean exposeNativeSession = false;
private boolean checkWriteOperations = true;
private boolean cacheQueries = false;
private String queryCacheRegion;
private int fetchSize = 0;
private int maxResults = 0;
/**
/**
/**
/**
Session
can be found for the current thread.true
.HibernateTemplate
is aware of a corresponding
Session
bound to the current thread, for example when usingallowCreate
istrue
, a new non-transactional Session
will befalse
, an {@link IllegalStateException} will get thrown inNOTE: As of Spring 2.5, switching allowCreate
false
will delegate to Hibernate'sSessionFactoryUtils.getSession(sessionFactory, false)
.allowCreate
set to true
/**
/**
Within a transaction, a new Hibernate Session used by this template
Turn this on for operations that are supposed to always execute
/**
/**
Default is "false": a Session proxy will be returned, suppressing
close
calls and automatically applying query cache/**
/**
Default is "true", for fail-fast behavior when attempting write operations
/**
/**
If this is "true", all Query and Criteria objects created by
To specify the query region to be used for queries cached
/**
/**
If this is specified, it will be applied to all Query and Criteria objects
The cache region will not take effect unless queries created by this
/**
/**
Default is 0, indicating to use the JDBC driver's default. */ public void setFetchSize(int fetchSize) { this.fetchSize = fetchSize; }
/**
/**
Default is 0, indicating to use the JDBC driver's default. */ public void setMaxResults(int maxResults) { this.maxResults = 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;
}
/**
This execute variant overrides the template-wide
null
/**
This execute variant overrides the template-wide
null
/**
null
/**
null
@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());
}
}
}
}
/**
Returns a new Session in case of "alwaysUseNewSession" (using the same
null
)/**
//-------------------------------------------------------------------------
// 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);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
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);
Filter filter = session.getEnabledFilter(filterName);
if (filter == null) {
filter = session.enableFilter(filterName);
}
return filter;
}
//-------------------------------------------------------------------------
// Convenience methods for storing individual objects
//-------------------------------------------------------------------------
public void lock(final Object entity, final LockMode lockMode) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.lock(entity, lockMode);
return null;
}
});
}
public void lock(final String entityName, final Object entity, final LockMode lockMode)
throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.lock(entityName, entity, lockMode);
return null;
}
});
}
public Serializable save(final Object entity) throws DataAccessException {
return (Serializable) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
return session.save(entity);
}
});
}
public Serializable save(final String entityName, final Object entity) throws DataAccessException {
return (Serializable) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
return session.save(entityName, entity);
}
});
}
public void update(Object entity) throws DataAccessException {
update(entity, null);
}
public void update(final Object entity, final LockMode lockMode) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.update(entity);
if (lockMode != null) {
session.lock(entity, lockMode);
}
return null;
}
});
}
public void update(String entityName, Object entity) throws DataAccessException {
update(entityName, entity, null);
}
public void update(final String entityName, final Object entity, final LockMode lockMode)
throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.update(entityName, entity);
if (lockMode != null) {
session.lock(entity, lockMode);
}
return null;
}
});
}
public void saveOrUpdate(final Object entity) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.saveOrUpdate(entity);
return null;
}
});
}
public void saveOrUpdate(final String entityName, final Object entity) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.saveOrUpdate(entityName, entity);
return null;
}
});
}
public void saveOrUpdateAll(final Collection entities) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
for (Iterator it = entities.iterator(); it.hasNext();) {
session.saveOrUpdate(it.next());
}
return null;
}
});
}
public void replicate(final Object entity, final ReplicationMode replicationMode)
throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.replicate(entity, replicationMode);
return null;
}
});
}
public void replicate(final String entityName, final Object entity, final ReplicationMode replicationMode)
throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.replicate(entityName, entity, replicationMode);
return null;
}
});
}
public void persist(final Object entity) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.persist(entity);
return null;
}
});
}
public void persist(final String entityName, final Object entity) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
session.persist(entityName, entity);
return null;
}
});
}
public Object merge(final Object entity) throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
return session.merge(entity);
}
});
}
public Object merge(final String entityName, final Object entity) throws DataAccessException {
return executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
return session.merge(entityName, entity);
}
});
}
public void delete(Object entity) throws DataAccessException {
delete(entity, null);
}
public void delete(final Object entity, final LockMode lockMode) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
if (lockMode != null) {
session.lock(entity, lockMode);
}
session.delete(entity);
return null;
}
});
}
public void delete(String entityName, Object entity) throws DataAccessException {
delete(entityName, entity, null);
}
public void delete(final String entityName, final Object entity, final LockMode lockMode)
throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
if (lockMode != null) {
session.lock(entityName, entity, lockMode);
}
session.delete(entityName, entity);
return null;
}
});
}
public void deleteAll(final Collection entities) throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
for (Iterator it = entities.iterator(); it.hasNext();) {
session.delete(it.next());
}
return null;
}
});
}
public void flush() throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.flush();
return null;
}
});
}
public void clear() throws DataAccessException {
executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) {
session.clear();
return null;
}
});
}
//-------------------------------------------------------------------------
// Convenience finder methods for HQL strings
//-------------------------------------------------------------------------
public List find(String queryString) throws DataAccessException {
return find(queryString, (Object[]) null);
}
public List find(String queryString, Object value) throws DataAccessException {
return find(queryString, new Object[] {value});
}
public List find(final String queryString, final Object[] values) throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}
});
}
public List findByNamedParam(String queryString, String paramName, Object value)
throws DataAccessException {
return findByNamedParam(queryString, new String[] {paramName}, new Object[] {value});
}
public List findByNamedParam(final String queryString, final String[] paramNames, final Object[] values)
throws DataAccessException {
if (paramNames.length != values.length) {
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
}
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
}
}
return queryObject.list();
}
});
}
public List findByValueBean(final String queryString, final Object valueBean)
throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
queryObject.setProperties(valueBean);
return queryObject.list();
}
});
}
//-------------------------------------------------------------------------
// Convenience finder methods for named queries
//-------------------------------------------------------------------------
public List findByNamedQuery(String queryName) throws DataAccessException {
return findByNamedQuery(queryName, (Object[]) null);
}
public List findByNamedQuery(String queryName, Object value) throws DataAccessException {
return findByNamedQuery(queryName, new Object[] {value});
}
public List findByNamedQuery(final String queryName, final Object[] values) throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.getNamedQuery(queryName);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}
});
}
public List findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
throws DataAccessException {
return findByNamedQueryAndNamedParam(queryName, new String[] {paramName}, new Object[] {value});
}
public List findByNamedQueryAndNamedParam(
final String queryName, final String[] paramNames, final Object[] values)
throws DataAccessException {
if (paramNames != null && values != null && paramNames.length != values.length) {
throw new IllegalArgumentException("Length of paramNames array must match length of values array");
}
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.getNamedQuery(queryName);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
}
}
return queryObject.list();
}
});
}
public List findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.getNamedQuery(queryName);
prepareQuery(queryObject);
queryObject.setProperties(valueBean);
return queryObject.list();
}
});
}
//-------------------------------------------------------------------------
// Convenience finder methods for detached criteria
//-------------------------------------------------------------------------
public List findByCriteria(DetachedCriteria criteria) throws DataAccessException {
return findByCriteria(criteria, -1, -1);
}
public List findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults)
throws DataAccessException {
Assert.notNull(criteria, "DetachedCriteria must not be null");
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria executableCriteria = criteria.getExecutableCriteria(session);
prepareCriteria(executableCriteria);
if (firstResult >= 0) {
executableCriteria.setFirstResult(firstResult);
}
if (maxResults > 0) {
executableCriteria.setMaxResults(maxResults);
}
return executableCriteria.list();
}
});
}
public List findByExample(Object exampleEntity) throws DataAccessException {
return findByExample(null, exampleEntity, -1, -1);
}
public List findByExample(String entityName, Object exampleEntity) throws DataAccessException {
return findByExample(entityName, exampleEntity, -1, -1);
}
public List findByExample(Object exampleEntity, int firstResult, int maxResults) throws DataAccessException {
return findByExample(null, exampleEntity, firstResult, maxResults);
}
public List findByExample(
final String entityName, final Object exampleEntity, final int firstResult, final int maxResults)
throws DataAccessException {
Assert.notNull(exampleEntity, "Example entity must not be null");
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria executableCriteria = (entityName != null ?
session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
executableCriteria.add(Example.create(exampleEntity));
prepareCriteria(executableCriteria);
if (firstResult >= 0) {
executableCriteria.setFirstResult(firstResult);
}
if (maxResults > 0) {
executableCriteria.setMaxResults(maxResults);
}
return executableCriteria.list();
}
});
}
//-------------------------------------------------------------------------
// Convenience query methods for iteration and bulk updates/deletes
//-------------------------------------------------------------------------
public Iterator iterate(String queryString) throws DataAccessException {
return iterate(queryString, (Object[]) null);
}
public Iterator iterate(String queryString, Object value) throws DataAccessException {
return iterate(queryString, new Object[] {value});
}
public Iterator iterate(final String queryString, final Object[] values) throws DataAccessException {
return (Iterator) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.iterate();
}
});
}
public void closeIterator(Iterator it) throws DataAccessException {
try {
Hibernate.close(it);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
public int bulkUpdate(String queryString) throws DataAccessException {
return bulkUpdate(queryString, (Object[]) null);
}
public int bulkUpdate(String queryString, Object value) throws DataAccessException {
return bulkUpdate(queryString, new Object[] {value});
}
public int bulkUpdate(final String queryString, final Object[] values) throws DataAccessException {
Integer updateCount = (Integer) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return new Integer(queryObject.executeUpdate());
}
});
return updateCount.intValue();
}
//-------------------------------------------------------------------------
// Helper methods used by the operations above
//-------------------------------------------------------------------------
/**
Default implementation throws an InvalidDataAccessApiUsageException in
FlushMode.NEVER/MANUAL
. Can be overridden in subclasses./**
/**
/**
@throws HibernateException if thrown by the Query object
*/
protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value)
throws HibernateException {
if (value instanceof Collection) {
queryObject.setParameterList(paramName, (Collection) value);
}
else if (value instanceof Object[]) {
queryObject.setParameterList(paramName, (Object[]) value);
}
else {
queryObject.setParameter(paramName, value);
}
}
/**
@see org.hibernate.Session#close
*/
private class CloseSuppressingInvocationHandler implements InvocationHandler {
private final Session target;
public CloseSuppressingInvocationHandler(Session target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on Session interface coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of Session proxy.
return new Integer(System.identityHashCode(proxy));
}
else if (method.getName().equals("close")) {
// Handle close method: suppress, not valid.
return null;
}
// Invoke method on target Session.
try {
Object retVal = method.invoke(this.target, args);
// If return value is a Query or Criteria, apply transaction timeout.
// Applies to createQuery, getNamedQuery, createCriteria.
if (retVal instanceof Query) {
prepareQuery(((Query) retVal));
}
if (retVal instanceof Criteria) {
prepareCriteria(((Criteria) retVal));
}
return retVal;
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
}
[/code]
[code="java"]
public List find(final String queryString, final Object[] values) throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}
});
}
[/code]