Skip to content
Snippets Groups Projects
Commit 0a007b49 authored by brinn's avatar brinn
Browse files

change: set an upper limit for property loading also for material listing...

change: set an upper limit for property loading also for material listing (generalized from sample listing)

SVN: 11950
parent 882a76f4
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2009 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.openbis.generic.server.dataaccess.db;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
/**
* Utility routines for DAOs.
*
* @author Bernd Rinn
*/
final class DAOUtils
{
private DAOUtils()
{
// Cannot be instantiated
}
/**
* Don't try to get properties for more than 1000 entities.
*/
final static int MAX_COUNT_FOR_PROPERTIES = 1000;
/**
* Returns the number of entities that the given <var>critera</var> will return.
*/
static int getCount(final Criteria criteria)
{
int count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
// Undo the rowCount projection
criteria.setProjection(null);
criteria.setResultTransformer(Criteria.ROOT_ENTITY);
return count;
}
}
......@@ -61,7 +61,15 @@ public class MaterialDAO extends AbstractGenericEntityDAO<MaterialPE> implements
final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
criteria.add(Restrictions.eq("materialType", materialType));
criteria.setFetchMode("materialProperties", FetchMode.JOIN);
final int count = DAOUtils.getCount(criteria);
if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES)
{
criteria.setFetchMode("materialProperties", FetchMode.JOIN);
} else
{
operationLog.info(String.format("Found %d materials, disable properties loading.",
count));
}
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
final List<MaterialPE> list = cast(criteria.list());
if (operationLog.isDebugEnabled())
......
......@@ -24,7 +24,6 @@ import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.validator.ClassValidator;
import org.springframework.dao.DataAccessException;
......@@ -52,11 +51,6 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa
{
private final static Class<SamplePE> ENTITY_CLASS = SamplePE.class;
/**
* Don't try to get properties for more than 1000 samples.
*/
private final static int MAX_COUNT_FOR_PROPERTIES = 1000;
/**
* This logger does not output any SQL statement. If you want to do so, you had better set an
* appropriate debugging level for class {@link JdbcAccessor}. </p>
......@@ -105,11 +99,11 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa
{
basicCriteria.add(criterion);
}
final int count = getCount(basicCriteria);
final int count = DAOUtils.getCount(basicCriteria);
if (withExperimentAndProperties)
{
basicCriteria.setFetchMode("experimentInternal", FetchMode.JOIN);
if (count < MAX_COUNT_FOR_PROPERTIES)
if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES)
{
basicCriteria.setFetchMode("sampleProperties", FetchMode.JOIN);
} else
......@@ -122,15 +116,6 @@ public class SampleDAO extends AbstractGenericEntityDAO<SamplePE> implements ISa
return cast(basicCriteria.list());
}
private int getCount(final Criteria basicCriteria)
{
int count = (Integer) basicCriteria.setProjection(Projections.rowCount()).uniqueResult();
// Undo the rowCount projection
basicCriteria.setProjection(null);
basicCriteria.setResultTransformer(Criteria.ROOT_ENTITY);
return count;
}
private List<SamplePE> listSamplesByCriteria(boolean withExperimentAndProperties,
Criterion... criterions) throws DataAccessException
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment