编程语言
首页 > 编程语言> > java-复合PK字段上的分离标准?

java-复合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 
}

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

如果有人可以帮助我编写适当的标准,我将不胜感激.
谢谢.

解决方法:

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

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

标签:hibernate,hibernate-criteria,detachedcriteria,java
来源: https://codeday.me/bug/20191208/2087872.html