Example usage for org.hibernate Criteria setFetchMode

List of usage examples for org.hibernate Criteria setFetchMode

Introduction

In this page you can find the example usage for org.hibernate Criteria setFetchMode.

Prototype

public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException;

Source Link

Document

Specify an association fetching strategy for an association or a collection of values.

Usage

From source file:net.firejack.platform.core.store.registry.RelationshipStore.java

License:Apache License

@Override
@Transactional(readOnly = true)//from w  w w . j  av  a2s .c  o  m
public List<RelationshipModel> findRelatedEntitiesByEntityId(final Long registryNodeId,
        final SpecifiedIdsFilter<Long> filter) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);
            Criterion sourceEntityCriterion = Restrictions.eq("sourceEntity.id", registryNodeId);
            Criterion targetEntityCriterion = Restrictions.eq("targetEntity.id", registryNodeId);
            criteria.add(Restrictions.or(sourceEntityCriterion, targetEntityCriterion));
            criteria.setFetchMode("sourceEntity", FetchMode.JOIN);
            criteria.setFetchMode("targetEntity", FetchMode.JOIN);
            return (List<RegistryNodeModel>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.registry.RelationshipStore.java

License:Apache License

@Override
@Transactional(readOnly = true)//from   ww w.  j av  a  2 s .  com
public List<RelationshipModel> findRelatedEntitiesBySourceEntityId(final Long registryNodeId,
        final boolean loadSourceField, final boolean loadTargetField, final SpecifiedIdsFilter<Long> filter) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createCriteriaForFilter(session, filter);
            criteria.add(Restrictions.eq("sourceEntity.id", registryNodeId));
            criteria.setFetchMode("sourceEntity", FetchMode.JOIN);
            criteria.setFetchMode("targetEntity", FetchMode.JOIN);
            if (loadSourceField)
                criteria.setFetchMode("sourceEntity.fields", FetchMode.JOIN);
            if (loadTargetField)
                criteria.setFetchMode("targetEntity.fields", FetchMode.JOIN);
            if (loadSourceField || loadTargetField)
                criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

            return (List<RegistryNodeModel>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.registry.resource.CollectionStore.java

License:Apache License

@Override
@Transactional(readOnly = true)/*w  ww . j av  a2 s.  c  om*/
public CollectionModel findByUID(String uid) {
    if (uid == null) {
        throw new IllegalArgumentException("Empty UID parameter.");
    }
    Criteria criteria = getSession().createCriteria(getClazz());
    criteria.createAlias("uid", "uid");
    criteria.add(Restrictions.eq("uid.uid", uid));
    criteria.setFetchMode("collectionMembers", FetchMode.JOIN);
    CollectionModel collection = (CollectionModel) criteria.uniqueResult();
    if (collection.getCollectionMembers() != null) {
        for (CollectionMemberModel collectionMember : collection.getCollectionMembers()) {
            Hibernate.initialize(collectionMember.getReference());
        }
    }
    return collection;
}

From source file:net.firejack.platform.core.store.registry.resource.ResourceStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from   w  ww . j ava 2  s . c o  m
public R findByUID(String uid) {
    if (uid == null) {
        throw new IllegalArgumentException("Empty UID parameter.");
    }
    Criteria criteria = getSession().createCriteria(getClazz());
    criteria.createAlias("uid", "uid");
    criteria.add(Restrictions.eq("uid.uid", uid));
    criteria.setFetchMode("resourceVersion", FetchMode.JOIN);
    return (R) criteria.uniqueResult();
}

From source file:net.firejack.platform.core.store.registry.resource.ResourceStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)/*from w  w w  .j  ava 2s  . co  m*/
public List<R> findAllByLikeLookupPrefix(final String lookupPrefix) {
    if (StringUtils.isBlank(lookupPrefix)) {
        return Collections.emptyList();
    }
    return getHibernateTemplate().execute(new HibernateCallback<List<R>>() {
        public List<R> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.add(Restrictions.like("lookup", lookupPrefix + "%"));
            criteria.setFetchMode("resourceVersions", FetchMode.JOIN);
            return (List<R>) criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.registry.ResourceLocationStore.java

License:Apache License

@Override
@Transactional(readOnly = true)/*from   ww w.j av  a2 s.  c o  m*/
public ResourceLocationModel findByUID(String uid) {
    if (uid == null) {
        throw new IllegalArgumentException("Empty UID parameter.");
    }
    Criteria criteria = getSession().createCriteria(getClazz());
    criteria.createAlias("uid", "uid");
    criteria.add(Restrictions.eq("uid.uid", uid));
    criteria.setFetchMode("permissions", FetchMode.JOIN);
    return (ResourceLocationModel) criteria.uniqueResult();
}

From source file:net.firejack.platform.core.store.registry.RoleStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)/*  w w  w . j  a  va  2s  .  co m*/
public Map<RoleModel, Boolean> findAllAssignedRolesByUserId(Long userId) {
    Map<RoleModel, Boolean> result;
    if (userId == null) {
        result = null;
    } else {
        Criteria criteria = getSession().createCriteria(UserRoleModel.class);
        criteria.setFetchMode("role", FetchMode.JOIN);
        criteria.add(Restrictions.eq("user.id", userId));
        List<UserRoleModel> userRoles = (List<UserRoleModel>) criteria.list();
        result = new HashMap<RoleModel, Boolean>();
        if (userRoles != null) {
            for (UserRoleModel userRole : userRoles) {
                boolean isGlobal = userRole.getInternalId() == null && userRole.getType() == null;
                result.put(userRole.getRole(), isGlobal);
            }
        }
    }
    return result;
}

From source file:net.firejack.platform.core.store.registry.SecuredRecordStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from  ww  w . j  a  v a 2  s . c o m
public SecuredRecordModel findByExternalIdAndRegistryNode(final Long externalNumberId,
        final RegistryNodeModel registryNode) {
    SecuredRecordModel sr;
    if (externalNumberId == null || registryNode == null) {
        sr = null;
    } else {
        Criteria criteria = getSession().createCriteria(getClazz());
        criteria.setFetchMode("registryNode", FetchMode.JOIN);
        criteria.setFetchMode("parentSecuredRecords", FetchMode.JOIN);
        criteria.add(Restrictions.eq("externalNumberId", externalNumberId));
        criteria.add(Restrictions.eq("registryNode.id", registryNode.getId()));
        List<SecuredRecordModel> srList = (List<SecuredRecordModel>) criteria.list();
        sr = srList.isEmpty() ? null : srList.get(0);
    }
    return sr;
}

From source file:net.firejack.platform.core.store.registry.SecuredRecordStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from   www  . j a  va  2 s. c om
public SecuredRecordModel findByExternalStringIdAndRegistryNode(String externalStringId,
        RegistryNodeModel registryNode) {
    SecuredRecordModel sr;
    if (StringUtils.isBlank(externalStringId) || registryNode == null) {
        sr = null;
    } else {
        Criteria criteria = getSession().createCriteria(getClazz());
        criteria.setFetchMode("registryNode", FetchMode.JOIN);
        criteria.setFetchMode("parentSecuredRecords", FetchMode.JOIN);
        criteria.add(Restrictions.eq("externalStringId", externalStringId));
        criteria.add(Restrictions.eq("registryNode.id", registryNode.getId()));
        List<SecuredRecordModel> srList = (List<SecuredRecordModel>) criteria.list();
        sr = srList.isEmpty() ? null : srList.get(0);
    }
    return sr;
}

From source file:net.firejack.platform.core.store.registry.SecuredRecordStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from  www.j av a  2s.c  om
public List<SecuredRecordModel> findAllWithLoadedRegistryNode() {
    return getHibernateTemplate().executeFind(new HibernateCallback<List<SecuredRecordModel>>() {
        @Override
        public List<SecuredRecordModel> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.setFetchMode("registryNode", FetchMode.JOIN);
            return criteria.list();
        }
    });
}