我有这个神秘的hibernate ql异常。它涉及三个类:
package com.systemspecs.Entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
/**
*
* @
* @author-Admin
*/
@Entity
@Table(name = "echequeupload")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
public class EchequeUpload implements Serializable {
private Long id;
private String bank_acronym = "";
private String accountHolderId = "";
private String accountNumber = "";
private Double trasanctionAmount = 0.0;
private String shortDesc = "";
private String longDesc = "";
private String filePath = "";
private Long batchNo = new Long(System.currentTimeMillis());
private Date transactionDate = new Date();
private String status = "OK";
private String idNumber = "";
private String surname = "";
private String firstname = "";
private String othernames = "";
private String switch_id = "";
private String accountType = "";
private String payPeriod = "";
private String company_id = "";
private String mobile = "";
private String email = "";
private String uploadType = "";
private String processed = "OK";
private Account account = null;
private String variance = null;
private String rejected = "N";
private Double grossAmount = 0.0;
private Double withholdingTax = 0.0;
private String taxOffice = "";
private Double whtRate = 0.0;
private String taxFlag = "OK";
private String bankBranch;
private String taxState;
private String bankName;
private Long dividendId;
private String customerNumber="";
/** Creates a new instance of PensionSalary */
public EchequeUpload() {
}
public EchequeUpload(String bank_acronym, String accountNumber, String accountType, Double trasanctionAmount,
String shortDesc, String longDesc, String surname, String mobile, String email, Double whtRate, String taxOffice,
Long batchNo, String company_id, Long dividendId,String customerNumber) {
this.bank_acronym = bank_acronym;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.trasanctionAmount = trasanctionAmount;
this.shortDesc = shortDesc;
this.longDesc = longDesc;
this.surname = surname;
this.mobile = mobile;
this.email = email;
this.whtRate = whtRate;
this.taxOffice = taxOffice;
this.batchNo = batchNo;
this.company_id = company_id;
this.dividendId=dividendId;
this.customerNumber=customerNumber;
}
public EchequeUpload(String bank_acronym, String accountNumber, String accountType, Double trasanctionAmount,
String shortDesc, String longDesc, String surname, String mobile, String email, Double whtRate, String taxOffice,
Long batchNo, String company_id, Long dividendId) {
this.bank_acronym = bank_acronym;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.trasanctionAmount = trasanctionAmount;
this.shortDesc = shortDesc;
this.longDesc = longDesc;
this.surname = surname;
this.mobile = mobile;
this.email = email;
this.whtRate = whtRate;
this.taxOffice = taxOffice;
this.batchNo = batchNo;
this.company_id = company_id;
this.dividendId=dividendId;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBank_acronym() {
return bank_acronym;
}
public void setBank_acronym(String bank_acronym) {
if (bank_acronym != null) {
this.bank_acronym = bank_acronym.trim();
}
}
public String getAccountHolderId() {
return accountHolderId;
}
public void setAccountHolderId(String accountHolderId) {
if (accountHolderId != null) {
this.accountHolderId = accountHolderId.trim();
}
}
public Double getTrasanctionAmount() {
return trasanctionAmount;
}
public void setTrasanctionAmount(Double trasanctionAmount) {
if (trasanctionAmount != null)
this.trasanctionAmount = trasanctionAmount;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
if (shortDesc != null) {
this.shortDesc = shortDesc.trim();
}
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
if (accountNumber != null) {
this.accountNumber = accountNumber.trim();
}
}
public String getLongDesc() {
return longDesc;
}
public void setLongDesc(String longDesc) {
if (longDesc != null) {
this.longDesc = longDesc.trim();
}
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
if (filePath != null) {
this.filePath = filePath.trim();
}
}
public Date getTransactionDate() {
return transactionDate;
}
public void setTransactionDate(Date transactionDate) {
this.transactionDate = transactionDate;
}
public Long getBatchNo() {
return batchNo;
}
public void setBatchNo(Long batchNo) {
this.batchNo = batchNo;
}
public String getStatus() {
return status;
}
public String getCustomerNumber() {
return customerNumber;
}
public void setCustomerNumber(String customerNumber) {
this.customerNumber = customerNumber;
}
public void setStatus(String status) {
if (status != null) {
this.status = status.trim();
}
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
if (accountType != null) {
this.accountType = accountType.trim();
}
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
if (email != null) {
this.email = email.trim();
}
}
public String getCompany_id() {
return company_id;
}
public void setCompany_id(String company_id) {
if (company_id != null) {
this.company_id = company_id.trim();
}
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
if (firstname != null) {
this.firstname = firstname.trim();
}
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
if (idNumber != null) {
this.idNumber = idNumber.trim();
}
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
if (mobile != null) {
this.mobile = mobile.trim();
}
}
public String getPayPeriod() {
return payPeriod;
}
public void setPayPeriod(String payPeriod) {
if (payPeriod != null) {
this.payPeriod = payPeriod.trim();
}
}
public String getProcessed() {
return processed;
}
public void setProcessed(String processed) {
if (processed != null) {
this.processed = processed.trim();
}
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
if (surname != null) {
this.surname = surname.trim();
}
}
public String getSwitch_id() {
return switch_id;
}
public void setSwitch_id(String switch_id) {
if (switch_id != null) {
this.switch_id = switch_id.trim();
}
}
public String getUploadType() {
return uploadType;
}
public void setUploadType(String uploadType) {
if (uploadType != null) {
this.uploadType = uploadType.trim();
}
}
public String getOthernames() {
return othernames;
}
public void setOthernames(String othernames) {
if (othernames != null) {
this.othernames = othernames.trim();
}
}
@ManyToOne
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
@Column
public String getVariance() {
return variance;
}
public void setVariance(String variance) {
this.variance = variance;
}
@Column
public String getRejected() {
return rejected;
}
public void setRejected(String rejected) {
this.rejected = rejected;
}
public Double getGrossAmount() {
return grossAmount;
}
public void setGrossAmount(Double grossAmount) {
if (grossAmount != null) {
this.grossAmount = grossAmount;
}
}
public String getTaxOffice() {
return taxOffice;
}
public void setTaxOffice(String taxOffice) {
this.taxOffice = taxOffice;
}
public Double getWithholdingTax() {
return withholdingTax;
}
public void setWithholdingTax(Double withholdingTax) {
if (withholdingTax != null) {
this.withholdingTax = withholdingTax;
}
}
public Double getWhtRate() {
return whtRate;
}
public void setWhtRate(Double whtRate) {
if (whtRate != null) {
this.whtRate = whtRate;
}
}
@Column(length = 4)
public String getTaxFlag() {
return taxFlag;
}
public void setTaxFlag(String taxFlag) {
if (taxFlag != null) {
this.taxFlag = taxFlag;
}
}
/**
* @return the bankBranch
*/
public String getBankBranch() {
return bankBranch;
}
/**
* @param bankBranch the bankBranch to set
*/
public void setBankBranch(String bankBranch) {
this.bankBranch = bankBranch;
}
/**
* @return the taxState
*/
public String getTaxState() {
return taxState;
}
/**
* @param taxState the taxState to set
*/
public void setTaxState(String taxState) {
this.taxState = taxState;
}
/**
* @return the bankName
*/
public String getBankName() {
return bankName;
}
/**
* @param bankName the bankName to set
*/
public void setBankName(String bankName) {
this.bankName = bankName;
}
/**
* @return the dividendId
*/
public Long getDividendId() {
return dividendId;
}
/**
* @param dividendId the dividendId to set
*/
public void setDividendId(Long dividendId) {
this.dividendId = dividendId;
}
}
其他两个类没有关系。我通过公共字段customerNumber
加入了两个不相关的类CustomerLimits和DividendUpload:
public List<EchequeUpload> findUploadedAllByTransactionIDAndCustomerNumber(
Long transaction_id) {
String ql = "select new EchequeUpload(c.account.switch.switchId, c.account.accountnumber, c.account.accountType, nvl(d.amount,0), ";
ql += "d.shortDescription, d.longDescription, c.accountHolder.name, c.accountHolder.mobile, c.accountHolder.email, nvl(d.tax,0), d.taxState, ";
ql += "d.batchNo, d.company_id, d.id,d.customerNumber) from DividendUpload d , CustomerLimits c where d.customerNumber=c.customerNumber and d.transaction_id=?1";
return this.entityManager.createQuery(ql).setParameter(1,
transaction_id).setHint("org.hibernate.cacheable",
new Boolean(true)).setHint("org.hibernate.readOnly",
new Boolean(true)).getResultList();
}
现在的问题是它一直给我这个错误。 IllegalArgumentException自然会表明我的构造函数不正确但它是。事实上它是非常正确的,所以我在这里不知所措。
2011-02-25 15:37:34,968 ERROR [org.hibernate.hql.PARSER] Unable to locate appropriate constructor on class [com.systemspecs.Entity.EchequeUpload]
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.systemspecs.Entity.EchequeUpload]
2011-02-25 15:37:35,015 INFO [STDOUT] In error================================:
2011-02-25 15:37:35,015 ERROR [STDERR] java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.systemspecs.Entity.EchequeUpload] [select new EchequeUpload(c.account.switch.switchId, c.account.accountnumber, c.account.accountType, nvl(d.amount,0), d.shortDescription, d.longDescription, c.accountHolder.name, c.accountHolder.mobile, c.accountHolder.email, nvl(d.tax,0), d.taxState, d.batchNo, d.company_id, d.id,d.customerNumber) from com.systemspecs.Entity.DividendUpload d , com.systemspecs.Entity.CustomerLimits c where d.customerNumber=c.customerNumber and d.transaction_id=?1]
2011-02-25 15:37:35,031 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:616)
2011-02-25 15:37:35,031 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:95)
2011-02-25 15:37:35,046 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManager.createQuery(TransactionScopedEntityManager.java:134)
2011-02-25 15:37:35,046 ERROR [STDERR] at com.systemspecs.dao.EchequeUploadDAO.findUploadedAllByTransactionIDAndCustomerNumber(EchequeUploadDAO.java:103)
2011-02-25 15:37:35,046 ERROR [STDERR] at com.systemspecs.businessobjects.EchequeUploadService2.saveEchequeUpload(EchequeUploadService2.java:705)
2011-02-25 15:37:35,046 ERROR [STDERR] at com.systemspecs.businessobjects.EchequeUploadService2$$FastClassByCGLIB$$c253fe58.invoke(<generated>)
2011-02-25 15:37:35,046 ERROR [STDERR] at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
2011-02-25 15:37:35,046 ERROR [STDERR] at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
答案 0 :(得分:2)
我会说 - 从查询中删除那个巨大的构造函数。您在查询中没有编译时安全性,因此您必须手动验证每个参数的类型和正确的顺序等 - 这是一个维护地狱。
相反,选择要从中获取字段的实体并使用纯Java构造对象。最好使用Builder,而不是具有如此多参数的构造函数 - 这就是“代码味道”
答案 1 :(得分:2)
你的2个构造函数各占15个参数(!!!!!!!!!!!!!! HEAD EXPLODES !!!!!!!!!!!!!!!!)
在您的hql构造函数中传递了14.
异常意味着它意味着什么。 aint用你拥有的构造函数和你认为你拥有的构造函数编写....还有,正如评论所指出的那样,某些类型也是不正确的。