当前位置: 首页 > 面试题库 >

Hibernate查询语法异常:org.hibernate.hql.ast.QuerySyntaxException:意外令牌

苏星宇
2023-03-14
问题内容

我有一个查询,我正在联接两个表ROuteMaster和RouteHalts。当我执行内部联接时

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1,

column 169 [SELECT  rm.id , rm.routeCode , rm.startPlaceId , rm.endPlaceId , 
rm.active, rm.linkedRoute FROM com.oprs.pojo.routes.RouteMaster rm  INNER JOIN 
RouteHalts rh  on rm.id = rh.routeId  WHERE  rh.placeId = :PlaceId  
ORDER BY  rm.id  ASC]

我搜索了该网站,发现了类似的问题及其答案。提到的问题是

Hibernate: org.hibernate.hql.ast.QuerySyntaxException: unexpected token

我在RouteHalts中为RouteMaster进行了多对一映射,在RouteHalts中为RouteMaster定义了getter和setter方法

<many-to-one name="RouteMaster" class="com.oprs.pojo.routes.RouteMaster" 
                    fetch="join"
            foreign-key="id" column="ROUTE_ID" insert="false"
            update="false" lazy="false" />

但仍然出现相同的错误。可以请一个人来指导我。

映射文件

<hibernate-mapping package="com.oprs.pojo.routes">
    <!-- Hibernate mapping for RouteMaster table -->
    <class name="RouteMaster" table="OPRS_ROUTE_MASTER">
        <id name="id" column="ROUTE_ID" type="java.lang.Long">
            <generator class="assigned" />
        </id>
        <property name="startPlaceId" column="START_PLACE_ID"/>
        <property name="endPlaceId" column="END_PLACE_ID"/>
        <property name="routeCode" column="ROUTE_CODE"/>
        <property name="routeName" column="ROUTE_NAME"/>
        <property name="active" column="IS_ACTIVE"/>
        <property name="linkedRoute" column="LINKED_ROUTE"/>
        <property name="returnRouteId" column="RET_ROUTE_ID"/>
        <!-- Auditor Information -->
        <component name="auditor" class="com.oprs.pojo.base.Auditor">
            <property name="createdBy" column="CREATED_BY" />
            <property name="createdDate" column="CREATED_DATE" />
            <property name="modifiedBy" column="MODIFIED_BY" />
            <property name="modifiedDate" column="MODIFIED_DATE" />
        </component>
        <many-to-one  name="RouteHalts" class="com.oprs.pojo.routes.RouteHalts" fetch="join"
            foreign-key="routeId" column="ROUTE_ID" insert="false"
            update="false" lazy="false" />
    </class>

    <!-- Hibernate mapping for RouteHalts table -->
    <class name="RouteHalts" table="OPRS_ROUTE_HALTS">
        <id name="id" column="HALTS_ID" type="java.lang.Long">
            <generator class="assigned" />
        </id>
        <property name="routeId" column="ROUTE_ID"/>
        <property name="placeId" column="PLACE_ID"/>
        <property name="seqNo" column="SEQ_NO"/>
        <property name="distanceKM" column="DISTANCE_KM"/>
        <property name="border" column="IS_BORDER"/>
        <property name="tolls" column="NO_OF_TOLLS"/>       
        <!-- Auditor Information -->
        <component name="auditor" class="com.oprs.pojo.base.Auditor">
            <property name="createdBy" column="CREATED_BY" />
            <property name="createdDate" column="CREATED_DATE" />
            <property name="modifiedBy" column="MODIFIED_BY" />
            <property name="modifiedDate" column="MODIFIED_DATE" />
        </component>
    </class>


POJO of Route Master

