无法使用Criteria和Projection获取属性

时间:2011-09-16 03:05:16

标签: hibernate criteria

      <id name="id" type="java.lang.Long">
            <column name="MBR_ATR_ID"/>
            <generator class="sequence">
                <param name="sequence">ESPMB.MBR_ATR_ID_SEQ</param>
            </generator>
        </id>

        <property name="memberId" type="long">
            <column name="MBR_ID" length="18"/>
        </property>
        <property name="customerId" type="long">
            <column name="CUST_ID" length="18"/>
        </property>
        <property name="eligibilitySourceSystem" type="string">
            <column name="ELIG_SRC_SYS" length="20" not-null="true"/>
        </property>

above HBM file belongs to MemberAttribute.hbm file and memberId is foreign key and it is  primary key in Member.hbm file.

Projection projection = Projections.projectionList()
        .add(Property.forName("memberId"))
        .add(Property.forName("customerId"));
memberAttributeCriteria.setProjection(projection);
 List<Object> results =  memberAttributeCriteria.list();
Iterator iter = results.iterator();
while (iter.hasNext())
        {
            System.out.println("New object");
            Object[] obj = (Object[]) iter.next();
            for (int i=0;i<obj.length;i++)
            {
                System.out.println(obj[i]);  // memberId always coming as null. 
            }


        }

MemberId始终为null。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您与会员实体之间没有任何关系。如果您不想使用hibernate获得关系,则必须在orm.xml中定义此关系。

例如:

<one-to-one name="memberId" target-entity="path.to.entiny.Member">
     <join-column name="ID" referenced-column-name="id"/>
</one-to-one>