I have object called Property. in this multiple concerns associated.
<class name="Property"
table="T_PROPERTY">
<id name="propertyId" type="integer" column="PRTY_ID">
<generator class="native" />
</id>
<property name="propertyName" column="PRTY_NAME" type="string"/>
<many-to-one name="propertyType" class="com.mmm.ehspreg2.entity.property.PropertyType" column="PRTY_TYP_ID" />
<property name="active" column="ACTV" type="boolean"/>
<set name="propertyConcern">
<key column="PRTY_ID"/>
<one-to-many class="PropertyConcern"/>
</set>
</class>
<class name="PropertyConcern"
table="T_CONCERN">
<id name="prtyCrnId" type="integer" column="PRTY_CRN_ID">
<generator class="native" />
</id>
<many-to-one name="concern"
class="com.mmm.ehspreg2.entity.property.Concern" column="CRN_ID" />
<many-to-one name="property"
class="com.mmm.ehspreg2.entity.property.Property" column="PRTY_ID" />
</class>
So I need List of unique property object. Below is my code:
Criteria criteria = getPersManager().getCurrentSession()
.createCriteria(Property.class).createAlias("propertyType",
"type").createCriteria("propertyConcern",
"propertyConcern", CriteriaSpecification.LEFT_JOIN)
.createCriteria("propertyConcern.concern",
CriteriaSpecification.LEFT_JOIN).setFetchMode("type",
FetchMode.JOIN).setFetchMode("propertyConcern",
FetchMode.JOIN).setFetchMode("propertyConcern.concern",
FetchMode.JOIN).setResultTransformer(
CriteriaSpecification.ROOT_ENTITY);
List of property, if I traverse through property I should get the other objects. I am able to get the other objects, but Property object is ge开发者_JAVA百科tting duplicated. How should I avoid it?
Replace
setResultTransformer(CriteriaSpecification.ROOT_ENTITY)
with
setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
That will do!
EDIT: after reading OP's comment
In this how do i bring propertyConcern==null property on top in the order?
Actually, Oracle does have a concept of NULLS FIRST/LAST (Topic Name : Sorting)
But, I GUESS Criteria API does not have support for this facility (the reason might be that only Oracle (or few RDBMS) support this facility, not sure though).
Ok, whatever the reason why Criterai API does not support this, you can do following trick to make it work.... (assuming that your DB supports Nulls First/Last)
Order o = new Order(${propertyName}, true) {
@Override
public String toSqlString(Criteria criteria, org.hibernate.criterion.CriteriaQuery criteriaQuery)
throws HibernateException {
return super.toSqlString(criteria, criteriaQuery) + " NULLS FIRST"; /* or LAST*/
}
};
where ${propertyName} means the name of property you're gonna sort
then
criteria.addOrder(o);
精彩评论