公共类RouteMaster扩展持久{

private static final long serialVersionUID = -5710336066048392949L;

private Long startPlaceId;
private Long endPlaceId;
private Long returnRouteId;
private String startPlaceCode;
private String endPlaceCode;
private String startPlaceName;
private String endPlaceName;
private String routeCode;
private String routeName;
private String active;
private Auditor auditor;
private boolean revervseRoute;
private String linkedRoute = AppConstants.N;
private Map<Double, RouteHalts> haltsMap;
private RouteHalts routeHalts;


public RouteHalts getRouteHalts() {
    return routeHalts;
}

public void setRouteHalts(RouteHalts routeHalts) {
    this.routeHalts = routeHalts;
}

public Long getStartPlaceId() {
    return startPlaceId;
}

public void setStartPlaceId(Long startPlaceId) {
    this.startPlaceId = startPlaceId;
}

public Long getEndPlaceId() {
    return endPlaceId;
}

public void setEndPlaceId(Long endPlaceId) {
    this.endPlaceId = endPlaceId;
}

public String getStartPlaceCode() {
return startPlaceCode;
}

public void setStartPlaceCode(String startPlaceCode) {
this.startPlaceCode = startPlaceCode;
}

public String getEndPlaceCode() {
return endPlaceCode;
}

public void setEndPlaceCode(String endPlaceCode) {
this.endPlaceCode = endPlaceCode;
}

public Long getReturnRouteId() {
    return returnRouteId;
}

public void setReturnRouteId(Long returnRouteId) {
    this.returnRouteId = returnRouteId;
}

public String getRouteCode() {
return routeCode;
}

public void setRouteCode(String routeCode) {
this.routeCode = routeCode;
}

public Auditor getAuditor() {
return auditor;
}

public void setAuditor(Auditor auditor) {
this.auditor = auditor;
}

public String getStartPlaceName() {
    return startPlaceName;
}

public void setStartPlaceName(String startPlaceName) {
    this.startPlaceName = startPlaceName;
}

public String getEndPlaceName() {
    return endPlaceName;
}

public void setEndPlaceName(String endPlaceName) {
    this.endPlaceName = endPlaceName;
}

public String getActive() {
    return active;
}

public void setActive(String active) {
    this.active = active;
}

public Map<Double, RouteHalts> getHaltsMap() {
    return haltsMap;
}

public void setHaltsMap(Map<Double, RouteHalts> haltsMap) {
    this.haltsMap = haltsMap;
}

public boolean isRevervseRoute() {
    return revervseRoute;
}

public void setRevervseRoute(boolean revervseRoute) {
    this.revervseRoute = revervseRoute;
}

public String getLinkedRoute() {
    return linkedRoute;
}

public void setLinkedRoute(String linkedRoute) {
    this.linkedRoute = linkedRoute;
}

public String getRouteName() {
    return routeName;
}

public void setRouteName(String routeName) {
    this.routeName = routeName;
}

}

POJO of RouteHalts

公共类RouteHalts扩展了Persistent {

private static final long serialVersionUID = -1491637903595290895L;
private Long placeId;
private Long routeId;
private String placeCode;
private Double seqNo;
private Double distanceKM;
private boolean border;
private Auditor auditor;

private String placeName;
private String stateCode;
private String stopType;
private String departureTime;
private Integer tolls;
private String platformNo;
private Long stopTypeId;
private Integer linkSequenceNo;
private String actualTime;
private int arrivalDay;

public String getStateCode() {
    return stateCode;
}

public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}

public Long getRouteId() {
    return routeId;
}

public void setRouteId(Long routeId) {
    this.routeId = routeId;
}

public Long getPlaceId() {
    return placeId;
}

public void setPlaceId(Long placeId) {
    this.placeId = placeId;
}

public String getPlaceCode() {
return placeCode;
}

public void setPlaceCode(String placeCode) {
this.placeCode = placeCode;
}

public Double getDistanceKM() {
return distanceKM;
}

public void setDistanceKM(Double distanceKM) {
this.distanceKM = distanceKM;
}

public boolean isBorder() {
    return border;
}

public void setBorder(boolean border) {
    this.border = border;
}

public Auditor getAuditor() {
return auditor;
}

public void setAuditor(Auditor auditor) {
this.auditor = auditor;
}

public String getPlaceName() {
    return placeName;
}

public void setPlaceName(String placeName) {
    this.placeName = placeName;
}

public Double getSeqNo() {
    return seqNo;
}

public void setSeqNo(Double seqNo) {
    this.seqNo = seqNo;
}

public String getStopType() {
    return stopType;
}

public void setStopType(String stopType) {
    this.stopType = stopType;
}

public String getDepartureTime() {
    return departureTime;
}

public void setDepartureTime(String departureTime) {
    this.departureTime = departureTime;
}

public Integer getTolls() {
    return tolls;
}

