tags:

views:

23

answers:

1

I need to fetch only specific feilds instead of all fields. All my tables has audit feilds(4) . i want that to be ommitted on the selection. how to do that from mapping. is there any other way?.

Criteria criteria = session.createCriteria(Property.class, "property")
                .createAlias("property.propertyType", "type").createAlias(
                        "property.propertyConcern", "propertyConcern",
                        CriteriaSpecification.LEFT_JOIN).createAlias(
                        "propertyConcern.concern", "concern",
                        CriteriaSpecification.LEFT_JOIN)
                        .setResultTransformer(
                        CriteriaSpecification.DISTINCT_ROOT_ENTITY);
A: 

Use a ProjectionList to return only the fields you are after.

Criteria criteria = session.createCriteria(Property.class, "property")
    .setProjection(Projections.projectionList()
        .add(Projections.property("property.field1"))
        .add(Projections.property("concern.field2"))
        /*etc*/)
    /*etc etc etc*/;

The result is an array of arrays.

for (Object result : criteria.list()) {
    final Object[] fields = (Object[]) result;
    log.info("property.field1={}, concern.field2={}", fields[0], fields[1]);
}

By the way, your entity name "Property" could cause confusion/conflict with the existing Hibernate class, especially since you are using Criteria.

C P1R8
But, Again i need to iterate into returned items to form object hierachy. instead of that is there any other way?
Jothi