diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java index cea86ba032846c83a4ff821163cce8e64a543308..4334d76f8828a42371be88801a08c246eb75d8c8 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AuthorizationDAOFactory.java @@ -97,7 +97,7 @@ public class AuthorizationDAOFactory implements IAuthorizationDAOFactory experimentDAO = new ExperimentDAO(persistencyResources, historyCreator); projectDAO = new ProjectDAO(sessionFactory, historyCreator); sampleDAO = new SampleDAO(persistencyResources, historyCreator); - sampleRelationshipDAO = new SampleRelationshipDAO(sessionFactory, relationshipTypeDAO, historyCreator); + sampleRelationshipDAO = new SampleRelationshipDAO(sessionFactory, historyCreator); gridCustomFilterDAO = new GridCustomFilterDAO(sessionFactory, historyCreator); gridCustomColumnDAO = new GridCustomColumnDAO(sessionFactory, historyCreator); queryDAO = new QueryDAO(sessionFactory, historyCreator); diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleRelationshipDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleRelationshipDAO.java index 2f9df6bf8b9e6b41b0d3264f221ace46a17b526b..35bb49aeba79be9727fb856aae670a7ecfc5a3a7 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleRelationshipDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SampleRelationshipDAO.java @@ -1,6 +1,5 @@ package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; -import ch.systemsx.cisd.openbis.generic.server.dataaccess.IRelationshipTypeDAO; import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.deletion.EntityHistoryCreator; import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant; import ch.systemsx.cisd.openbis.generic.shared.dto.ISampleRelationshipDAO; @@ -15,15 +14,47 @@ import java.util.List; public class SampleRelationshipDAO extends AbstractGenericEntityDAO<SampleRelationshipPE> implements ISampleRelationshipDAO { - private final IRelationshipTypeDAO relationshipTypeDAO; + private Long parentChildRelationshipId = null; - protected SampleRelationshipDAO(SessionFactory sessionFactory, IRelationshipTypeDAO relationshipTypeDAO, EntityHistoryCreator historyCreator) { + protected SampleRelationshipDAO(SessionFactory sessionFactory, EntityHistoryCreator historyCreator) + { super(sessionFactory, SampleRelationshipPE.class, historyCreator); - this.relationshipTypeDAO = relationshipTypeDAO; } - public void persist(Collection<SampleRelationshipPE> sampleRelationships) { - RelationshipTypePE relationshipType = relationshipTypeDAO.tryFindRelationshipTypeByCode(BasicConstant.PARENT_CHILD_INTERNAL_RELATIONSHIP); + // + // Helper Methods to obtain relationship ID "fast" and using detached criteria that should not require a session + // + + private Long getParentChildRelationshipId() + { + if (parentChildRelationshipId == null) + { + synchronized(SampleRelationshipDAO.class) + { + if (parentChildRelationshipId == null) + { + parentChildRelationshipId = getParentChildRelationship().getId(); + } + } + } + return parentChildRelationshipId; + } + + private RelationshipTypePE getParentChildRelationship() + { + final DetachedCriteria criteria = DetachedCriteria.forClass(RelationshipTypePE.class); + criteria.add(Restrictions.eq("code", BasicConstant.PARENT_CHILD_INTERNAL_RELATIONSHIP)); + List<RelationshipTypePE> cast = cast(getHibernateTemplate().findByCriteria(criteria)); + return cast.get(0); + } + + // + // DAO Methods + // + + public void persist(Collection<SampleRelationshipPE> sampleRelationships) + { + RelationshipTypePE relationshipType = getParentChildRelationship(); for (SampleRelationshipPE sampleRelationship : sampleRelationships) { sampleRelationship.setRelationship(relationshipType); @@ -31,22 +62,23 @@ public class SampleRelationshipDAO extends AbstractGenericEntityDAO<SampleRelati } } - public void delete(Collection<SampleRelationshipPE> sampleRelationships) { + public void delete(Collection<SampleRelationshipPE> sampleRelationships) + { getHibernateTemplate().deleteAll(sampleRelationships); } - public List<SampleRelationshipPE> listSampleParents(List<Long> childrenTechIds) { - Long typeId = relationshipTypeDAO.getRelationshipTypeId(BasicConstant.PARENT_CHILD_INTERNAL_RELATIONSHIP); + public List<SampleRelationshipPE> listSampleParents(List<Long> childrenTechIds) + { final DetachedCriteria criteria = DetachedCriteria.forClass(SampleRelationshipPE.class); - criteria.add(Restrictions.eq("relationship.id", typeId)); + criteria.add(Restrictions.eq("relationship.id", getParentChildRelationshipId())); criteria.add(Restrictions.in("childSample.id", childrenTechIds)); return cast(getHibernateTemplate().findByCriteria(criteria)); } - public List<SampleRelationshipPE> listSampleChildren(List<Long> parentTechIds) { - Long typeId = relationshipTypeDAO.getRelationshipTypeId(BasicConstant.PARENT_CHILD_INTERNAL_RELATIONSHIP); + public List<SampleRelationshipPE> listSampleChildren(List<Long> parentTechIds) + { final DetachedCriteria criteria = DetachedCriteria.forClass(SampleRelationshipPE.class); - criteria.add(Restrictions.eq("relationship.id", typeId)); + criteria.add(Restrictions.eq("relationship.id", getParentChildRelationshipId())); criteria.add(Restrictions.in("parentSample.id", parentTechIds)); return cast(getHibernateTemplate().findByCriteria(criteria)); }