_baserootdao.java
来自「《精通Spring》源代码」· Java 代码 · 共 838 行 · 第 1/2 页
JAVA
838 行
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given and the Session given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter array
* @s the Session
* @return Query
*/
public java.util.List getNamedQuery(String name, Serializable[] params,
Session s) throws HibernateException {
Query q = s.getNamedQuery(name);
if (null != params) {
for (int i = 0; i < params.length; i++) {
setParameterValue(q, i, params[i]);
}
}
return q.list();
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter Map
* @return Query
*/
public java.util.List getNamedQuery(String name, Map params)
throws HibernateException {
Session s = null;
try {
s = getSession();
return getNamedQuery(name, params, s);
} finally {
closeSession();
}
}
/**
* Obtain an instance of Query for a named query string defined in the
* mapping file. Use the parameters given and the Session given.
*
* @param name
* the name of a query defined externally
* @param params
* the parameter Map
* @s the Session
* @return Query
*/
public java.util.List getNamedQuery(String name, Map params, Session s)
throws HibernateException {
Query q = s.getNamedQuery(name);
if (null != params) {
for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
setParameterValue(q, (String) entry.getKey(), entry.getValue());
}
}
return q.list();
}
/**
* Execute a query.
*
* @param query
* a query expressed in Hibernate's query language
* @return a distinct list of instances (or arrays of instances)
*/
public java.util.List find(String query, Object obj, Type type)
throws HibernateException {
Session s = null;
try {
s = getSession();
return find(query, obj, type, s);
} finally {
closeSession();
}
}
/**
* Perform a find but use the session given instead of creating a new one.
*
* @param query
* a query expressed in Hibernate's query language
* @s the Session to use
*/
public java.util.List find(String query, Object obj, Type type, Session s)
throws HibernateException {
return s.find(query, obj, type);
}
/**
* Execute a query.
*
* @param query
* a query expressed in Hibernate's query language
* @return a distinct list of instances (or arrays of instances)
*/
public java.util.List find(String query, Object[] obj, Type[] type)
throws HibernateException {
Session s = null;
try {
s = getSession();
return find(query, obj, type, s);
} finally {
closeSession();
}
}
/**
* Perform a find but use the session given instead of creating a new one.
*
* @param query
* a query expressed in Hibernate's query language
* @s the Session to use
*/
public java.util.List find(String query, Object[] obj, Type[] type,
Session s) throws HibernateException {
return s.find(query, obj, type);
}
/**
* Return a Criteria object that relates to the DAO's table. A session will
* be created if an open one is not located. This session must be closed!
*/
protected Criteria createCriteria() throws HibernateException {
Session s = getSession();
return createCriteria(s);
}
/**
* Return a Criteria object that relates to the DAO's table
*/
protected Criteria createCriteria(Session s) throws HibernateException {
return s.createCriteria(getReferenceClass());
}
/**
* Return the property of the class you would like to use for default
* ordering
*
* @return the property name
*/
public String getDefaultOrderProperty() {
return null;
}
/**
* Convenience method to set paramers in the query given based on the actual
* object type in passed in as the value. You may need to add more
* functionaly to this as desired (or not use this at all).
*
* @param query
* the Query to set
* @param position
* the ordinal position of the current parameter within the query
* @param value
* the object to set as the parameter
*/
protected void setParameterValue(Query query, int position, Object value) {
if (null == value) {
return;
} else if (value instanceof Boolean) {
query.setBoolean(position, ((Boolean) value).booleanValue());
} else if (value instanceof String) {
query.setString(position, (String) value);
} else if (value instanceof Integer) {
query.setInteger(position, ((Integer) value).intValue());
} else if (value instanceof Long) {
query.setLong(position, ((Long) value).longValue());
} else if (value instanceof Float) {
query.setFloat(position, ((Float) value).floatValue());
} else if (value instanceof Double) {
query.setDouble(position, ((Double) value).doubleValue());
} else if (value instanceof BigDecimal) {
query.setBigDecimal(position, (BigDecimal) value);
} else if (value instanceof Byte) {
query.setByte(position, ((Byte) value).byteValue());
} else if (value instanceof Calendar) {
query.setCalendar(position, (Calendar) value);
} else if (value instanceof Character) {
query.setCharacter(position, ((Character) value).charValue());
} else if (value instanceof Timestamp) {
query.setTimestamp(position, (Timestamp) value);
} else if (value instanceof Date) {
query.setDate(position, (Date) value);
} else if (value instanceof Short) {
query.setShort(position, ((Short) value).shortValue());
}
}
/**
* Convenience method to set paramers in the query given based on the actual
* object type in passed in as the value. You may need to add more
* functionaly to this as desired (or not use this at all).
*
* @param query
* the Query to set
* @param key
* the key name
* @param value
* the object to set as the parameter
*/
protected void setParameterValue(Query query, String key, Object value) {
if (null == key || null == value) {
return;
} else if (value instanceof Boolean) {
query.setBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof String) {
query.setString(key, (String) value);
} else if (value instanceof Integer) {
query.setInteger(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
query.setLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
query.setFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
query.setDouble(key, ((Double) value).doubleValue());
} else if (value instanceof BigDecimal) {
query.setBigDecimal(key, (BigDecimal) value);
} else if (value instanceof Byte) {
query.setByte(key, ((Byte) value).byteValue());
} else if (value instanceof Calendar) {
query.setCalendar(key, (Calendar) value);
} else if (value instanceof Character) {
query.setCharacter(key, ((Character) value).charValue());
} else if (value instanceof Timestamp) {
query.setTimestamp(key, (Timestamp) value);
} else if (value instanceof Date) {
query.setDate(key, (Date) value);
} else if (value instanceof Short) {
query.setShort(key, ((Short) value).shortValue());
}
}
/**
* Used by the base DAO classes but here for your modification Load object
* matching the given key and return it.
*/
protected Object load(Class refClass, Serializable key)
throws HibernateException {
Session s = null;
try {
s = getSession();
return load(refClass, key, s);
} finally {
closeSession();
}
}
/**
* Used by the base DAO classes but here for your modification Load object
* matching the given key and return it.
*/
protected Object load(Class refClass, Serializable key, Session s)
throws HibernateException {
return s.load(refClass, key);
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or
* using the current value of the identifier property if the assigned
* generator is used.)
*/
protected Serializable save(Object obj) throws HibernateException {
Transaction t = null;
Session s = null;
try {
s = getSession();
t = beginTransaction(s);
Serializable rtn = save(obj, s);
commitTransaction(t);
return rtn;
} catch (HibernateException e) {
if (null != t)
t.rollback();
throw e;
} finally {
closeSession();
}
}
/**
* Used by the base DAO classes but here for your modification Persist the
* given transient instance, first assigning a generated identifier. (Or
* using the current value of the identifier property if the assigned
* generator is used.)
*/
protected Serializable save(Object obj, Session s)
throws HibernateException {
return s.save(obj);
}
/**
* Used by the base DAO classes but here for your modification Either save()
* or update() the given instance, depending upon the value of its
* identifier property.
*/
protected void saveOrUpdate(Object obj) throws HibernateException {
Transaction t = null;
Session s = null;
try {
s = getSession();
t = beginTransaction(s);
saveOrUpdate(obj, s);
commitTransaction(t);
} catch (HibernateException e) {
if (null != t)
t.rollback();
throw e;
} finally {
closeSession();
}
}
/**
* Used by the base DAO classes but here for your modification Either save()
* or update() the given instance, depending upon the value of its
* identifier property.
*/
protected void saveOrUpdate(Object obj, Session s)
throws HibernateException {
s.saveOrUpdate(obj);
}
/**
* Used by the base DAO classes but here for your modification Update the
* persistent state associated with the given identifier. An exception is
* thrown if there is a persistent instance with the same identifier in the
* current session.
*
* @param obj
* a transient instance containing updated state
*/
protected void update(Object obj) throws HibernateException {
Transaction t = null;
Session s = null;
try {
s = getSession();
t = beginTransaction(s);
update(obj, s);
commitTransaction(t);
} catch (HibernateException e) {
if (null != t)
t.rollback();
throw e;
} finally {
closeSession();
}
}
/**
* Used by the base DAO classes but here for your modification Update the
* persistent state associated with the given identifier. An exception is
* thrown if there is a persistent instance with the same identifier in the
* current session.
*
* @param obj
* a transient instance containing updated state
* @param s
* the Session
*/
protected void update(Object obj, Session s) throws HibernateException {
s.update(obj);
}
/**
* Used by the base DAO classes but here for your modification Remove a
* persistent instance from the datastore. The argument may be an instance
* associated with the receiving Session or a transient instance with an
* identifier associated with existing persistent state.
*/
protected void delete(Object obj) throws HibernateException {
Transaction t = null;
Session s = null;
try {
s = getSession();
t = beginTransaction(s);
delete(obj, s);
commitTransaction(t);
} catch (HibernateException e) {
if (null != t)
t.rollback();
throw e;
} finally {
closeSession();
}
}
/**
* Used by the base DAO classes but here for your modification Remove a
* persistent instance from the datastore. The argument may be an instance
* associated with the receiving Session or a transient instance with an
* identifier associated with existing persistent state.
*/
protected void delete(Object obj, Session s) throws HibernateException {
s.delete(obj);
}
/**
* Used by the base DAO classes but here for your modification Re-read the
* state of the given instance from the underlying database. It is
* inadvisable to use this to implement long-running sessions that span many
* business tasks. This method is, however, useful in certain special
* circumstances.
*/
protected void refresh(Object obj, Session s) throws HibernateException {
s.refresh(obj);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?