【问题标题】:translate sql to JPQL将 sql 转换为 JPQL
【发布时间】:2018-03-16 21:32:59
【问题描述】:

我是 JPQL 的新手,所以我正在开发一个 Spring Boot 应用程序,我有这个 SQL 查询部分:

select TOP 10 RFC_NUMBER, RECIPIENT_ID from [50004].SD_REQUEST S
INNER JOIN [50004].AM_EMPLOYEE E
--ON S.RECIPIENT_ID = E.EMPLOYEE_ID
WHERE E.AVAILABLE_FIELD_5  ='j.doe'  
        AND SD_REQUEST.STATUS_ID NOT IN (8,6,18,7,24)
        AND SD_REQUEST.RFC_NUMBER like 'I%'

到 JPQL。

我尝试过这样的@Query:

   @Query("select x from Incident x  Left join x.recipient recip where recip.login=:login and (x.rfcnumber like :I_% or :rfcnumber = null )"
+ " and x.status NOT IN (8,6,18,7,24)")

但它只返回该员工的所有 rfc 编号,我希望它只提取以字母 I 开头的 rfc 编号, 我尝试通过在网络上搜索来做 CONCAT,同样的事情。 我是新手,所以我认为它会更简单,我认为这只是语法问题。

非常感谢。

编辑(添加模型):

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="SD_REQUEST")
public class Incident implements Serializable {


private static final long serialVersionUID = -8235081865121541091L;


@Id
@Column(name="REQUEST_ID")
private Integer inid;

@ManyToOne
@JoinColumn(name = "SUBMITTED_BY")
private Employee sender;


@Column(name="RFC_NUMBER")
private String rfcnumber;

@Column(name="CREATION_DATE_UT")
private Date date;

@Column(name="DESCRIPTION")
private String description;

@Column(name="COMMENT")
private String comment;

@Column(name="STATUS_ID")
private Integer status;

@ManyToOne
@JoinColumn(name = "RECIPIENT_ID")
private Employee recipient;

public Incident()
{

}

public Incident(int inid,String rfcnumber,Date date,String description,String comment,Integer status)
{
    this.inid=inid; 
    this.rfcnumber= rfcnumber;
    this.date=date;
    this.description=description;
    this.comment=comment;
    this.status=status;

}

public int getInid() {
    return inid;
}

public void setInid(int inid) {
    this.inid = inid;
}

public String getRfcnumber() {
    return rfcnumber;
}

public void setRfcnumber(String rfcnumber) {
    this.rfcnumber = rfcnumber;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

public Employee getSender() {
    return sender;
}

public void setSender(Employee sender) {
    this.sender = sender;
}

public Employee getRecipient() {
    return recipient;
}

public void setRecipient(Employee recipient) {
    this.recipient = recipient;
}

public void setInid(Integer inid) {
    this.inid = inid;
}

}

这是 Employee 的模型:

import javax.persistence.*;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@Entity
@JsonDeserialize(as =Employee.class)
@Table(name = "AM_EMPLOYEE")
public class Employee implements Serializable {

    private static final long serialVersionUID = 5071617893593927440L;

@Id
@Column(name = "EMPLOYEE_ID" )
private Integer id;


@Column(name = "LAST_NAME")
private String lastName;


@Column(name = "AVAILABLE_FIELD_5")
private String login;

@OneToMany(mappedBy="sender")
@JsonIgnore
private List<Incident> myCreatedIncidents;  
@OneToMany(mappedBy="recipient")
@JsonIgnore
private List<Incident> myOtherIncidents;

@Column(name = "PASSWD")
private String password;

public Employee() {
//super();
}

public Employee (String login,String password)
{

}
public Employee(Integer id, String lastName,String login, String password) {
    this.id = id;
    this.lastName = lastName;
    this.login = login;
    this.password = password;

}

/**
 * @return the id
 */
public Integer getId() {
    return id;
}

/**
 * @param id
 *            the id to set
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 * @return the lastName
 */
public String getLastName() {
    return lastName;
}

/**
 * @param lastName
 *            the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}

/**
 * @return the login
 */
public String getLogin() {
    return login;
}

/**
 * @param login
 *            the login to set
 */
public void setLogin(String login) {
    this.login = login;
}

/**
 * @return the password
 */
public String getPassword() {
    return password;
}

/**
 * @param password
 *            the password to set
 */
public void setPassword(String password) {
    this.password = password;
}

public List<Incident> getMyCreatedIncidents() {
    return myCreatedIncidents;
}

public void setMyCreatedIncidents(List<Incident> myCreatedIncidents) {
    this.myCreatedIncidents = myCreatedIncidents;
}

public List<Incident> getMyOtherIncidents() {
    return myOtherIncidents;
}

public void setMyOtherIncidents(List<Incident> myOtherIncidents) {
    this.myOtherIncidents = myOtherIncidents;
}

}

【问题讨论】:

  • 为什么不查看调用的 SQL?然后你可以比较你是否正确。没有您发布类/字段,没有人可以告诉您 JPQL
  • 我将编辑帖子以包含豆类。
  • 这里是“我”的参数吗??

标签: jpa spring-boot jpql


【解决方案1】:

硬编码字符

我认为你应该使用与 SQL 中相同的:

喜欢“我%”

具体根据文章@http://www.objectdb.com/java/jpa/query/jpql/string#LIKE_-_String_Pattern_Matching_with_Wildcards_

百分比字符 (%) - 匹配零个或多个任意字符。 块引用

所以尝试以下方法:

   @Query("select x from Incident x  Left join x.recipient recip where recip.login=:login and (x.rfcnumber like 'I%' or :rfcnumber = null )"
+ " and x.status NOT IN (8,6,18,7,24)"

)

参数

如果您使用参数,请参阅解决方案@Parameter in like clause JPQL

示例:

喜欢 :code%

stackoverflow 问题中还包含其他示例。

【讨论】:

  • 您好,我已经亲身尝试过,我昨天已经检查了两个链接并尝试执行显示的操作,但它仍然是一样的。无论第一个字母如何,都显示所有 Rfc 编号结果。谢谢你
【解决方案2】:

@Query("select x from Incident x where x.recipient.login=:login and (x.rfcnumber like I% or x.rfcnumber = null)" + " 和 x.status 不在 (8,6,18,7,24))"

试试这个查询

【讨论】:

  • I 是某个 rfc 编号的第一个字母 .. 有 I141103_0002 或 R141103_0002 等形式的数字。所以是的,我只想拉那些有 I 并且我已经尝试过该查询,它没有返回我寻求的结果,从逻辑上讲,它应该返回,但由于某种原因,我得到的结果是所有的数字。 , LIKE 似乎不适用
猜你喜欢
  • 2014-08-02
  • 2011-09-29
  • 2019-10-01
  • 1970-01-01
  • 2019-07-13
  • 2021-01-22
  • 1970-01-01
  • 2014-03-08
  • 2011-03-25
相关资源
最近更新 更多