在JPQL中使用BETWEEN原生查询

huangapple go评论81阅读模式
英文:

Using BETWEEN in native query in JPQL

问题

我尝试使用JPQL中的BETWEEN来进行查询,我已经在SQL中进行了测试,它是有效的,但是当我在JPQL中实现时,出现了错误:

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"

我不明白为什么在:附近会出现错误,有人可以帮我解决吗?

以下是我的代码:

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(); 
}
英文:

I tried to make a query using BETWEEN in JPQL, I already tested it in SQL and it works, but when I implemented in JPQL I got an error:

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 &#39;:&#39;.
Error Code: 102&quot;

I don't understand why there is the error near :, can anybody help me figure it out?

Here is my code:

public List&lt;Object[]&gt; reportPendapatan(String tahun) {
    String tahun1 = tahun +&quot;-01-01&quot;;
    String tahun2 = tahun +&quot;-12-31&quot;;
    return em.createNativeQuery(&quot;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&quot;)       
            .setParameter(&quot;tahun&quot;, tahun1)
            .setParameter(&quot;tahun2&quot;, tahun2)
            .getResultList(); 
}

答案1

得分: 1

根据这个答案,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 ? 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(); 
}

另一种选择是使用_位置参数_1和2来表示tahun1tahun2

    // ...
    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 ? 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(); 
英文:

According to this answer, EclipseLink has slightly different syntax of setting named parameters in native queries using ? prefix instead of :

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

Other option is to use positional parameters 1 and 2 for tahun1 and tahun2 respectively:

    // ...
    return em.createNativeQuery(
        &quot;SELECT p.tanggal, m.nama, pg.nama_jenis, g.nama_guru,pg.harga_masuk &quot;
        + &quot;FROM Pendaftaran p, Murid m, pengajian pg, Guru g &quot;
        + &quot;WHERE p.id_murid = m.id_murid and p.tanggal between ? and ? &quot;
        + &quot;and p.id_guru = g.id_guru and p.id_jenis = pg.id_jenis and p.status=4&quot;)
        .setParameter(1, tahun1)
        .setParameter(2, tahun2)
        .getResultList(); 

huangapple
  • 本文由 发表于 2020年9月7日 20:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63777748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定