Hibernate 如何在不重复的情况下检索实体,即使其子实体已被提取?

Hibernate 如何在不重复的情况下检索实体,即使其子实体已被提取?,hibernate,Hibernate,我有一个叫做属性的对象。在这方面,多个关注点相互关联 <class name="Property" table="T_PROPERTY"> <id name="propertyId" type="integer" column="PRTY_ID"> <generator class="native" /> </id> <property name="pr

我有一个叫做属性的对象。在这方面,多个关注点相互关联

<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>
属性列表,如果我遍历属性,我将获得其他对象。我可以获取其他对象,但属性对象正在被复制。我应该如何避免它

更换
setResultTransformer(标准规范.根实体)
具有
setResultTransformer(标准规范.独立根实体)

那就行了

编辑:阅读OP的评论后


在本例中,如何将propertyConcern==null属性置于顺序的顶部

实际上,Oracle确实有一个概念(主题名称:排序)

但是,我猜CriteriaAPI不支持此功能(原因可能是只有Oracle(或少数RDBMS)支持此功能,但不确定)

好的,无论Criterai API不支持此功能的原因是什么,您都可以使用以下技巧使其正常工作。。。。(假设您的数据库支持空值First/Last)

其中${propertyName}表示要排序的属性的名称

然后


由于@OneToMany join,您会得到重复的引用。您应该使用distinct子句来避免此问题。我不知道在使用标准API时如何使用distinct子句。检查Hibernate参考文档。在本文档中,我如何将propertyConcern==null属性按顺序置于顶部?
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);
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*/
        }

    };
criteria.addOrder(o);