Detached criteria on composite PK fields?
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 CIDResultCurrentState 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.
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();
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);
链接地址: http://www.djcxy.com/p/37052.html
上一篇: Hibernate查询条件涉及继承的映射
下一篇: 组合PK字段的分离标准?