- 浏览: 448699 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
g_man1990:
update config 不成功啊
build-helper-maven-plugin 配置多 source resource 文件 -
netwelfare:
文章很详细,就是太长了,读起来有点困难,倒不如写精练点,像这篇 ...
Java 基本类型 -
huyuancai1010:
function commitForm() {
var ...
加时间戳或者随机数去除js缓存 -
Smile__:
不过这个东西以前还真没研究过 。
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size -
Smile__:
想不到你也是北大青鸟的 。哈
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.AbstractQueryImpl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class GenericSpringDAO<T extends AbstractPersistentVO>
extends HibernateDaoSupport
implements GenericDAOIF<T> {
/**
* Definite Logger
*/
private static final Logger LOGGER = Logger.getLogger(GenericSpringDAO.class.getName());
/**
* Definite template class
*/
private Class<T> persistentClass;
/**
* Default constructor of GenericSpringDAO
*/
public GenericSpringDAO(){
super();
}
/**
* constructor
* @param sessionFactory sessionFactory
*/
@SuppressWarnings("unchecked")
public GenericSpringDAO(SessionFactory sessionFactory){
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
setSessionFactory(sessionFactory);
}
public Class<T> getPersistentClass(){
return persistentClass;
}
/**
* findById method
* @param id id
*/
@SuppressWarnings("unchecked")
public T findById(Serializable id){
T t = (T) getSession().get(getPersistentClass(), id);
LOGGER.info("Find by ID for " + getPersistentClass().getSimpleName() + ", using id = '" + id + "', returning: " + ((t==null)?null:t.toString()));
return t;
}
/**
* findAll
*/
public List<T> findAll(){
LOGGER.info("Find all for " + getPersistentClass().getSimpleName() + ".");
List<T> list = findByCriteria();
logListResults("Find all", list);
return list;
}
/**
* findByExample
* @param exampleInstance exampleInstance
* @param excludeProperty excludeProperty
* @return list
*/
@SuppressWarnings("unchecked")
public final List<T> findByExample(T exampleInstance, String... excludeProperty)
{
Criteria c = prepareExampleCriteria(exampleInstance, excludeProperty);
LOGGER.info("Find by example for " + getPersistentClass().getSimpleName() + ", using criteria = " + c.toString() + ".");
List<T> list = c.list();
logListResults("Find by example", list);
return list;
}
/**
* findByExample
* @param exampleInstance exampleInstance
* @param criterion criterion
* @param excludeProperty excludeProperty
* @return list
*/
@SuppressWarnings("unchecked")
public final List<T> findByExample(T exampleInstance,Criterion criterion , String... excludeProperty)
{
Criteria c = prepareExampleCriteria(exampleInstance, excludeProperty);
c.add(criterion);
LOGGER.info("Find by example for " + getPersistentClass().getSimpleName() + ", using criteria = " + c.toString() + ".");
List<T> list = c.list();
logListResults("Find by example", list);
return list;
}
/**
* findUniqueByExample
* @param exampleInstance exampleInstance
* @param excludeProperty excludeProperty
* @return t
*/
@SuppressWarnings("unchecked")
public T findUniqueByExample(T exampleInstance, String... excludeProperty){
Criteria c = prepareExampleCriteria(exampleInstance, excludeProperty);
LOGGER.info("Find Unique by example for " + getPersistentClass().getSimpleName() + ", using criteria = " + c.toString() + ".");
T t = (T)c.uniqueResult();
LOGGER.info("Find Unique by example for " + getPersistentClass().getSimpleName() + ", using example = " + exampleInstance + ", returning: " + ((t==null)?null:t.toString()));
return t;
}
/**
* findUniqueByExample
* @param query sql query
* @return list
*/
@SuppressWarnings("unchecked")
protected final List<T> executeQueryList(SQLQuery query){
StringBuilder params = new StringBuilder();
if (query instanceof AbstractQueryImpl){
params.append(", using params = ");
Object[] values = ((AbstractQueryImpl)query).getQueryParameters(null).getPositionalParameterValues();
for (int i = 0; i < values.length; i++){
if (i!=0){
params.append(", ");
}
params.append(values[i].toString());
}
}
LOGGER.info("Execute Query for " + getPersistentClass().getSimpleName() + ", using query = " + query.getQueryString() + params.toString() + ".");
List<T> result = query.list();
logListResults("Execute Query", result);
return result;
}
/**
* executeQuery
* @param criteria criteria
* @return list
*/
@SuppressWarnings("unchecked")
protected final List<T> executeQuery(Criteria criteria){
LOGGER.info("Execute Query for " + getPersistentClass().getSimpleName() + ", using criteria = " + criteria.toString() + ".");
List<T> result = criteria.list();
logListResults("Execute Query", result);
return result;
}
/**
* prepareBlankCriteria
* @return criteria
*/
protected Criteria prepareBlankCriteria(){
Criteria crit = getSession().createCriteria(getPersistentClass());
return crit;
}
/**
* injectFindByExampleExcludes
* @param vo vo
* @return string[]
*/
protected String[] injectFindByExampleExcludes(T vo){
//override in subclass as needed
return null;
}
/**
* injectFindByExampleCriteria
* @param criteria criteria
* @param vo vo
*/
protected void injectFindByExampleCriteria(Criteria criteria, T vo){
//override in subclass as needed
}
/**
* prepareExampleCriteria
* @param exampleInstance exampleInstance
* @param excludeProperty excludeProperty
* @return criteria
*/
protected Criteria prepareExampleCriteria(T exampleInstance, String... excludeProperty){
String[] excludes = injectFindByExampleExcludes(exampleInstance);
if (excludes != null){
List<String> excludeList = new ArrayList<String>(Arrays.asList(excludes));
if (excludeProperty != null){
Collections.addAll(excludeList, excludeProperty);
}
excludeProperty = new String[excludeList.size()];
excludeProperty = excludeList.toArray(excludeProperty);
}
Criteria crit = prepareBlankCriteria();
Example example = Example.create(exampleInstance);
for (String exclude : excludeProperty){
example.excludeProperty(exclude);
}
crit.add(example);
crit.setMaxResults(1000);
injectFindByExampleCriteria(crit, exampleInstance);
return crit;
}
/**
*
* @param c criteria
* @param propertyName property name
* @param startDate start date
* @param endDate end date
* @return The criteria to allow call chaining
*/
protected Criteria addDateRangeCriteria(Criteria c, String propertyName, Date startDate, Date endDate){
c.add(Restrictions.ge(propertyName, startDate));
c.add(Restrictions.le(propertyName, endDate));
return c;
}
/**
* Use this inside subclasses as a convenience method.
* @param criterion criterion
* @return list
*/
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion){
Criteria crit = getSession().createCriteria(getPersistentClass());
for (Criterion c : criterion){
crit.add(c);
}
return crit.list();
}
/**
* create
* @param entity entity
* @return entity
*/
public T create(T entity){
LOGGER.info("Create for " + getPersistentClass().getSimpleName() + ", using entity = " + entity + ".");
getSession().save(entity);
LOGGER.info("Create for " + getPersistentClass().getSimpleName() + ", returning = " + entity + ".");
return entity;
}
/**
* Persist
* @param entity entity
* @return entity
*/
public T persist(T entity){
LOGGER.info("Persist for " + getPersistentClass().getSimpleName() + ", using entity = " + entity + ".");
getSession().persist(entity);
LOGGER.info("Persist for " + getPersistentClass().getSimpleName() + ", returning = " + entity + ".");
return entity;
}
/**
* update method
* @param entity entity
* @return entity
*/
@SuppressWarnings("unchecked")
public T update(T entity){
LOGGER.info("Update for " + getPersistentClass().getSimpleName() + ", using entity = " + entity + ".");
if (!getSession().contains(entity)){
entity = (T)getSession().merge(entity);
}
else{
getSession().update(entity);
}
LOGGER.info("Update for " + getPersistentClass().getSimpleName() + ", returning = " + entity + ".");
return entity;
}
/**
* delete method
* @param entity entity
*/
public void delete(T entity){
LOGGER.info("Delete for " + getPersistentClass().getSimpleName() + ", using entity = " + entity + ".");
getSession().delete(entity);
}
/**
* detach
* @param entity entity
*/
public void detach(T entity){
LOGGER.info("Detaching entity : " + entity + ".");
getSession().evict(entity);
}
/**
* logListResults
* @param methodName method name
* @param list log list
*/
private void logListResults(String methodName, List<T> list){
//String lineSep = AbstractInitializer.getInitializer().getLineSep();
String lineSep = "/";
List<T> logList = null;
String debugMsg = "";
if (!LOGGER.isDebugEnabled() && list.size() > 10){
logList = list.subList(0, 10);
debugMsg = " Only logging first 10 records.";
}
else{
logList = list;
}
StringBuffer sb = new StringBuffer(logList.size() * 100);
sb.append(debugMsg);
for (T t : logList){
sb.append(lineSep);
sb.append(t.toString());
}
LOGGER.info(methodName + " for " + getPersistentClass().getSimpleName() + ", returning " + list.size() + " records." + sb.toString());
}
}
Daoimpl:
........
/**
* injectFindByExampleCriteria
* @param criteria criteria
* @param countryLanguageVO CountryLanguageVO
*/
@Override
public void injectFindByExampleCriteria(Criteria criteria,
CountryLanguageVO countryLanguageVO) {
if (null != countryLanguageVO.getCountryVO()) {
if (null != countryLanguageVO.getCountryVO().getCountryId()) {
criteria.add(Restrictions.eq("countryVO", countryLanguageVO
.getCountryVO()));
}
else {
Example example = Example.create(countryLanguageVO
.getCountryVO());
criteria.createCriteria("countryVO").add(example);
}
}
if (null != countryLanguageVO.getLanguageVO()) {
if (null != countryLanguageVO.getLanguageVO().getLanguageCd()) {
criteria.add(Restrictions.eq("languageVO", countryLanguageVO
.getLanguageVO()));
}
else {
Example example = Example.create(countryLanguageVO
.getLanguageVO());
criteria.createCriteria("languageVO").add(example);
}
}
}
发表评论
-
Hibernate search
2011-02-21 14:44 3420Hibernate Search是Hibernate的子项目, ... -
SimpleJdbc
2010-05-26 17:21 1925SimpleJdbcInsert类和SimpleJdbcCal ... -
Hibernate 注解 annotation
2010-05-05 20:37 15026一、 实体 Bean 每个持久化POJO类都是一个实体Bea ... -
Hibernate 拦截器 和 监听器
2009-11-25 11:29 1875拦截器(Intercept):顾名思义,拦截操作,也就是在Hi ... -
hibernate.jdbc.fetch_size 和 hibernate.jdbc.batch_size
2009-11-17 17:32 19922hibernate.jdbc.fetch_size 50 h ... -
Hibernate 二级缓存 和 查询缓存
2009-09-22 11:19 3066自己测试的一些结果 , ... -
get 会使用二级缓存
2009-09-03 17:17 1448经常看到session.get()和session.load( ... -
Query.list 与 Query.iterate 的区别
2009-09-03 17:08 1941list: 结果存入缓存,但不从缓存里面取;查询时属性连同id ... -
Jdbc 与 Jta 事务
2009-08-26 14:35 2665hibernate的两种事务管理jdbc 和jta方式。下边说 ... -
Hibernate Session Flush
2009-08-14 11:34 8620session flush在commit之前默 ... -
Hibernate Annotation driven equals and hashCode
2009-07-03 16:56 1470The following implementation of ... -
hibernate cascade inverse
2009-06-28 19:43 1147这两个属性都用于一多 ... -
Hibernate Criteria
2009-06-19 11:15 1064Hibernate 设计了 CriteriaSpec ... -
Hibernate的Criteria 简单用法
2009-05-26 16:52 1800在hibernate的Session里面使用createCri ... -
Hibernate中Criteria的完整用法
2009-05-26 16:46 614Criteria 在查询方法设计上可以灵活的根据 Criter ... -
Hibernate 的连接池属性简介
2009-05-07 11:30 1655Hibernate配置属性 ... -
Hibernate Inverse
2009-04-23 17:15 1276一、Inverse是hibernate双向关系中的基本概念。i ... -
hibernate的hibernate.hbm2ddl.auto属性
2009-01-06 16:33 2369<property name="hibern ... -
hibernate主键常用方式
2009-01-06 16:32 10421) assigned 主键由外部程序负责生成,无需Hibe ...
相关推荐
extends T>和List<? super T>是泛型的不同使用形式,它们在类型约束和操作上有所不同。 1. List<? extends T> - `? extends T` 是类型上界的表示,意味着列表中的元素可以是T类型或者是T的任何子类型。这种类型的...
1,01.zip<br>Output<br>显示所有的调试信息(5KB)<END><br>2,02.zip<br>Some general debugging tips<br>一般的调试技巧(11KB)<END><br>3,03.zip<br>Debugging ISAPI extension<br>调试ISAPI扩展(4KB)<END><br>4,04....
<br> }<br>}<br><br>答案::填空第1题<br>x=10,a=3,b=4,c=5<br>填空第2题<br>java.util<br>填空第3题<br>(public )(static )(void)(main)(String args[])<br>填空第4题<br>1<br>填空第5题<br>
<package name="default" namespace="/" extends="struts-default"> ... <action name="myAction" class="com.example.MyAction"> <interceptor-ref name="myStack"/> <result>success.jsp</result> </action>...
〖程序设计基础〗练习题1<br>一、选择题(每题1分,共30分)<br>...<br>public class myprogram extends _______//定义小程序的主类<br>{<br>Label p1;<br>TextField in;<br>public void init()<br>{ p1=new Label("请...
对象成员变量在对象创建时会获得一个缺省的初始值.<br>5.Java数组随时可以改变大小.<br>6.InputStream和OutputStream读写的数据是8位的.<br>7.Java是强类型语言.<br>8.Java没有指针,引用不能等同于...
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> ... <parameter> <name>username</name> <value>root</value> </parameter> <parameter> <name>password</name> <value>...
public class HibernateDaoImpl<T, PK extends Serializable> implements IHibernateDao<T, PK> { private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate ...
<package name="default" namespace="/" extends="struts-default"> <action name="myAction" class="com.example.MyAction"> <result name="success">/success.jsp</result> </action> </package> ... </...
<package name="default" extends="struts-default"> <action name="myAction" class="com.example.MyAction"> <interceptor-ref name="myStack"/> <result name="success">/success.jsp</result> </action> ...
Map<String, String> filterChainDefinitionMap = new HashMap<>(); // ... factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return factoryBean; } } @Configuration @...
List<String> flavors = new ArrayList<>(); Collections.addAll(flavors, "Peaches'nPlutonium", "RockyRacoon"); ``` 此例中,`flavors`列表将被填充上指定的字符串元素。 #### 2. `asLifoQueue` 此方法将`...
<package name="Student_CRUD_DEMO" extends="struts-default" namespace="/jsp"> <action name="add" class="cn.fjnu.edu.action.AddAction" method="addStu"> <result name="success">list.jsp</result> </...
out.println("<br><br><br><hr><center><font color=red size=12><B>"); try...{ UserBean user=new UserBean(); if(user.check(name,pword)) out.println("登陆成功"); else out.println("登陆失败"); }...
<keyspace <name>beautifulKeyspaceName</name> <columnFamilies> <columnFamily> <name>beautifulColumnFamilyName</name> <row> <key>key10</key> <column> <name>name11</name> <value>value11</value> ...
接触新版本出了问题后,解决了把jar文件...<package name="default" namespace="/" extends="struts-default"> <action name="helloworld"> <result> /helloWorld.jsp </result> </action> </package> </struts>
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.x.x.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>...
<p>...</p> case 2: <p>...</p> default: <p>...</p> } }模板继承(extends,block)base.tpl :<html>@block aa {aaaa@block bb {bbb}@block cc {ccc} }@section Pagination(curPage int...
<package name="erpweb" extends="struts-default"> <global-results> <result name="IOException" >/error/ioexception.jsp</result> <result name="SQLException" >/error/sqlexception.jsp</result> </...
<definition name="home" extends="baseLayout"> <put-attribute name="title" value="Home Page" /> <put-attribute name="page" value="/WEB-INF/views/home.jsp" /> </definition> <!-- 更多页面定义... --...