diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..45f98cfc977f7b1e8e4170278e460384dcd66b54 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/DAOUtils.java @@ -0,0 +1,52 @@ +/* + * 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; + } + +} diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java index 96161791a62cd8a1c3848b4d1035bacfa987f3cb..7ca4f5785a7a8dd40a2e5c9e7110991167d1ebe3 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/MaterialDAO.java @@ -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()) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java index 47e4276f2a1ba3107aff3453f15cedf528b99e83..9f3c90fa5b0327f5e2aa8e8ddd69116f43693afd 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleDAO.java @@ -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 {