我正在尝试构建一个rest api应用程序。我正在构建的其余应用程序提供了一个mysql数据库的特定查询。我在理解@NamedQueries
时遇到了问题我想从sql查询中创建一个命名查询
SELECT C.*
FROM MDM_MASTER.Customer as C
INNER JOIN MDM_MASTER.SystemAccount as S
ON C.x_customerid = S.x_fkcustomer_x_customerid
WHERE S.x_datasourcename = 'Uber Proper' AND S.x_systemaccountid = 118096
AND S.x_accountstatus ='Active'
AND C.x_customercode = 'C001826' AND C.x_customerstatus = 'Active';
我做了名字查询
@NamedQuery( name = "Customer.findCustomerBySystemAccountIdAndCustomerCode"
, query = "SELECT cu FROM Customer cu INNER JOIN cu.systemAccounts as s "+
"WHERE s.xSystemaccountid = ?1 AND s.xDatasourcename = ?2 "+
"AND s.xAccountstatus = ?3 AND cu.xCustomercode = ?4 AND "+
"cu.xCustomerstatus = ?5")
但是当我在Controller中使用查询时,我得到的是null结果。我认为NamedQuery有问题。我做错了什么?
我会把大部分课程放在下面,以防其他问题出现。
CustomerController.java
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);
@Autowired
private CustomerRepository customerRepository;
@Autowired
private SystemAccountRepository systemAccountRepository;
@JsonSerialize
public class EmptyJsonResponse { }
//Example:
//http://localhost:8080/api/v1/customer/all?page=0&size=20
@RequestMapping(value="/all",method=RequestMethod.GET)
Page<Customer> findCustomers(Pageable pageable) {
return customerRepository.findCustomers(pageable);
}
//Example:
//http://localhost:8080/api/v1/systemaccount/bydatasourceid?x_datasourcename=Uber%20Proper&x_sourceaccountid=449211&page=0&size=20
@RequestMapping(value="/bydatasourceid",method=RequestMethod.GET)
public ResponseEntity findBySystemSourceId(Model model, @RequestParam(value = "xdatasourcename", required = true) String x_datasourcename,
@RequestParam(value = "xsourceaccountid", required = true ) String x_sourceaccountid,
@RequestParam(value = "xaccountstatus", required = false) String x_accountstatus ) {
LOGGER.info("Recieved bydatasourceid GET, xdatasourcename="+x_datasourcename+" "+"xsourceaccountid="+x_sourceaccountid+
" xaccountstatus="+x_accountstatus);
if (x_accountstatus==null ||x_accountstatus.equals(""))
x_accountstatus="Active";
SystemAccount systemAccount = systemAccountRepository.findBySystemSourceId(x_datasourcename, x_sourceaccountid, x_accountstatus );
LOGGER.info("Result "+systemAccount);
if(systemAccount==null || systemAccount.getCustomer()==null)
return new ResponseEntity(new CustomerController.EmptyJsonResponse(), HttpStatus.OK);
return new ResponseEntity(systemAccount.getCustomer(), HttpStatus.OK);
}
@RequestMapping(value="/bydatasourceidandcustomercode",method=RequestMethod.GET)
public ResponseEntity findCustomerbySystemAccountIdAndCustomerCode(Model model, @RequestParam(value = "xdatasourcename", required = true) String x_datasourcename,
@RequestParam(value = "xsourceaccountid", required = true ) String x_sourceaccountid,
@RequestParam(value = "xaccountstatus", required = false ) String x_accountstatus,
@RequestParam(value = "xcustomercode", required = true) String x_customercode,
@RequestParam(value = "xcustomerstatus", required = false) String x_customerstatus ) {
LOGGER.info("Recieved bydatasourceidandcustomercode GET, xdatasourcename="+x_datasourcename+" xsourceaccountid="+x_sourceaccountid+
" xaccountstatus="+x_accountstatus+" xcustomercode="+x_customercode+" xcustomerstatus="+x_customerstatus);
if (x_accountstatus==null ||x_accountstatus.equals(""))
x_accountstatus="Active";
if (x_customerstatus==null ||x_customerstatus.equals(""))
x_customerstatus="Active";
Customer customer = customerRepository.findCustomerBySystemAccountIdAndCustomerCode(x_datasourcename, x_sourceaccountid,
x_accountstatus, x_customercode, x_customerstatus );
LOGGER.info("Result "+customer);
if(customer==null )
return new ResponseEntity(new CustomerController.EmptyJsonResponse(), HttpStatus.OK);
return new ResponseEntity(customer, HttpStatus.OK);
}
}
Customer.Java
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.Date;
import java.math.BigInteger;
import java.util.List;
@Entity
@Table(name="customer")
@NamedQueries({
@NamedQuery( name = "Customer.findCustomers", query = "SELECT c FROM Customer c"),
@NamedQuery( name = "Customer.findCustomerBySystemAccountIdAndCustomerCode", query = "SELECT cu FROM Customer cu INNER JOIN cu.systemAccounts as s " +
"WHERE s.xSystemaccountid = ?1 AND s.xDatasourcename = ?2 AND s.xAccountstatus = ?3 " +
"AND cu.xCustomercode = ?4 AND cu.xCustomerstatus = ?5")
})
public class Customer {
@Id
@Column(name="x_customerid", unique=true, nullable=false, length=255)
private String xCustomerid;
@Column(name="x_createdbybatchid")
private Integer xCreatedbybatchid;
@Column(name="x_createdbyuser", length=255)
private String xCreatedbyuser;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_createddate", nullable=false)
private Date xCreateddate;
@Column(name="x_customercode", nullable=false, length=255)
private String xCustomercode;
@Column(name="x_customername", nullable=false, length=255)
private String xCustomername;
@Column(name="x_customerstatus", length=255)
private String xCustomerstatus;
@Column(name="x_hasaccounts")
private byte xHasaccounts;
@Column(name="x_modifiedbybatchid")
private Integer xModifiedbybatchid;
@Column(name="x_modifiedbyuser", length=255)
private String xModifiedbyuser;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_modifieddate")
private Date xModifieddate;
@Column(name="x_talend_task_id", length=255)
private String xTalendTaskId;
@Column(name="x_talend_timestamp", nullable=false)
private BigInteger xTalendTimestamp;
//bi-directional many-to-one association to SystemAccount
@OneToMany(mappedBy="customer" , fetch = FetchType.EAGER)
private List<SystemAccount> systemAccounts;
public Customer() {
}
public String getXCustomerid() {
return this.xCustomerid;
}
public void setXCustomerid(String xCustomerid) {
this.xCustomerid = xCustomerid;
}
public Integer getXCreatedbybatchid() {
return this.xCreatedbybatchid;
}
public void setXCreatedbybatchid(int xCreatedbybatchid) {
this.xCreatedbybatchid = xCreatedbybatchid;
}
public String getXCreatedbyuser() {
return this.xCreatedbyuser;
}
public void setXCreatedbyuser(String xCreatedbyuser) {
this.xCreatedbyuser = xCreatedbyuser;
}
public Date getXCreateddate() {
return this.xCreateddate;
}
public void setXCreateddate(Date xCreateddate) {
this.xCreateddate = xCreateddate;
}
public String getXCustomercode() {
return this.xCustomercode;
}
public void setXCustomercode(String xCustomercode) {
this.xCustomercode = xCustomercode;
}
public String getXCustomername() {
return this.xCustomername;
}
public void setXCustomername(String xCustomername) {
this.xCustomername = xCustomername;
}
public String getXCustomerstatus() {
return this.xCustomerstatus;
}
public void setXCustomerstatus(String xCustomerstatus) {
this.xCustomerstatus = xCustomerstatus;
}
public byte getXHasaccounts() {
return this.xHasaccounts;
}
public void setXHasaccounts(byte xHasaccounts) {
this.xHasaccounts = xHasaccounts;
}
public Integer getXModifiedbybatchid() {
return this.xModifiedbybatchid;
}
public void setXModifiedbybatchid(int xModifiedbybatchid) {
this.xModifiedbybatchid = xModifiedbybatchid;
}
public String getXModifiedbyuser() {
return this.xModifiedbyuser;
}
public void setXModifiedbyuser(String xModifiedbyuser) {
this.xModifiedbyuser = xModifiedbyuser;
}
public Date getXModifieddate() {
return this.xModifieddate;
}
public void setXModifieddate(Date xModifieddate) {
this.xModifieddate = xModifieddate;
}
public String getXTalendTaskId() {
return this.xTalendTaskId;
}
public void setXTalendTaskId(String xTalendTaskId) {
this.xTalendTaskId = xTalendTaskId;
}
public BigInteger getXTalendTimestamp() {
return this.xTalendTimestamp;
}
public void setXTalendTimestamp(BigInteger xTalendTimestamp) {
this.xTalendTimestamp = xTalendTimestamp;
}
@JsonIgnore
public List<SystemAccount> getSystemAccounts() {
return this.systemAccounts;
}
public void setSystemAccounts(List<SystemAccount> systemAccounts) {
this.systemAccounts = systemAccounts;
}
public SystemAccount addSystemAccount(SystemAccount systemAccount) {
getSystemAccounts().add(systemAccount);
systemAccount.setCustomer(this);
return systemAccount;
}
public SystemAccount removeSystemAccount(SystemAccount systemAccount) {
getSystemAccounts().remove(systemAccount);
systemAccount.setCustomer(null);
return systemAccount;
}
}
SystemAccount.java
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.Date;
import java.math.BigInteger;
@Entity
@Table(name="systemaccount")
@NamedQueries({
@NamedQuery( name = "SystemAccount.findSystemAccounts", query = "SELECT s FROM SystemAccount s"),
@NamedQuery( name = "SystemAccount.findBySystemSourceId", query = "SELECT s FROM SystemAccount s " +
"WHERE s.xDatasourcename = ?1 AND s.xSourceaccountid = ?2 AND s.xAccountstatus = ?3")
})
public class SystemAccount {
@Id
@Column(name="x_systemaccountid", unique=true, nullable=false, length=255)
private String xSystemaccountid;
@Column(name="x_accountlockedtocustomer", nullable=false)
private byte xAccountlockedtocustomer;
@Column(name="x_accountname", nullable=false, length=255)
private String xAccountname;
@Column(name="x_accountowner", length=255)
private String xAccountowner;
@Column(name="x_accountstatus", length=255)
private String xAccountstatus;
@Column(name="x_accounttype", length=255)
private String xAccounttype;
@Column(name="x_cleanhexid", length=255)
private String xCleanhexid;
@Column(name="x_createdbybatchid")
private Integer xCreatedbybatchid;
@Column(name="x_createdbyuser", length=255)
private String xCreatedbyuser;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_createddate", nullable=false)
private Date xCreateddate;
@Column(name="x_currencyisocode", length=255)
private String xCurrencyisocode;
@Column(name="x_datasourcename", nullable=false, length=255)
private String xDatasourcename;
@Column(name="x_ecaccountnumber", length=255)
private String xEcaccountnumber;
@Column(name="x_externalid", length=255)
private String xExternalid;
@Column(name="x_fkparentaccount_x_systemaccountid", length=255)
private String xFkparentaccountXSystemaccountid;
@Column(name="x_fkpartner_x_partneraccountid", length=255)
private String xFkpartnerXPartneraccountid;
@Column(name="x_fkwholesaler_x_wholesaleraccountid", length=255)
private String xFkwholesalerXWholesaleraccountid;
@Column(name="x_iscustomermaster")
private byte xIscustomermaster;
@Column(name="x_modifiedbybatchid")
private Integer xModifiedbybatchid;
@Column(name="x_modifiedbyuser", length=255)
private String xModifiedbyuser;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_modifieddate")
private Date xModifieddate;
@Column(name="x_naspid", length=255)
private String xNaspid;
@Column(name="x_normalizedaccountname", length=255)
private String xNormalizedaccountname;
@Column(name="x_sourceaccountid", nullable=false, length=255)
private String xSourceaccountid;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_sourcecreateddate")
private Date xSourcecreateddate;
@Column(name="x_sourcehexid", length=255)
private String xSourcehexid;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="x_sourcemodifieddate")
private Date xSourcemodifieddate;
@Column(name="x_talend_task_id", length=255)
private String xTalendTaskId;
@Column(name="x_talend_timestamp", nullable=false)
private BigInteger xTalendTimestamp;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name="x_fkcustomer_x_customerid")
private Customer customer;
public SystemAccount() {
}
public String getXSystemaccountid() {
return this.xSystemaccountid;
}
public void setXSystemaccountid(String xSystemaccountid) {
this.xSystemaccountid = xSystemaccountid;
}
public byte getXAccountlockedtocustomer() {
return this.xAccountlockedtocustomer;
}
public void setXAccountlockedtocustomer(byte xAccountlockedtocustomer) {
this.xAccountlockedtocustomer = xAccountlockedtocustomer;
}
public String getXAccountname() {
return this.xAccountname;
}
public void setXAccountname(String xAccountname) {
this.xAccountname = xAccountname;
}
public String getXAccountowner() {
return this.xAccountowner;
}
public void setXAccountowner(String xAccountowner) {
this.xAccountowner = xAccountowner;
}
public String getXAccountstatus() {
return this.xAccountstatus;
}
public void setXAccountstatus(String xAccountstatus) {
this.xAccountstatus = xAccountstatus;
}
public String getXAccounttype() {
return this.xAccounttype;
}
public void setXAccounttype(String xAccounttype) {
this.xAccounttype = xAccounttype;
}
public String getXCleanhexid() {
return this.xCleanhexid;
}
public void setXCleanhexid(String xCleanhexid) {
this.xCleanhexid = xCleanhexid;
}
public Integer getXCreatedbybatchid() {
return this.xCreatedbybatchid;
}
public void setXCreatedbybatchid(int xCreatedbybatchid) {
this.xCreatedbybatchid = xCreatedbybatchid;
}
public String getXCreatedbyuser() {
return this.xCreatedbyuser;
}
public void setXCreatedbyuser(String xCreatedbyuser) {
this.xCreatedbyuser = xCreatedbyuser;
}
public Date getXCreateddate() {
return this.xCreateddate;
}
public void setXCreateddate(Date xCreateddate) {
this.xCreateddate = xCreateddate;
}
public String getXCurrencyisocode() {
return this.xCurrencyisocode;
}
public void setXCurrencyisocode(String xCurrencyisocode) {
this.xCurrencyisocode = xCurrencyisocode;
}
public String getXDatasourcename() {
return this.xDatasourcename;
}
public void setXDatasourcename(String xDatasourcename) {
this.xDatasourcename = xDatasourcename;
}
public String getXEcaccountnumber() {
return this.xEcaccountnumber;
}
public void setXEcaccountnumber(String xEcaccountnumber) {
this.xEcaccountnumber = xEcaccountnumber;
}
public String getXExternalid() {
return this.xExternalid;
}
public void setXExternalid(String xExternalid) {
this.xExternalid = xExternalid;
}
public String getXFkparentaccountXSystemaccountid() {
return this.xFkparentaccountXSystemaccountid;
}
public void setXFkparentaccountXSystemaccountid(String xFkparentaccountXSystemaccountid) {
this.xFkparentaccountXSystemaccountid = xFkparentaccountXSystemaccountid;
}
public String getXFkpartnerXPartneraccountid() {
return this.xFkpartnerXPartneraccountid;
}
public void setXFkpartnerXPartneraccountid(String xFkpartnerXPartneraccountid) {
this.xFkpartnerXPartneraccountid = xFkpartnerXPartneraccountid;
}
public String getXFkwholesalerXWholesaleraccountid() {
return this.xFkwholesalerXWholesaleraccountid;
}
public void setXFkwholesalerXWholesaleraccountid(String xFkwholesalerXWholesaleraccountid) {
this.xFkwholesalerXWholesaleraccountid = xFkwholesalerXWholesaleraccountid;
}
public byte getXIscustomermaster() {
return this.xIscustomermaster;
}
public void setXIscustomermaster(byte xIscustomermaster) {
this.xIscustomermaster = xIscustomermaster;
}
public Integer getXModifiedbybatchid() {
return this.xModifiedbybatchid;
}
public void setXModifiedbybatchid(Integer xModifiedbybatchid) {
this.xModifiedbybatchid = xModifiedbybatchid;
}
public String getXModifiedbyuser() {
return this.xModifiedbyuser;
}
public void setXModifiedbyuser(String xModifiedbyuser) {
this.xModifiedbyuser = xModifiedbyuser;
}
public Date getXModifieddate() {
return this.xModifieddate;
}
public void setXModifieddate(Date xModifieddate) {
this.xModifieddate = xModifieddate;
}
public String getXNaspid() {
return this.xNaspid;
}
public void setXNaspid(String xNaspid) {
this.xNaspid = xNaspid;
}
public String getXNormalizedaccountname() {
return this.xNormalizedaccountname;
}
public void setXNormalizedaccountname(String xNormalizedaccountname) {
this.xNormalizedaccountname = xNormalizedaccountname;
}
public String getXSourceaccountid() {
return this.xSourceaccountid;
}
public void setXSourceaccountid(String xSourceaccountid) {
this.xSourceaccountid = xSourceaccountid;
}
public Date getXSourcecreateddate() {
return this.xSourcecreateddate;
}
public void setXSourcecreateddate(Date xSourcecreateddate) {
this.xSourcecreateddate = xSourcecreateddate;
}
public String getXSourcehexid() {
return this.xSourcehexid;
}
public void setXSourcehexid(String xSourcehexid) {
this.xSourcehexid = xSourcehexid;
}
public Date getXSourcemodifieddate() {
return this.xSourcemodifieddate;
}
public void setXSourcemodifieddate(Date xSourcemodifieddate) {
this.xSourcemodifieddate = xSourcemodifieddate;
}
public String getXTalendTaskId() {
return this.xTalendTaskId;
}
public void setXTalendTaskId(String xTalendTaskId) {
this.xTalendTaskId = xTalendTaskId;
}
public BigInteger getXTalendTimestamp() {
return this.xTalendTimestamp;
}
public void setXTalendTimestamp(BigInteger xTalendTimestamp) {
this.xTalendTimestamp = xTalendTimestamp;
}
@JsonIgnore
public Customer getCustomer() {
return this.customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
CustomerRepository.java
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Page<Customer> findCustomers(Pageable pageable);
Customer findCustomerBySystemAccountIdAndCustomerCode(String x_datasourcename, String x_sourceaccountid,
String x_accountstatus, String x_customercode, String x_customerstatus );
}