我想在表格中获得一列的总和。
tes.mat
它有错误:
org.hibernate.QueryException:无法解析属性:TransId:entities.PaymentLog [从entities.PaymentLog p中选择p,其中p.TransId如:TransId]
这是我的实体
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);
}
}
}
这是我的@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;
}
}
中的LoanLog实体TransId
是PaymentLog
中Id
的外键
LoanLog
我该如何解决? 非常感谢你
答案 0 :(得分:0)
参考此URL(请参阅Select子句),字段必须是您拥有的实体的一部分。所以我猜你的选择必须引用Id字段而不是TransId(我也认为Id必须映射到TransId,因为名称不同) http://www.tutorialspoint.com/hibernate/hibernate_query_language.htm
答案 1 :(得分:0)
你没有发布你的LoanLog
实体,但我猜你的id
属性为LoanLog
或类似,所以试试这个
select sum(p.Payment) "
+ "from PaymentLog p "
+ "where p.loanLoag.id =:TransId
您在transId
实体中引用了PaymentLog
属性,该属性存在于java中。它存在于数据库中,但由于您没有执行本机查询,因此查询必须符合java规则,这意味着:在实体中查找属性并且transId
属性不存在。