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.EntityStore.java

License:Apache License

@Override
@Transactional(readOnly = true)//  ww  w . ja v a2  s .  c o m
public EntityModel findByUID(String uid) {
    if (uid == null) {
        throw new IllegalArgumentException("Empty UID parameter.");
    }
    Criteria criteria = getSession().createCriteria(getClazz());
    criteria.createAlias("uid", "uid");
    criteria.createAlias("referenceObject", "referenceObject");
    criteria.add(Restrictions.eq("uid.uid", uid));
    criteria.setFetchMode("fields", FetchMode.JOIN);
    EntityModel entity = (EntityModel) criteria.uniqueResult();
    if (entity.getFields() != null && !entity.getFields().isEmpty()) {
        for (FieldModel field : entity.getFields()) {
            Hibernate.initialize(field.getUid());
        }
    }
    return entity;
}

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

License:Apache License

@Override
@Transactional(readOnly = true)/*from  www  . j  ava2  s .  c  om*/
public R findById(final Long id) {
    final R example = instantiate();
    return getHibernateTemplate().execute(new HibernateCallback<R>() {
        @Override
        public R doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(example.getClass());
            criteria.add(Restrictions.eq("id", id));
            criteria.setFetchMode("fields", FetchMode.JOIN);
            return (R) criteria.uniqueResult();
        }
    });
}

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

License:Apache License

@Override
@Transactional(readOnly = true)//from   w w w  .  j  av  a  2  s  . c o  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.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("fields", FetchMode.JOIN);
            criteria.add(Restrictions.like("lookup", lookupPrefix + "%"));
            return (List<R>) criteria.list();
        }
    });
}

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

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from w ww . j a v a2  s  .c o  m
public List<GroupMappingModel> loadGroupMappingsForLdapGroup(Long directoryId, String ldapGroupDN) {
    List<GroupMappingModel> mappings;
    if (directoryId == null || StringUtils.isBlank(ldapGroupDN)) {
        mappings = Collections.emptyList();
    } else {
        Criteria criteria = getSession().createCriteria(getClazz());
        criteria.setFetchMode("group", FetchMode.JOIN);
        criteria.add(Restrictions.eq("ldapGroupDN", ldapGroupDN));
        criteria.add(Restrictions.eq("directory.id", directoryId));
        mappings = criteria.list();
    }
    return mappings;
}

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

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//w w  w  . j  ava2s.  c  o m
public Map<String, List<GroupMappingModel>> loadGroupMappingsForLdapGroupList(Long directoryId,
        List<String> ldapGroupDNList) {
    List<GroupMappingModel> mappings;
    if (directoryId == null || ldapGroupDNList == null || ldapGroupDNList.isEmpty()) {
        mappings = Collections.emptyList();
    } else {
        Criteria criteria = getSession().createCriteria(getClazz());
        criteria.setFetchMode("group", FetchMode.JOIN);
        criteria.add(Restrictions.in("ldapGroupDN", ldapGroupDNList));
        criteria.add(Restrictions.eq("directory.id", directoryId));
        mappings = criteria.list();
    }
    Map<String, List<GroupMappingModel>> groupMappings = new HashMap<String, List<GroupMappingModel>>();
    if (mappings != null) {
        for (GroupMappingModel mappingModel : mappings) {
            List<GroupMappingModel> groupMappingModels = groupMappings.get(mappingModel.getLdapGroupDN());
            if (groupMappingModels == null) {
                groupMappingModels = new LinkedList<GroupMappingModel>();
                groupMappings.put(mappingModel.getLdapGroupDN(), groupMappingModels);
            }
            groupMappingModels.add(mappingModel);
        }
    }
    return groupMappings;
}

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

License:Apache License

@Override
@Transactional(readOnly = true)//from   w  w w .j  a  va  2s  .  co  m
public NavigationElementModel 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 (NavigationElementModel) criteria.uniqueResult();
}

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

License:Apache License

@Override
@Transactional(readOnly = true)//w ww.j a  va2  s.c  o  m
public List<PackageChangesModel> findAllLastChanges(Long timestamp) {
    final Date date = new Date();
    date.setTime(timestamp);
    return getHibernateTemplate().executeFind(new HibernateCallback<List<PackageChangesModel>>() {
        public List<PackageChangesModel> doInHibernate(Session session)
                throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.add(Restrictions.gt("created", date));
            criteria.setFetchMode("entity", FetchMode.JOIN);
            criteria.setFetchMode("packageModel", FetchMode.JOIN);
            return criteria.list();
        }
    });
}

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

License:Apache License

@Override
@Transactional(readOnly = true)/*ww w .  j a va2s  .c  om*/
public R findByIdWithParent(final Long id) {
    return getHibernateTemplate().execute(new HibernateCallback<R>() {
        @Override
        public R doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.add(Restrictions.eq("id", id));
            criteria.setFetchMode("parent", FetchMode.JOIN);
            return (R) criteria.uniqueResult();
        }
    });
}

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

License:Apache License

@Override
@Transactional(readOnly = true)//from ww  w.  ja  v a2 s  .co m
public List<RelationshipModel> findAllByLikeLookupPrefix(final String lookupPrefix) {
    if (StringUtils.isBlank(lookupPrefix)) {
        return Collections.emptyList();
    }
    return getHibernateTemplate().execute(new HibernateCallback<List<RelationshipModel>>() {
        public List<RelationshipModel> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getClazz());
            criteria.add(Restrictions.like("lookup", lookupPrefix + "%"));
            criteria.setFetchMode("sourceEntity", FetchMode.JOIN);
            criteria.setFetchMode("targetEntity", FetchMode.JOIN);
            return (List<RelationshipModel>) criteria.list();
        }
    });
}

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

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)/*from  w  w  w . ja v a2s  .com*/
public List<RelationshipModel> findChildrenByParentId(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);
            if (registryNodeId == null) {
                criteria.add(Restrictions.isNull("parent"));
            } else {
                criteria.add(Restrictions.eq("parent.id", registryNodeId));
            }
            criteria.setFetchMode("sourceEntity", FetchMode.JOIN);
            criteria.setFetchMode("targetEntity", FetchMode.JOIN);
            return (List<RegistryNodeModel>) criteria.list();
        }
    });
}