我有一个包含 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
}
休眠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));
如果有人可以帮助我编写适当的标准,我将不胜感激。
谢谢。