组合PK字段的分离标准?

我有一个3列作为主键的表。 代码和映射如下:

组合键的类

public class CIDResultCurrentState implements Serializable
{
    private static final long serialVersionUID = -4346801004559840730L;

    private String mID = "";
    private String sNo = "";
    private String date = "";
    // getters, setters, equals(), hashCode() omitted for brevity 
}

班级

public class ResultCurrentState implements IBaseModel, Serializable
{
    private static final long serialVersionUID = 5707101766665188676L;

    private CIDResultCurrentState ID;
    private byte[] rData;
    private byte[] pData;
    // getters, setters, equals(), hashCode() omitted for brevity 
}

hibernate的xml地图

<hibernate-mapping>

<class name="org.irvas.amrregina.backend.model.ResultCurrentState" table="RESULT_CURRENT_STATE">

    <composite-id name="ID" class="org.irvas.amrregina.backend.model.CIDResultCurrentState">
        <key-property name="mID" column="M_ID" type="java.lang.String"/>
        <key-property name="sNo" column="S_NO" type="java.lang.String"/>
        <key-property name="date" column="S_DATE" type="java.lang.String"/>
    </composite-id>

    <property name="rData" column="R_DATA" not-null="false" type="binary"/>
    <property name="pData" column="P_DATA" not-null="false" type="binary"/>

</class>

</hibernate-mapping>

所有这些工作正常。 现在,我需要编写DetachedCriteria查询来获取特定的ResultCurrentState对象/记录。 我尝试了这样的东西,但它不起作用(我不知道是否将此复合ID作为嵌套属性处理):

//...
DetachedCriteria criteria = DetachedCriteria.forClass(ResultCurrentState.class);
DetachedCriteria criteriaID = criteria.createCriteria("ID");
criteriaID.add(Restrictions.eq("mID", arg.getType().getMID()));
criteriaID.add(Restrictions.eq("sNo", arg.getSN()));
criteriaID.add(Restrictions.eq("date", date));
return (ResultCurrentState)DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteriaID));

如果有人能帮我写出适当的标准,我真的很感激它。
谢谢。


Criteria criteria = session.forClass(ResultCurrentState.class);
DetachedCriteria criteriaID = DetachedCriteria.forClass(CIDResultCurrentState.class);
criteriaID.add(Restrictions.eq("mID", arg.getType().getMID()));
criteriaID.add(Restrictions.eq("sNo", arg.getSN()));
criteriaID.add(Restrictions.eq("date", date));
criteria.add(Subqueries.propertyIn("ID", criteriaID));
return criteria.list();

至于使用复合主键的标准,这应该足够了:

DetachedCriteria criteria = DetachedCriteria.forClass(ResultCurrentState.class);
criteria.add(Restrictions.eq("ID.mID", arg.getType().getMID()));
criteria.add(Restrictions.eq("ID.sNo", arg.getSN()));
criteria.add(Restrictions.eq("ID.date", date));

请注意,由于您已在类ResultCurrentState使用该名称在POJO中声明组合键,因此将ID用作前缀:

private CIDResultCurrentState ID;

至于查询执行,我认为它是这样的(我的不同 - 不一定更好):

return (ResultCurrentState)DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));

使用我的会看起来像这样:

List<ResultCurrentState> result = DataAccessUtils.find(criteria);
链接地址: http://www.djcxy.com/p/37051.html

上一篇: Detached criteria on composite PK fields?

下一篇: Hibernate: Criteria with many