条件 - where 子句加入 table

Criteria - where clause on joined table

我在 MySQL 数据库中有两个表:COURSE 和 COURSE_SESSION.

课程有两个字段:代码(主键)和标题。

CourseSession 有一个名为 course_code 的字段,它是链接到课程元素的外键。

我想做这样的事情,但是使用 Java 中的标准 API :

SELECT * FROM COURSE_SESSION join COURSE 
WHERE course_code = code AND title like "%substring%";

两个表都使用休眠正确映射。

我试过这个:

Criteria criter = s.createCriteria(CourseSession.class);
        criter.add(Restrictions.like("courseCode.title", "%substring%"));

但我最终遇到了一个错误:

HTTP 500 - could not resolve property: courseCode.title of: org.xxx.core.entity.CourseSession

message could not resolve property: courseCode.title of: org.xxx.core.entity.CourseSession

exception

org.hibernate.QueryException: could not resolve property: courseCode.title of: fr.utbm.lo54.core.entity.CourseSession
    org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
    org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:96)
    org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
    org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1443)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:443)
    org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
    org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
    org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:114)
    org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:83)
    org.hibernate.loader.criteria.CriteriaLoader.(CriteriaLoader.java:92)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    fr.utbm.lo54.core.servlet.SearchData.doGet(SearchData.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

奇怪的是,如果我用 Restrictions.like("courseCode.code", "%substring%") 对 courseCode.code 进行限制,它工作正常。

编辑

Course.hbm.xml :

<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.xxx.core.entity">
    <class name="Course" table="COURSE">
        <id name="code" column="CODE">
            <generator class ="identity"/>
        </id>
    <property name="title" column="TITLE" not-null="true"/>
    </class>
</hibernate-mapping>

CourseSession.hbm.xml :

<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.xxx.core.entity">
    <class name="CourseSession" table="COURSE_SESSION">
        <id name="id" column="ID">
            <generator class="identity" />
        </id>

        <property name="startDate" column="START_DATE" not-null="true" />
        <property name="endDate" column="END_DATE" not-null="true" />
        <many-to-one name="courseCode" column="COURSE_CODE" />
        <many-to-one name="locationId" column="LOCATION_ID" />
    </class>
</hibernate-mapping>

首先为字段创建别名以实现此目的。

试试下面的代码,应该会有帮助

criter.createAlias("courseCode","courseCode");//course code is the association variable in your pojo class.