首页 > 解决方案 > 执行 count 语句时 JPA 和 Hibernate 库的问题

问题描述

在过去一周左右的时间里,我一直试图让这个 JPA 库正常工作,但毫无结果。

我可以执行简单的查询,例如通过 id 查找实体,或获取实体集合等,但是,当我尝试执行更复杂的查询时,我似乎总是遇到错误。有人可以帮助调试这个产生以下错误堆栈跟踪的 COUNT 语句:

Caused by: org.hibernate.query.sqm.InterpretationException: Error interpreting query [SELECT COUNT(a.id) FROM AgreementEntity a]; this may indicate a semantic (user query) problem or a bug in the parser
Caused by: java.lang.NullPointerException

这是导致错误的代码:

    EntityManager em = getFirstEntityManager();
    Session session = em.unwrap(Session.class);
    // The following line of code causes an error
    session.createQuery("SELECT COUNT(a.id) FROM AgreementEntity a");

这是整个AgreementEntity班级:


package com.profectus.rdm.entities;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Objects;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "agreement", schema = "rdm_demovendorxa_prod", catalog = "")
public class AgreementEntity {
  private Long id;
  private Long version;
  private String agreementType;
  private Boolean autoextend;
  private Boolean isAutoextendAgreement;
  private Long parentAgreementId;
  private String comments;
  private Timestamp creationDate;
  private String datasource;
  private String description;
  private Timestamp endDate;
  private String fEmail;
  private String fName;
  private String fPhone;
  private Boolean journal;
  private Boolean pdfemail;
  private String sEmail;
  private String sName;
  private String sPhone;
  private Timestamp startDate;
  private String status;
  private String terms;
  private String termsAmendments;
  private Boolean vendorClaims;
  private String notifyEmail;
  private Timestamp ceaseDate;
  private String sTitle;
  private String sMobile;
  private String sFax;
  private String fTitle;
  private String fMobile;
  private String fFax;
  private String ceaseReason;
  private Boolean isCeasedAlerted;
  private Integer isExpiringAlerted;
  private Timestamp contractEndDate;
  private String contractReference;
  private String confidenceLevel;
  private String collectionMethod;
  private Boolean autoextended;
  private String arNumber;
  private String journalPostingCompanyCode;
  private Long marketingEventId;
  private UsersEntity usersByLeadBuyerId;
  private UsersEntity usersByBuyerId;
  private RetailerVendorEntity retailerVendorByRetailerVendorId;
  private RetailerVendorEntity retailerVendorByDistributorRetailerVendorId;
  private UsersEntity usersByUserId;
  private CountryEntity countryByCountryId;
  private CountryEntity countryByCountryId_0;
  private Collection<AgreementAttachmentEntity> agreementAttachmentsById;
  private Collection<AgreementCommentEntity> agreementCommentsById;
  private Collection<AgreementCopyEntity> agreementCopiesById;
  private Collection<AgreementCopyEntity> agreementCopiesById_0;
  //  private Collection<AgreementDistributorRetailerVendorEntity> agreementDistributorRetailerVendorsById;
//  private Collection<AgreementDistributorVendorGroupEntity> agreementDistributorVendorGroupsById;
  private Collection<AgreementHistoryEntity> agreementHistoriesById;
  private Collection<AgreementImportEntity> agreementImportsById;
  private Collection<AgreementNoteEntity> agreementNotesById;
  private Collection<AttachmentEntity> attachmentsById;
  private Collection<RuleEntity> rulesById;
  private Collection<TierReportResultEntity> tierReportResultsById;

  @Id
  @Column(name = "id", nullable = false)
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Basic
  @Column(name = "version", nullable = true)
  public Long getVersion() {
    return version;
  }

  public void setVersion(Long version) {
    this.version = version;
  }

  @Basic
  @Column(name = "agreement_type", nullable = true, length = 255)
  public String getAgreementType() {
    return agreementType;
  }

  public void setAgreementType(String agreementType) {
    this.agreementType = agreementType;
  }

  @Basic
  @Column(name = "autoextend", nullable = false)
  public Boolean getAutoextend() {
    return autoextend;
  }

  public void setAutoextend(Boolean autoextend) {
    this.autoextend = autoextend;
  }

  @Basic
  @Column(name = "is_autoextend_agreement", nullable = true)
  public Boolean getAutoextendAgreement() {
    return isAutoextendAgreement;
  }

  public void setAutoextendAgreement(Boolean autoextendAgreement) {
    isAutoextendAgreement = autoextendAgreement;
  }

  @Basic
  @Column(name = "parent_agreement_id", nullable = true)
  public Long getParentAgreementId() {
    return parentAgreementId;
  }

