我是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)")
但它只返回该员工的所有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;
}
}
这是员工的模型:
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;
}
}
答案 0 :(得分:0)
我认为您应该使用与SQL中相同的内容:
喜欢&#39; I%&#39;
具体来说,根据文章@ 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。
<强>示例:强>
LIKE:代码%
stackoverflow问题中还包含其他示例。
答案 1 :(得分:0)
@Query(&#34;从Incident x中选择x,其中x.recipient.login =:login和(x.rfcnumber,如I%或x.rfcnumber = null)&#34; +&#34;和x.status NOT IN(8,6,18,7,24))&#34;
尝试此查询