import org.hibernate.criterion.Order;\r
import org.hibernate.criterion.Projections;\r
import org.hibernate.criterion.Restrictions;\r
+import org.hibernate.envers.query.AuditQuery;\r
import org.springframework.dao.DataAccessException;\r
import org.springframework.stereotype.Repository;\r
-import org.springframework.transaction.annotation.Transactional;\r
\r
import eu.etaxonomy.cdm.model.common.CdmBase;\r
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;\r
+import eu.etaxonomy.cdm.persistence.query.OrderHint;\r
\r
\r
/**\r
* @author a.mueller\r
- *\r
+ * FIXME CdmEntityDaoBase is abstract, can it be annotated with @Repository?\r
*/\r
@Repository\r
public abstract class CdmEntityDaoBase<T extends CdmBase> extends DaoBase implements ICdmEntityDao<T> {\r
private static final Logger logger = Logger.getLogger(CdmEntityDaoBase.class);\r
\r
- int flushAfterNo = 2000;\r
+ int flushAfterNo = 1000; //large numbers may cause synchronisation errors when commiting the session !!\r
protected Class<T> type;\r
\r
public CdmEntityDaoBase(Class<T> type){\r
Iterator<T> iterator = cdmObjCollection.iterator();\r
int i = 0;\r
while(iterator.hasNext()){\r
- if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}\r
+ if ( ( (i % 2000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}\r
T cdmObj = iterator.next();\r
UUID uuid = saveCdmObject_(cdmObj);\r
if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}\r
return transientObject.getUuid();\r
}\r
\r
+ public UUID refresh(T persistentObject) throws DataAccessException {\r
+ getSession().refresh(persistentObject);\r
+ return persistentObject.getUuid();\r
+ }\r
+ \r
public UUID delete(T persistentObject) throws DataAccessException {\r
getSession().delete(persistentObject);\r
return persistentObject.getUuid();\r
}\r
\r
public List<T> list(Integer limit, Integer start) {\r
+ return list(limit, start, null); \r
+ }\r
+ \r
+ public <TYPE extends T> List<TYPE> list(Integer limit, Integer start, List<OrderHint> orderHints) {\r
+ \r
Criteria crit = getSession().createCriteria(type); \r
if(limit != null) {\r
crit.setFirstResult(start);\r
crit.setMaxResults(limit);\r
}\r
+ if(orderHints != null){\r
+ for(OrderHint orderHint : orderHints){\r
+ Order order;\r
+ String assocObj = null, propname;\r
+ int pos;\r
+ if((pos = orderHint.getPropertyName().indexOf('.', 0)) >= 0){\r
+ assocObj = orderHint.getPropertyName().substring(0, pos);\r
+ propname = orderHint.getPropertyName().substring(pos + 1);\r
+ } else {\r
+ propname = orderHint.getPropertyName();\r
+ }\r
+ if(orderHint.isAscending()){\r
+ order = Order.asc(propname); \r
+ } else {\r
+ order = Order.desc(propname);\r
+ }\r
+ if(assocObj != null){\r
+ crit.createCriteria(assocObj).addOrder(order);\r
+ } else {\r
+ crit.addOrder(order); \r
+ }\r
+ }\r
+ }\r
return crit.list(); \r
}\r
\r
}\r
return crit.list(); \r
}\r
-\r
-\r
+ \r
public List<T> rows(String tableName, int limit, int start) {\r
Query query = getSession().createQuery("from " + tableName + " order by uuid");\r
query.setFirstResult(start);\r
List<T> result = query.list();\r
return result;\r
}\r
+ \r
+ public Class<T> getType() {\r
+ return type;\r
+ }\r
+ \r
+ protected void setPagingParameter(Query query, Integer pageSize, Integer pageNumber){\r
+ if(pageSize != null) {\r
+ query.setMaxResults(pageSize);\r
+ if(pageNumber != null) {\r
+ query.setFirstResult(pageNumber * pageSize);\r
+ } else {\r
+ query.setFirstResult(0);\r
+ }\r
+ }\r
+ }\r
+ \r
+ protected void setPagingParameter(AuditQuery query, Integer pageSize, Integer pageNumber){\r
+ if(pageSize != null) {\r
+ query.setMaxResults(pageSize);\r
+ if(pageNumber != null) {\r
+ query.setFirstResult(pageNumber * pageSize);\r
+ } else {\r
+ query.setFirstResult(0);\r
+ }\r
+ }\r
+ }\r
}\r