  public void setParentAgreementId(Long parentAgreementId) {
    this.parentAgreementId = parentAgreementId;
  }

  @Basic
  @Column(name = "comments", nullable = true, length = -1)
  public String getComments() {
    return comments;
  }

  public void setComments(String comments) {
    this.comments = comments;
  }

  @Basic
  @Column(name = "creation_date", nullable = false)
  public Timestamp getCreationDate() {
    return creationDate;
  }

  public void setCreationDate(Timestamp creationDate) {
    this.creationDate = creationDate;
  }

  @Basic
  @Column(name = "datasource", nullable = false, length = 255)
  public String getDatasource() {
    return datasource;
  }

  public void setDatasource(String datasource) {
    this.datasource = datasource;
  }

  @Basic
  @Column(name = "description", nullable = true, length = 255)
  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @Basic
  @Column(name = "end_date", nullable = true)
  public Timestamp getEndDate() {
    return endDate;
  }

  public void setEndDate(Timestamp endDate) {
    this.endDate = endDate;
  }

  @Basic
  @Column(name = "fEmail", nullable = true, length = 255)
  public String getfEmail() {
    return fEmail;
  }

  public void setfEmail(String fEmail) {
    this.fEmail = fEmail;
  }

  @Basic
  @Column(name = "fName", nullable = true, length = 255)
  public String getfName() {
    return fName;
  }

  public void setfName(String fName) {
    this.fName = fName;
  }

  @Basic
  @Column(name = "fPhone", nullable = true, length = 255)
  public String getfPhone() {
    return fPhone;
  }

  public void setfPhone(String fPhone) {
    this.fPhone = fPhone;
  }

  @Basic
  @Column(name = "journal", nullable = false)
  public Boolean getJournal() {
    return journal;
  }

  public void setJournal(Boolean journal) {
    this.journal = journal;
  }

  @Basic
  @Column(name = "pdfemail", nullable = false)
  public Boolean getPdfemail() {
    return pdfemail;
  }

  public void setPdfemail(Boolean pdfemail) {
    this.pdfemail = pdfemail;
  }

  @Basic
  @Column(name = "sEmail", nullable = true, length = 255)
  public String getsEmail() {
    return sEmail;
  }

  public void setsEmail(String sEmail) {
    this.sEmail = sEmail;
  }

  @Basic
  @Column(name = "sName", nullable = true, length = 255)
  public String getsName() {
    return sName;
  }

  public void setsName(String sName) {
    this.sName = sName;
  }

  @Basic
  @Column(name = "sPhone", nullable = true, length = 255)
  public String getsPhone() {
    return sPhone;
  }

  public void setsPhone(String sPhone) {
    this.sPhone = sPhone;
  }

  @Basic
  @Column(name = "start_date", nullable = true)
  public Timestamp getStartDate() {
    return startDate;
  }

  public void setStartDate(Timestamp startDate) {
    this.startDate = startDate;
  }

  @Basic
  @Column(name = "status", nullable = false, length = 255)
  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  @Basic
  @Column(name = "terms", nullable = true, length = -1)
  public String getTerms() {
    return terms;
  }

  public void setTerms(String terms) {
    this.terms = terms;
  }

  @Basic
  @Column(name = "terms_amendments", nullable = true, length = -1)
  public String getTermsAmendments() {
    return termsAmendments;
  }

  public void setTermsAmendments(String termsAmendments) {
    this.termsAmendments = termsAmendments;
  }

  @Basic
  @Column(name = "vendorClaims", nullable = false)
  public Boolean getVendorClaims() {
    return vendorClaims;
  }

  public void setVendorClaims(Boolean vendorClaims) {
    this.vendorClaims = vendorClaims;
  }

  @Basic
  @Column(name = "notify_email", nullable = true, length = 512)
  public String getNotifyEmail() {
    return notifyEmail;
  }

  public void setNotifyEmail(String notifyEmail) {
    this.notifyEmail = notifyEmail;
  }

  @Basic
  @Column(name = "cease_date", nullable = true)
  public Timestamp getCeaseDate() {
    return ceaseDate;
  }

  public void setCeaseDate(Timestamp ceaseDate) {
    this.ceaseDate = ceaseDate;
  }

  @Basic
  @Column(name = "sTitle", nullable = true, length = 255)
  public String getsTitle() {
    return sTitle;
  }

  public void setsTitle(String sTitle) {
    this.sTitle = sTitle;
  }

  @Basic
  @Column(name = "sMobile", nullable = true, length = 255)
  public String getsMobile() {
    return sMobile;
  }

