Thursday 12 June 2014

add hint to a criteria in hibernate

http://stackoverflow.com/questions/1327503/how-to-insert-an-optimizer-hint-to-hibernate-criteria-api-query

---

ProjectionList proList = Projections.projectionList();
proList.add(Projections.sqlProjection("/*+ index(this_ OUTGOING_ADAPTOR_MSG_IDX_6) */ 1 as MYHINT",    new String[]{},    new Type  []{}));

for (javax.persistence.metamodel.Attribute<. super OutgoingAdaptorMessageEntity, .> o : getManager().getMetamodel().managedType(OutgoingAdaptorMessageEntity.class).getAttributes()) {
    proList.add(Projections.property(o.getName()),o.getName());
}

criteria.setProjection(proList);

criteria.setResultTransformer(new AliasToBeanResultTransformer(OutgoingAdaptorMessageEntity.class));