Example usage for org.hibernate.criterion Projections property

List of usage examples for org.hibernate.criterion Projections property

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections property.

Prototype

public static PropertyProjection property(String propertyName) 

Source Link

Document

A property value projection

Usage

From source file:com.romeikat.datamessie.core.base.dao.impl.ProjectDao.java

License:Open Source License

public ProjectDto getAsDto(final SharedSessionContract ssc, final long projectId, final Long userId) {
    // Restrict to user
    final Collection<Long> projectIdsForUser = getIdsForUser(ssc, userId);
    if (projectIdsForUser.isEmpty()) {
        return null;
    }/*from ww  w  . j a  va 2s.  co m*/

    // Query: Project
    final EntityWithIdQuery<Project> projectQuery = new EntityWithIdQuery<>(Project.class);
    projectQuery.addRestriction(Restrictions.idEq(projectId));
    projectQuery.addIdRestriction(projectIdsForUser);
    projectQuery.addOrder(Order.desc("started"));
    projectQuery.setResultTransformer(new AliasToBeanResultTransformer(ProjectDto.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("name"), "name");
    projectionList.add(Projections.property("crawlingEnabled"), "crawlingEnabled");
    projectionList.add(Projections.property("crawlingInterval"), "crawlingInterval");
    projectionList.add(Projections.property("preprocessingEnabled"), "preprocessingEnabled");
    final ProjectDto dto = (ProjectDto) projectQuery.uniqueForProjection(ssc, projectionList);
    return dto;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.RedirectingRuleDao.java

License:Open Source License

public List<RedirectingRuleDto> getAsDtos(final SharedSessionContract ssc, final long sourceId) {
    // Query: RedirectingRule
    final EntityWithIdQuery<RedirectingRule> redirectingRuleQuery = new EntityWithIdQuery<>(
            RedirectingRule.class);
    redirectingRuleQuery.addRestriction(Restrictions.eq("sourceId", sourceId));
    redirectingRuleQuery.addOrder(Order.asc("activeFrom"));
    redirectingRuleQuery.setResultTransformer(new AliasToBeanResultTransformer(RedirectingRuleDto.class));

    // Done/*from w  w  w. j  a v  a2 s  . c  om*/
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("regex"), "regex");
    projectionList.add(Projections.property("regexGroup"), "regexGroup");
    projectionList.add(Projections.property("activeFrom"), "activeFrom");
    projectionList.add(Projections.property("activeTo"), "activeTo");
    @SuppressWarnings("unchecked")
    final List<RedirectingRuleDto> dtos = (List<RedirectingRuleDto>) redirectingRuleQuery.listForProjection(ssc,
            projectionList);
    return dtos;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceTypeDao.java

License:Open Source License

public List<SourceTypeDto> getAsDtos(final SharedSessionContract ssc) {
    // Query: SourceType
    final EntityWithIdQuery<SourceType> sourceQuery = new EntityWithIdQuery<>(SourceType.class);
    sourceQuery.addOrder(Order.asc("name"));
    sourceQuery.setResultTransformer(new AliasToBeanResultTransformer(SourceTypeDto.class));

    // Done//from w w  w .  j a  v a  2  s . c  o  m
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("name"), "name");
    @SuppressWarnings("unchecked")
    final List<SourceTypeDto> dtos = (List<SourceTypeDto>) sourceQuery.listForProjection(ssc, projectionList);
    return dtos;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.SourceTypeDao.java

License:Open Source License

public List<SourceTypeDto> getAsDtos(final SharedSessionContract ssc, final long sourceId) {
    // Query: Source2SourceType
    final EntityQuery<Source2SourceType> project2SourceQuery = new EntityQuery<>(Source2SourceType.class);
    project2SourceQuery.addRestriction(Restrictions.eq("sourceId", sourceId));
    final List<Long> sourceTypeIds = project2SourceQuery.listIdsForProperty(ssc, "sourceTypeId");
    if (sourceTypeIds.isEmpty()) {
        return Collections.emptyList();
    }/*from   w  w  w .  j  a  v  a  2s  .c  o  m*/

    // Query: SourceType
    final EntityQuery<SourceType> sourceTypeQuery = new EntityQuery<>(SourceType.class);
    sourceTypeQuery.addRestriction(Restrictions.in("id", sourceTypeIds));
    sourceTypeQuery.addOrder(Order.asc("name"));
    sourceTypeQuery.setResultTransformer(new AliasToBeanResultTransformer(SourceTypeDto.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("name"), "name");
    @SuppressWarnings("unchecked")
    final List<SourceTypeDto> dtos = (List<SourceTypeDto>) sourceTypeQuery.listForProjection(ssc,
            projectionList);
    return dtos;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.TagSelectingRuleDao.java

License:Open Source License

public List<TagSelectingRuleDto> getAsDtos(final SharedSessionContract ssc, final long sourceId) {
    // Query: TagSelectingRule
    final EntityWithIdQuery<TagSelectingRule> tagSelectingRuleQuery = new EntityWithIdQuery<>(
            TagSelectingRule.class);
    tagSelectingRuleQuery.addRestriction(Restrictions.eq("sourceId", sourceId));
    tagSelectingRuleQuery.addOrder(Order.asc("activeFrom"));
    tagSelectingRuleQuery.setResultTransformer(new AliasToBeanResultTransformer(TagSelectingRuleDto.class));

    // Done//w  w w .  j  a v  a  2  s .c  o  m
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("tagSelector"), "tagSelector");
    projectionList.add(Projections.property("activeFrom"), "activeFrom");
    projectionList.add(Projections.property("activeTo"), "activeTo");
    @SuppressWarnings("unchecked")
    final List<TagSelectingRuleDto> dtos = (List<TagSelectingRuleDto>) tagSelectingRuleQuery
            .listForProjection(ssc, projectionList);
    return dtos;
}

From source file:com.romeikat.datamessie.core.base.query.entity.EntityQueryTest.java

License:Open Source License

@Test
public void listForProjection() {
    final EntityQuery<BarEntity> query = new EntityQuery<>(BarEntity.class);
    query.addOrder(Order.asc("name"));

    @SuppressWarnings("unchecked")
    final List<String> names = (List<String>) query.listForProjection(sessionProvider.getStatelessSession(),
            Projections.property("name"));
    final List<String> expected = Arrays.asList("BarEntity1", "BarEntity2", "BarEntity3", "BarEntity4");
    assertTrue(ListUtils.isEqualList(expected, names));

    dbSetupTracker.skipNextLaunch();/*w  ww  .  java  2  s . c  om*/
}

From source file:com.romeikat.datamessie.core.base.query.entity.EntityQueryTest.java

License:Open Source License

@Test
public void uniqueForProjection() {
    final EntityQuery<BarEntity> query = new EntityQuery<>(BarEntity.class);
    query.addRestriction(Restrictions.eq("name", "BarEntity1"));

    final String name = (String) query.uniqueForProjection(sessionProvider.getStatelessSession(),
            Projections.property("name"));
    assertEquals("BarEntity1", name);

    dbSetupTracker.skipNextLaunch();/* w  ww  . j a va  2  s. c  o  m*/
}

From source file:com.romeikat.datamessie.core.base.query.entity.EntityWithIdQueryTest.java

License:Open Source License

@Test
public void listForProjection() {
    final EntityWithIdQuery<BarEntityWithId> query = new EntityWithIdQuery<>(BarEntityWithId.class);
    query.addOrder(Order.asc("name"));

    @SuppressWarnings("unchecked")
    final List<String> names = (List<String>) query.listForProjection(sessionProvider.getStatelessSession(),
            Projections.property("name"));
    final List<String> expected = Arrays.asList("BarEntityWithId1", "BarEntityWithId2", "BarEntityWithId3",
            "BarEntityWithId4");
    assertTrue(ListUtils.isEqualList(expected, names));

    dbSetupTracker.skipNextLaunch();//ww  w.j a  v  a 2  s  .  c o m
}

From source file:com.romeikat.datamessie.core.base.query.entity.EntityWithIdQueryTest.java

License:Open Source License

@Test
public void uniqueForProjection() {
    final EntityWithIdQuery<BarEntityWithId> query = new EntityWithIdQuery<>(BarEntityWithId.class);
    query.addRestriction(Restrictions.eq("name", "BarEntityWithId1"));

    final String name = (String) query.uniqueForProjection(sessionProvider.getStatelessSession(),
            Projections.property("name"));
    assertEquals("BarEntityWithId1", name);

    dbSetupTracker.skipNextLaunch();/*from  ww w  .  j a v a 2 s  .c  om*/
}

From source file:com.romeikat.datamessie.core.base.query.entity.execute.AbstractEntityQueryExecutor.java

License:Open Source License

public void applyPropertyProjection(final Criteria criteria, final String propertyName) {
    criteria.setProjection(Projections.property(propertyName));
}