public void setTolls(Integer tolls) {
    this.tolls = tolls;
}

public String getPlatformNo() {
    return platformNo;
}

public void setPlatformNo(String platformNo) {
    this.platformNo = platformNo;
}

public Long getStopTypeId() {
    return stopTypeId;
}

public void setStopTypeId(Long stopTypeId) {
    this.stopTypeId = stopTypeId;
}

public Integer getLinkSequenceNo() {
    return linkSequenceNo;
}

public void setLinkSequenceNo(Integer linkSequenceNo) {
    this.linkSequenceNo = linkSequenceNo;
}

public int getArrivalDay() {
    return arrivalDay;
}

public void setArrivalDay(int arrivalDay) {
    this.arrivalDay = arrivalDay;
}

public String getActualTime() {
    return actualTime;
}

public void setActualTime(String actualTime) {
    this.actualTime = actualTime;
}

}


问题答案:

您不应在HQL中使用显式的“ JOIN ON”。相反,您可以在HQL中使用隐式联接:

SELECT  rm.id , rm.routeCode , rm.startPlaceId , rm.endPlaceId , rm.active , rm.linkedRoute 
FROM com.abhibus.oprs.pojo.routes.RouteMaster rm  
INNER JOIN rm.routeHalts rh WHERE  rh.placeId = :PlaceId  ORDER BY  rm.id  ASC

或者您可以使用Theta风格编写联接:

SELECT  rm.id , rm.routeCode , rm.startPlaceId , rm.endPlaceId , rm.active , rm.linkedRoute 
FROM com.abhibus.oprs.pojo.routes.RouteMaster rm, RouteHalts rh
WHERE rm.id = rh.routeId AND rh.placeId = :PlaceId  ORDER BY  rm.id  ASC

您也可以将查询作为本机SQL查询而不是HQL查询执行。为此,您应该使用

session.createSQLQuery(queryText);

代替

session.createQuery(queryText);

顺便说一句,在您的情况下,最好是获取整个实体,而不是分开的字段(列)?为此,您可以使用:

select rm from ...

这将返回List<RouteMaster>insted的的List<Object[]>



 类似资料:
  • 我有一个查询,其中我正在连接两个表ROuteMaster和routeHalts。当我执行内部连接时,我会得到 我在网站上搜索,发现了类似的问题和对它的回应。所提到的问题是 我在RouteHalts中为RouteMaster定义了多对一映射,我在RouteHalts中为RouteMaster定义了getter和setter方法 但仍然得到相同的错误。能请一个人给我指路吗。 映射文件 公共类Route

  • 我将Postgresql与Hibernate一起使用。工作正常,但时区UTC的抛出错误。 版本:PostgreSQL9.5。同样的sql查询在pgadmin中运行良好。 java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:意外标记:在第1行第167列附近[从com.hp.jam

  • My HQL: 我收到以下错误: 我是HQL的新手。我想通过代码动态设置间隔值。我设置了等于比较的参数值。我读过http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/post,但我不知道在没有其他符号的情况下如何设置timeOffSet值。 斯纳克斯。 编辑: 我试过?及其工作良好,但我想用来实现这一点。

  • 我在使用Struts2和Hibernate时遇到了以下错误: 我有这个方法: folling代码定义类之间的关系: Contrat类: Paiement类: PeriodePay等级: 此查询在phpMyAdmin中工作良好;你能告诉我出了什么问题吗 编辑1: 我在试着别搞错了: 我在控制台中得到结果: 但是使用jsp我在数据表中得不到结果,也许我必须修复迭代器

  • 我在执行查询时收到错误。错误是: org.hibernate.hql.internal.ast.查询同步异常:意外令牌:第1行,第239列附近的组[SELECTj.orderDate, MAX(j.endTime), MIN(j.startTime)fromfr.thelem.timesbatch.dao.entities.JobExecEntity j WHEREj.name=?0 ANDTO_

  • 我必须获得我的数据库(PostgreSQL)中的所有注册表,并使用不带大小写的。我试过使用标准,但ignoreCase()对我不起作用(我使用的是Hibernate3.6)。 我也尝试过使用ilike方法,但仍然不起作用。 这个版本也是: 因此,当我尝试在Hibernate中使用HQL创建查询时,会出现以下错误: 我的代码如下所示: 我做错了什么?