Quiero obtener una suma de una columna en la tabla.

public class PaymentLogDAO extends AbstractModel<PaymentLog> {
    public PaymentLogDAO() {
        super(PaymentLog.class);
    }
        public BigDecimal sum(String keyword) {
        try {
            if (!sessionFactory.getCurrentSession().getTransaction().isActive())
                sessionFactory.getCurrentSession().getTransaction().begin();    
            Query query = sessionFactory.getCurrentSession()
                    .createQuery("select sum(p.Payment) "
                            + "from PaymentLog p "
                            + "where p.TransId =:TransId");
            query.setParameter("TransID", keyword);
            return (BigDecimal) query.uniqueResult();
        } catch (Exception e) {
            System.out.println(e);
            return BigDecimal.valueOf(0);    
        }

    }
}

Y obtuvo un error:

org.hibernate.QueryException: no se pudo resolver la propiedad: TransId de: entidades.PaymentLog [Seleccione p de las entidades.PaymentLog p donde p.TransId como: TransId]

Estas son mis entidades

@Entity
@Table(name="PaymentLog"
    ,schema="dbo"
    ,catalog="Project2"
)
public class PaymentLog  implements java.io.Serializable {


     private int id;
     private LoanLog loanLog;
     private Date transDate;
     private BigDecimal payment;


    public PaymentLog() {
    }


    public PaymentLog(int id) {
        this.id = id;
    }
    public PaymentLog(int id, LoanLog loanLog, Date transDate, BigDecimal payment) {
       this.id = id;
       this.loanLog = loanLog;
       this.transDate = transDate;
       this.payment = payment;
    }

    public PaymentLog(int id, LoanLog loanLog) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

     @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

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

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="TransId")
    public LoanLog getLoanLog() {
        return this.loanLog;
    }

    public void setLoanLog(LoanLog loanLog) {
        this.loanLog = loanLog;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="TransDate", length=23)
    public Date getTransDate() {
        return this.transDate;
    }

    public void setTransDate(Date transDate) {
        this.transDate = transDate;
    }


    @Column(name="Payment", scale=4)
    public BigDecimal getPayment() {
        return this.payment;
    }

    public void setPayment(BigDecimal payment) {
        this.payment = payment;
    }

}

Estas son mis entidades de LoanLog TransId en PaymentLog es la clave externa de Id en LoanLog

/**
 * LoanLog generated by hbm2java
 */
@Entity
@Table(name="LoanLog"
    ,schema="dbo"
    ,catalog="Project2"
)
public class LoanLog  implements java.io.Serializable {


     private int id;
     private Account account;
     private CustInfo custInfo;
     private LoanInfo loanInfo;
     private Date loanDate;
     private BigDecimal amount;
     private Integer duration;
     private Integer payingPeriod;
     private BigDecimal rate;
     private Set<PaymentLog> paymentLogs = new HashSet<PaymentLog>(0);
     private Set<FineLog> fineLogs = new HashSet<FineLog>(0);

    public LoanLog() {
    }


    public LoanLog(int id) {
        this.id = id;
    }
    public LoanLog(int id, Account account, CustInfo custInfo, LoanInfo loanInfo, Date loanDate, BigDecimal amount, Integer duration, Integer payingPeriod, BigDecimal rate, Set<PaymentLog> paymentLogs, Set<FineLog> fineLogs) {
       this.id = id;
       this.account = account;
       this.custInfo = custInfo;
       this.loanInfo = loanInfo;
       this.loanDate = loanDate;
       this.amount = amount;
       this.duration = duration;
       this.payingPeriod = payingPeriod;
       this.rate = rate;
       this.paymentLogs = paymentLogs;
       this.fineLogs = fineLogs;
    }

      @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "Id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

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

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="EmployId")
    public Account getAccount() {
        return this.account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="CustId")
    public CustInfo getCustInfo() {
        return this.custInfo;
    }

    public void setCustInfo(CustInfo custInfo) {
        this.custInfo = custInfo;
    }

@ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="LoanId")
    public LoanInfo getLoanInfo() {
        return this.loanInfo;
    }

    public void setLoanInfo(LoanInfo loanInfo) {
        this.loanInfo = loanInfo;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="LoanDate", length=23)
    public Date getLoanDate() {
        return this.loanDate;
    }

    public void setLoanDate(Date loanDate) {
        this.loanDate = loanDate;
    }


    @Column(name="Amount", scale=4)
    public BigDecimal getAmount() {
        return this.amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }


    @Column(name="Duration")
    public Integer getDuration() {
        return this.duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }


    @Column(name="PayingPeriod")
    public Integer getPayingPeriod() {
        return this.payingPeriod;
    }

    public void setPayingPeriod(Integer payingPeriod) {
        this.payingPeriod = payingPeriod;
    }


    @Column(name="Rate", precision=10, scale=4)
    public BigDecimal getRate() {
        return this.rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

@OneToMany(fetch=FetchType.LAZY, mappedBy="loanLog")
    public Set<PaymentLog> getPaymentLogs() {
        return this.paymentLogs;
    }

    public void setPaymentLogs(Set<PaymentLog> paymentLogs) {
        this.paymentLogs = paymentLogs;
    }

@OneToMany(fetch=FetchType.LAZY, mappedBy="loanLog")
    public Set<FineLog> getFineLogs() {
        return this.fineLogs;
    }

    public void setFineLogs(Set<FineLog> fineLogs) {
        this.fineLogs = fineLogs;
    }




}

¿Cómo puedo resolverlo? Muchísimas gracias

0
Tama Chan 24 jul. 2016 a las 14:45

2 respuestas

La mejor respuesta

No publicaste tu entidad LoanLog pero supongo que tienes una propiedad id de LoanLog o algo similar, así que prueba esto

select sum(p.Payment) "
+ "from PaymentLog p "
+ "where p.loanLoag.id =:TransId

Está haciendo referencia a una propiedad transId dentro de su entidad PaymentLog que no existe en java. Existe en la base de datos, pero como no está ejecutando una consulta nativa, la consulta debe ajustarse a las reglas de Java, lo que significa: busque la propiedad dentro de la entidad y la propiedad transId no existe.

0
Apostolos 24 jul. 2016 a las 13:15

Haciendo referencia a esta URL (consulte la cláusula Select), los campos deben ser parte de la Entidad que tiene. Así que supongo que su selección debe referirse al campo Id y no TransId (también creo que Id debe asignarse a TransId ya que los nombres son diferentes) http://www.tutorialspoint.com/hibernate/hibernate_query_language.htm

0
Ramachandran.A.G 24 jul. 2016 a las 13:09