在JPQL的本机查询中使用BETWEEN

问题描述

我试图在JPQL中使用Traceback (most recent call last): File "python",line 5,in <module> File "python",line 3,in printPreviousAndNext TypeError: can only concatenate str (not "int") to str 进行查询,我已经在SQL中对其进行了测试,并且可以正常工作,但是当我在JPQL中实现时,出现了错误:

BETWEEN

我不明白为什么Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ':'. Error Code: 102" 附近有错误,有人可以帮我解决吗?

这是我的代码:

:

解决方法

根据this answer,EclipseLink在使用?前缀而不是:的本机查询中设置命名参数的语法略有不同

public List<Object[]> reportPendapatan(String tahun) {
    String tahun1 = tahun +"-01-01";
    String tahun2 = tahun +"-12-31";
    return em.createNativeQuery(
        "SELECT p.tanggal,m.nama,pg.nama_jenis,g.nama_guru,pg.harga_masuk "
        + "FROM Pendaftaran p,Murid m,pengajian pg,Guru g "
        + "WHERE p.id_murid = m.id_murid and p.tanggal between ?tahun and ?tahun2 "
        + "and p.id_guru = g.id_guru and p.id_jenis = pg.id_jenis and p.status=4")
        .setParameter("tahun",tahun1)
        .setParameter("tahun2",tahun2)
        .getResultList(); 
}

其他选择是分别对tahun1tahun2使用位置参数 1和2:

    // ...
    return em.createNativeQuery(
        "SELECT p.tanggal,Guru g "
        + "WHERE p.id_murid = m.id_murid and p.tanggal between ? and ? "
        + "and p.id_guru = g.id_guru and p.id_jenis = pg.id_jenis and p.status=4")
        .setParameter(1,tahun1)
        .setParameter(2,tahun2)
        .getResultList(); 

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...