开发者

Detached criteria on composite PK fields?

开发者 https://www.devze.com 2023-04-02 22:25 出处:网络
I have a table with 3 columns as primary key. Code and mapping is as follows: class for the composite key

I have a table with 3 columns as primary key. Code and mapping is as follows:

class for the composite key

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 
}

class for the table

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

    private CIDResultCu开发者_运维技巧rrentState ID;
    private byte[] rData;
    private byte[] pData;
    // getters, setters, equals(), hashCode() omitted for brevity 
}

hibernate xml map

<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>

All of this works fine. Now, what I need is to write DetachedCriteria query to get specific ResultCurrentState object/record. I tried something like this but it doesn't work (I don't know whether to treat this composite ID as nested property or not):

//...
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));

If somebody can help me to write proper criteria, I'd really appreciate it.

Thanks.


As for criteria using composite primary key this should be enough:

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));

Note that ID is used as prefix since you have declared the composite key in the POJO using that name, in the class ResultCurrentState:

private CIDResultCurrentState ID;

As for the query execution I think it goes this way (mine is different -- not necessarily better):

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

Using mine would look like this:

List<ResultCurrentState> result = DataAccessUtils.find(criteria);


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消