简体   繁体   English

将sql转换为JPQL

[英]translate sql to JPQL

ima total newbie to JPQL ,so im working on a Spring Boot app and i have this SQL query part : ima完全是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%'

to JPQL. 到JPQL。

i tried doing a @Query like this : 我试着做这样的@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)")

but it only returns ALL the rfcnumber of the that employee , i want it to extract only the rfc number starting with letter I , i tried doing CONCAT from searching around in then web, same thing. 但它只返回该雇员的所有rfcnumber,我希望它仅提取以字母I开头的rfc编号,我试图通过在当时的网络中进行搜索来做CONCAT,同样的事情。 im new to this so i figure it'll be something much simpler , im thinking it's just syntax problem . 我对此很陌生,所以我认为这会简单得多,我认为这只是语法问题。

Thanks a bunch. 谢谢你

Edit (adding models): 编辑(添加模型):

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;
}

} }

And here's the model for Employee : 这是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;
}

} }

Hard-coded characters 硬编码字符

I think you should use the same as in SQL: 我认为您应该使用与SQL中相同的方法:

like 'I%' 就像“我%”

Specifically, according to the article @ http://www.objectdb.com/java/jpa/query/jpql/string#LIKE_-_String_Pattern_Matching_with_Wildcards_ : 具体来说,根据文章@ 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 块引用

So try the following: 因此,请尝试以下操作:

   @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)"

)

Parameters 参量

See the solutions @ Parameter in like clause JPQL if you are using a parameter. 如果使用参数,请参见like子句JPQL中的解决方案@ Parameter

Examples: 例子:

LIKE :code% 像:code%

Also other examples are included in the stackoverflow question. 另外,stackoverflow问题中还包含其他示例。

@Query("select x from Incident x where x.recipient.login=:login and (x.rfcnumber like I% or x.rfcnumber = null )" + " and x.status NOT IN (8,6,18,7,24))" @Query(“从事件x中选择x,其中x.recipient.login =:login和(x.rfcnumber如I%或x.rfcnumber = null)” +“和x.status NOT IN(8,6,18,7 ,24))”

try this query 试试这个查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM