【问题标题】:Hibernate EJB IllegalArgumentHibernate EJB IllegalArgument
【发布时间】:2011-02-25 21:46:03
【问题描述】:

我有这个神秘的休眠 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)

【问题讨论】:

    标签: java hibernate ejb ejb-3.1


    【解决方案1】:

    我会说 - 从查询中删除那个巨大的构造函数。您在查询中没有编译时安全性,因此您必须手动验证每个参数的类型和正确的顺序等 - 这是一个维护地狱。

    相反,选择要从中获取字段的实体并用纯 Java 构造对象。最好使用 Builder,而不是具有这么多参数的构造函数 - 这是一种“代码味道”

    【讨论】:

    • 和@alexR 和@hvgotcodes:你们说得有道理。这是维护地狱,特别是对于维护它的人(moi)。我认为构造函数中的参数数量是正确的,我可能在@alexr 提到的数据类型中忽略了这一点。我会仔细检查并给你们反馈。谢谢
    • 大家好,我放弃了休眠 QL 并决定编写一个存储过程来实现同样的目的,而不是把我的头撞到墙上。它现在可以工作了。感谢所有的帮助和建议
    • @Chinedu 在可维护性方面更糟
    【解决方案2】:

    您的 2 个构造函数各有 15 个参数(!!!!!!!!!!!!!!!HEAD EXPLODES!!!!!!!!!!!!!!!)

    在您的 hql 构造函数中,您将传递 14。

    异常意味着什么。用你拥有的构造函数和你认为你拥有的构造函数写的东西不是写的......另外,正如评论指出的那样,某些类型也很可能是不正确的。

    【讨论】:

    • 不,他也有 15 个参数在查询中。我数了两次。但没关系,有一个很好的规则:如果你的函数有超过 3 个参数,你至少会忘记一个。例如。永远不要写这样的 API。您没有发送给我们的课程是相关的。我相信您使用的某些参数类型错误。特别是检查 Long 和 Double。你确定所有 Long args 真的很长(不是 int)吗?
    • 如果包含无参数的构造函数,则有两个或三个构造函数。一个是 14 ,另一个是 15。
    • @chinedu,关键是另一个开发者不应该信任你;)他们应该能够在几秒钟内验证构造函数是否正确。
    • @hvgotcodes:你说得对,我说的也是。我只是在维护代码,我不是最初编写它的人。我还没有让它工作
    猜你喜欢
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2012-02-02
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多