From c0cf89ad581c58c12f08a339f147c34e0fab1d01 Mon Sep 17 00:00:00 2001 From: juanf <juanf@ethz.ch> Date: Fri, 27 Jan 2023 11:59:48 +0100 Subject: [PATCH] SSDM-13195 : Hibernate fixes, modifying PE layer to use DAO for parents/children --- .../db/AuthorizationDAOFactory.java | 2 +- .../dataaccess/db/SampleRelationshipDAO.java | 58 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) 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 cea86ba0328..4334d76f882 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 2f9df6bf8b9..35bb49aeba7 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)); } -- GitLab