EclipseLink- JPQL通过查询联接表

时间:2018-08-20 05:36:08

标签: sql eclipselink jpql

我的JPQl有问题。这与TrainRoute和TrainRouteStation是一对多的关系。我正在尝试创建一个内部联接并获取数据。当我使用mysql workbeanch并尝试将其转换为JPQL时,本机SQL查询正常工作。另外,我试图从2天开始修复。

错误:对象比较只能与OneToOneMappings一起使用。其他映射比较必须通过查询键或直接属性级别比较来完成。

课程:TrainRoute

    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id    
    @Column(name = "train_route_id", unique = true, nullable = false)
    public Long getTrainRouteId() {
        return this.trainRouteId;
    }

    public void setTrainRouteId(Long trainRouteId) {
        this.trainRouteId = trainRouteId;
    }

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER, mappedBy = "trainRoute")
    public List<TrainRouteStationData> getTrainRouteStations() {
        return this.trainRouteStations;
    }

    public void setTrainRouteStations(List<TrainRouteStationData> trainRouteStations) {
        this.trainRouteStations = trainRouteStations;
    }

类别:TrainRouteStation

    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id    
    @Column(name = "train_route_station_id", unique = true, nullable = false)
    public Long getTrainRouteStationId() {
        return this.trainRouteStationId;
    }

    public void setTrainRouteStationId(Long trainRouteStationId) {
        this.trainRouteStationId = trainRouteStationId;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "train_route_id", nullable = false)
    public TrainRouteData getTrainRoute() {
        return this.trainRoute;
    }

JPQL:"SELECT s FROM TrainRouteData t inner join TrainRouteStationData s ON t.trainRouteId=s.trainRoute where s.stationSeqN >=1 AND s.stationSeqN <=3 AND t.trainRouteDescX='Test1-Test2' order by s.stationSeqN asc"

本地SQL:SELECT train_route_station.* FROM train_route inner join train_route_station ON train_route.train_route_id=train_route_station.train_route_id where train_route_station.station_seq_n >= 1 AND train_route_station.station_seq_n <= 3 AND train_route.train_route_desc_x='Test1-Test2' order by train_route_station.station_seq_n asc

它抛出一个错误: 异常描述:对象比较只能与OneToOneMappings一起使用。其他映射比较必须通过查询键或直接属性级别比较来完成。 映射:[org.eclipse.persistence.mappings.DirectToFieldMapping [trainRouteId-> train_route.train_route_id]] 表达式:[ 查询键trainRouteId

如何更改该查询?

1 个答案:

答案 0 :(得分:1)

JPQL中的联接不是这样的。

正确的查询是

Public Sub MyProcedure()
    Dim range As Excel.Range = Nothing
    Try
        range = TryCast(Globals.ThisAddIn.Application.Selection, Excel.Range) 'must cast the selection as range or errors
        If range IsNot Nothing Then
            range.Validation.Delete()
        End If

    Catch
        'your error message here

    End Try

End Sub