  public void setsMobile(String sMobile) {
    this.sMobile = sMobile;
  }

  @Basic
  @Column(name = "sFax", nullable = true, length = 255)
  public String getsFax() {
    return sFax;
  }

  public void setsFax(String sFax) {
    this.sFax = sFax;
  }

  @Basic
  @Column(name = "fTitle", nullable = true, length = 255)
  public String getfTitle() {
    return fTitle;
  }

  public void setfTitle(String fTitle) {
    this.fTitle = fTitle;
  }

  @Basic
  @Column(name = "fMobile", nullable = true, length = 255)
  public String getfMobile() {
    return fMobile;
  }

  public void setfMobile(String fMobile) {
    this.fMobile = fMobile;
  }

  @Basic
  @Column(name = "fFax", nullable = true, length = 255)
  public String getfFax() {
    return fFax;
  }

  public void setfFax(String fFax) {
    this.fFax = fFax;
  }

  @Basic
  @Column(name = "cease_reason", nullable = true, length = 255)
  public String getCeaseReason() {
    return ceaseReason;
  }

  public void setCeaseReason(String ceaseReason) {
    this.ceaseReason = ceaseReason;
  }

  @Basic
  @Column(name = "is_ceased_alerted", nullable = true)
  public Boolean getCeasedAlerted() {
    return isCeasedAlerted;
  }

  public void setCeasedAlerted(Boolean ceasedAlerted) {
    isCeasedAlerted = ceasedAlerted;
  }

  @Basic
  @Column(name = "is_expiring_alerted", nullable = true)
  public Integer getIsExpiringAlerted() {
    return isExpiringAlerted;
  }

  public void setIsExpiringAlerted(Integer isExpiringAlerted) {
    this.isExpiringAlerted = isExpiringAlerted;
  }

  @Basic
  @Column(name = "contract_end_date", nullable = true)
  public Timestamp getContractEndDate() {
    return contractEndDate;
  }

  public void setContractEndDate(Timestamp contractEndDate) {
    this.contractEndDate = contractEndDate;
  }

  @Basic
  @Column(name = "contract_reference", nullable = true, length = 50)
  public String getContractReference() {
    return contractReference;
  }

  public void setContractReference(String contractReference) {
    this.contractReference = contractReference;
  }

  @Basic
  @Column(name = "confidence_level", nullable = true, length = 50)
  public String getConfidenceLevel() {
    return confidenceLevel;
  }

  public void setConfidenceLevel(String confidenceLevel) {
    this.confidenceLevel = confidenceLevel;
  }

  @Basic
  @Column(name = "collection_method", nullable = true, length = 2)
  public String getCollectionMethod() {
    return collectionMethod;
  }

  public void setCollectionMethod(String collectionMethod) {
    this.collectionMethod = collectionMethod;
  }

  @Basic
  @Column(name = "autoextended", nullable = true)
  public Boolean getAutoextended() {
    return autoextended;
  }

  public void setAutoextended(Boolean autoextended) {
    this.autoextended = autoextended;
  }

  @Basic
  @Column(name = "ar_number", nullable = true, length = 50)
  public String getArNumber() {
    return arNumber;
  }

  public void setArNumber(String arNumber) {
    this.arNumber = arNumber;
  }

  @Basic
  @Column(name = "journal_posting_company_code", nullable = true, length = 50)
  public String getJournalPostingCompanyCode() {
    return journalPostingCompanyCode;
  }

  public void setJournalPostingCompanyCode(String journalPostingCompanyCode) {
    this.journalPostingCompanyCode = journalPostingCompanyCode;
  }

  @Basic
  @Column(name = "marketing_event_id", nullable = true)
  public Long getMarketingEventId() {
    return marketingEventId;
  }

