将 sql 翻译成 JPQL
translate sql to JPQL
我是 JPQL 的新手,所以我正在开发 Spring 启动应用程序,我有这个 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)")
但它只是 returns 该员工的所有 rfcnumber,我希望它只提取以字母 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 相同的方法:
like 'I%'
具体根据@http://www.objectdb.com/java/jpa/query/jpql/string#LIKE_-_String_Pattern_Matching_with_Wildcards_文章:
The percent character (%) - which matches zero or more of any character.
Blockquote
所以请尝试以下操作:
@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。
示例:
LIKE :code%
Whosebug 问题中还包含其他示例。
@查询("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))"
试试这个查询
我是 JPQL 的新手,所以我正在开发 Spring 启动应用程序,我有这个 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)")
但它只是 returns 该员工的所有 rfcnumber,我希望它只提取以字母 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 相同的方法:
like 'I%'
具体根据@http://www.objectdb.com/java/jpa/query/jpql/string#LIKE_-_String_Pattern_Matching_with_Wildcards_文章:
The percent character (%) - which matches zero or more of any character. Blockquote
所以请尝试以下操作:
@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。
示例:
LIKE :code%
Whosebug 问题中还包含其他示例。
@查询("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))"
试试这个查询