Hibernate查询异常:无法在拥有实体

时间:2015-06-08 14:29:18

标签: java hibernate criteria

我无法访问嵌入式类中的值。

Criteria cr = this.getHibernateTemplate().getSessionFactory().openSession().createCriteria(DuaVO.class,"Dua");

if (duaSearchVO.getContractNum()!=null && !duaSearchVO.getContractNum().isEmpty()) {
    cr.createCriteria("Dua.contractFundingDetails", "contractFundingDetails");
    cr.add(Restrictions.eq("contractFundingDetails.contractNumber", duaSearchVO.getContractNum()));
    flag = true;
}

Junit测试

@Test
public void testsearchDua() {
    DuaSearchVO duaSearchVO = new DuaSearchVO();

    //ContractFundingDetailsVO - contractNumber
    duaSearchVO.setContractNum("HHS12132423");


    List<Object[]> result = duaSearchDaoImpl.searchDua(duaSearchVO);


    Assert.assertNotNull(result);
}

我得到了这个例外。

org.hibernate.QueryException: Criteria objects cannot be created directly on components.  Create a criteria on owning entity and use a dotted property to access component property: contractFundingDetails
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo(CriteriaQueryTranslator.java:274)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:230)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:112)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:86)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1568)

主类(DuaVO)

@Embedded
private ContractFundingDetailsVO contractFundingDetails;

嵌入式课程

@Embeddable
    public class ContractFundingDetailsVO implements Serializable
   {
       @Column(name="CNTRCT_NUM")
       private String contractNumber;
      }

1 个答案:

答案 0 :(得分:0)

解决。

我没有必要创建一个createCriteria()方法,因为没有连接。

    cr.add(Restrictions.eq
("Dua.contractFundingDetails.contractNumber", duaSearchVO.getContractNum()));

                flag = true;
            }