  public void setMarketingEventId(Long marketingEventId) {
    this.marketingEventId = marketingEventId;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    AgreementEntity that = (AgreementEntity) o;
    return Objects.equals(id, that.id) &&
        Objects.equals(version, that.version) &&
        Objects.equals(agreementType, that.agreementType) &&
        Objects.equals(autoextend, that.autoextend) &&
        Objects.equals(isAutoextendAgreement, that.isAutoextendAgreement) &&
        Objects.equals(parentAgreementId, that.parentAgreementId) &&
        Objects.equals(comments, that.comments) &&
        Objects.equals(creationDate, that.creationDate) &&
        Objects.equals(datasource, that.datasource) &&
        Objects.equals(description, that.description) &&
        Objects.equals(endDate, that.endDate) &&
        Objects.equals(fEmail, that.fEmail) &&
        Objects.equals(fName, that.fName) &&
        Objects.equals(fPhone, that.fPhone) &&
        Objects.equals(journal, that.journal) &&
        Objects.equals(pdfemail, that.pdfemail) &&
        Objects.equals(sEmail, that.sEmail) &&
        Objects.equals(sName, that.sName) &&
        Objects.equals(sPhone, that.sPhone) &&
        Objects.equals(startDate, that.startDate) &&
        Objects.equals(status, that.status) &&
        Objects.equals(terms, that.terms) &&
        Objects.equals(termsAmendments, that.termsAmendments) &&
        Objects.equals(vendorClaims, that.vendorClaims) &&
        Objects.equals(notifyEmail, that.notifyEmail) &&
        Objects.equals(ceaseDate, that.ceaseDate) &&
        Objects.equals(sTitle, that.sTitle) &&
        Objects.equals(sMobile, that.sMobile) &&
        Objects.equals(sFax, that.sFax) &&
        Objects.equals(fTitle, that.fTitle) &&
        Objects.equals(fMobile, that.fMobile) &&
        Objects.equals(fFax, that.fFax) &&
        Objects.equals(ceaseReason, that.ceaseReason) &&
        Objects.equals(isCeasedAlerted, that.isCeasedAlerted) &&
        Objects.equals(isExpiringAlerted, that.isExpiringAlerted) &&
        Objects.equals(contractEndDate, that.contractEndDate) &&
        Objects.equals(contractReference, that.contractReference) &&
        Objects.equals(confidenceLevel, that.confidenceLevel) &&
        Objects.equals(collectionMethod, that.collectionMethod) &&
        Objects.equals(autoextended, that.autoextended) &&
        Objects.equals(arNumber, that.arNumber) &&
        Objects.equals(journalPostingCompanyCode, that.journalPostingCompanyCode) &&
        Objects.equals(marketingEventId, that.marketingEventId);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, version, agreementType, autoextend, isAutoextendAgreement, parentAgreementId, comments, creationDate, datasource, description, endDate, fEmail, fName, fPhone, journal, pdfemail, sEmail, sName, sPhone, startDate, status, terms, termsAmendments, vendorClaims, notifyEmail, ceaseDate, sTitle, sMobile, sFax, fTitle, fMobile, fFax, ceaseReason, isCeasedAlerted, isExpiringAlerted, contractEndDate, contractReference, confidenceLevel, collectionMethod, autoextended, arNumber, journalPostingCompanyCode, marketingEventId);
  }

  @ManyToOne
  @JoinColumn(name = "lead_buyer_id", referencedColumnName = "id")
  public UsersEntity getUsersByLeadBuyerId() {
    return usersByLeadBuyerId;
  }

  public void setUsersByLeadBuyerId(UsersEntity usersByLeadBuyerId) {
    this.usersByLeadBuyerId = usersByLeadBuyerId;
  }

  @ManyToOne
  @JoinColumn(name = "buyer_id", referencedColumnName = "id")
  public UsersEntity getUsersByBuyerId() {
    return usersByBuyerId;
  }

  public void setUsersByBuyerId(UsersEntity usersByBuyerId) {
    this.usersByBuyerId = usersByBuyerId;
  }

  @ManyToOne
  @JoinColumn(name = "retailer_vendor_id", referencedColumnName = "id")
  public RetailerVendorEntity getRetailerVendorByRetailerVendorId() {
    return retailerVendorByRetailerVendorId;
  }

  public void setRetailerVendorByRetailerVendorId(RetailerVendorEntity retailerVendorByRetailerVendorId) {
    this.retailerVendorByRetailerVendorId = retailerVendorByRetailerVendorId;
  }

  @ManyToOne
  @JoinColumn(name = "distributor_retailer_vendor_id", referencedColumnName = "id")
  public RetailerVendorEntity getRetailerVendorByDistributorRetailerVendorId() {
    return retailerVendorByDistributorRetailerVendorId;
  }

  public void setRetailerVendorByDistributorRetailerVendorId(RetailerVendorEntity retailerVendorByDistributorRetailerVendorId) {
    this.retailerVendorByDistributorRetailerVendorId = retailerVendorByDistributorRetailerVendorId;
  }

  @ManyToOne
  @JoinColumn(name = "user_id", referencedColumnName = "id")
  public UsersEntity getUsersByUserId() {
    return usersByUserId;
  }

  public void setUsersByUserId(UsersEntity usersByUserId) {
    this.usersByUserId = usersByUserId;
  }

  @ManyToOne
  @JoinColumn(name = "country_id", referencedColumnName = "id")
  public CountryEntity getCountryByCountryId() {
    return countryByCountryId;
  }

  public void setCountryByCountryId(CountryEntity countryByCountryId) {
    this.countryByCountryId = countryByCountryId;
  }

  @ManyToOne
  @JoinColumn(name = "country_id", referencedColumnName = "id", insertable = false, updatable = false)
  public CountryEntity getCountryByCountryId_0() {
    return countryByCountryId_0;
  }

  public void setCountryByCountryId_0(CountryEntity countryByCountryId_0) {
    this.countryByCountryId_0 = countryByCountryId_0;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AgreementAttachmentEntity> getAgreementAttachmentsById() {
    return agreementAttachmentsById;
  }

  public void setAgreementAttachmentsById(Collection<AgreementAttachmentEntity> agreementAttachmentsById) {
    this.agreementAttachmentsById = agreementAttachmentsById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AgreementCommentEntity> getAgreementCommentsById() {
    return agreementCommentsById;
  }

  public void setAgreementCommentsById(Collection<AgreementCommentEntity> agreementCommentsById) {
    this.agreementCommentsById = agreementCommentsById;
  }

  @OneToMany(mappedBy = "agreementByParentAgreementId")
  public Collection<AgreementCopyEntity> getAgreementCopiesById() {
    return agreementCopiesById;
  }

  public void setAgreementCopiesById(Collection<AgreementCopyEntity> agreementCopiesById) {
    this.agreementCopiesById = agreementCopiesById;
  }

  @OneToMany(mappedBy = "agreementByChildAgreementId")
  public Collection<AgreementCopyEntity> getAgreementCopiesById_0() {
    return agreementCopiesById_0;
  }

  public void setAgreementCopiesById_0(Collection<AgreementCopyEntity> agreementCopiesById_0) {
    this.agreementCopiesById_0 = agreementCopiesById_0;
  }

//  @OneToMany(mappedBy = "agreementByAgreementId")
//  public Collection<AgreementDistributorRetailerVendorEntity> getAgreementDistributorRetailerVendorsById() {
//    return agreementDistributorRetailerVendorsById;
//  }
//
//  public void setAgreementDistributorRetailerVendorsById(Collection<AgreementDistributorRetailerVendorEntity> agreementDistributorRetailerVendorsById) {
//    this.agreementDistributorRetailerVendorsById = agreementDistributorRetailerVendorsById;
//  }
//
//  @OneToMany(mappedBy = "agreementByAgreementId")
//  public Collection<AgreementDistributorVendorGroupEntity> getAgreementDistributorVendorGroupsById() {
//    return agreementDistributorVendorGroupsById;
//  }
//
//  public void setAgreementDistributorVendorGroupsById(Collection<AgreementDistributorVendorGroupEntity> agreementDistributorVendorGroupsById) {
//    this.agreementDistributorVendorGroupsById = agreementDistributorVendorGroupsById;
//  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AgreementHistoryEntity> getAgreementHistoriesById() {
    return agreementHistoriesById;
  }

  public void setAgreementHistoriesById(Collection<AgreementHistoryEntity> agreementHistoriesById) {
    this.agreementHistoriesById = agreementHistoriesById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AgreementImportEntity> getAgreementImportsById() {
    return agreementImportsById;
  }

  public void setAgreementImportsById(Collection<AgreementImportEntity> agreementImportsById) {
    this.agreementImportsById = agreementImportsById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AgreementNoteEntity> getAgreementNotesById() {
    return agreementNotesById;
  }

  public void setAgreementNotesById(Collection<AgreementNoteEntity> agreementNotesById) {
    this.agreementNotesById = agreementNotesById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<AttachmentEntity> getAttachmentsById() {
    return attachmentsById;
  }

  public void setAttachmentsById(Collection<AttachmentEntity> attachmentsById) {
    this.attachmentsById = attachmentsById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<RuleEntity> getRulesById() {
    return rulesById;
  }

  public void setRulesById(Collection<RuleEntity> rulesById) {
    this.rulesById = rulesById;
  }

  @OneToMany(mappedBy = "agreementByAgreementId")
  public Collection<TierReportResultEntity> getTierReportResultsById() {
    return tierReportResultsById;
  }

  public void setTierReportResultsById(Collection<TierReportResultEntity> tierReportResultsById) {
    this.tierReportResultsById = tierReportResultsById;
  }
}


标签: javahibernatejpa

解决方案


您可以尝试 count(a) 而不是 a.id

EntityManager em = getFirstEntityManager();
        Session session = em.unwrap(Session.class);
        // The following line of code causes an error
        session.createQuery("select count(a) from AgreementEntity a ");

推荐阅读