[英]Criteria - where clause on joined table
I have two tables in a MySQL database: COURSE and COURSE_SESSION. 我在MySQL数据库中有两个表:COURSE和COURSE_SESSION。
COURSE has two fields : code(primary key) and title. 课程有两个字段:代码(主键)和标题。
CourseSession has a field called course_code which is a foreign key linked to an element of Course. CourseSession有一个名为course_code的字段,它是链接到Course元素的外键。
I want to do somthing like this but using the Criteria API in Java : 我想做这样的事情,但是在Java中使用Criteria API:
SELECT * FROM COURSE_SESSION join COURSE WHERE course_code = code AND title like "%substring%";
Both tables are mapped correctly with hibernate. 两个表都已使用休眠正确映射。
I tried this : 我尝试了这个:
Criteria criter = s.createCriteria(CourseSession.class); criter.add(Restrictions.like("courseCode.title", "%substring%"));
But I end up with an error : 但是我最终遇到一个错误:
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)
The strange thing is that if I do a Restriction on courseCode.code with Restrictions.like("courseCode.code", "%substring%")
, it works fine. 奇怪的是,如果我对Restrictions.like("courseCode.code", "%substring%")
courseCode.code进行Restrictions.like("courseCode.code", "%substring%")
,则可以正常工作。
EDIT 编辑
Course.hbm.xml : 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 : 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>
create an alias for the field first to achieve this. 首先,为该字段创建一个别名。
try the below code, it should help 试试下面的代码,应该会有所帮助
criter.createAlias("courseCode","courseCode");//course code is the association variable in your pojo class.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.