标准 - 连接表

时间:2015-10-29 17:24:44

标签: java hibernate-criteria

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

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

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

我想做这样的事情但是在Java中使用Criteria API:

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

两个表都使用hibernate正确映射。

我试过了:

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>

1 个答案:

答案 0 :(得分:0)

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

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

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