问题描述:
联合主键统计条数hibernate解析的SQL无法成功执行。
HQL语句:
select count(_it) from com.levelappro.gbism.app.xpay.model.Xpay_Drls _it解析后的SQL语句:
select count((xpay_drls0_.ywlsh, xpay_drls0_.ywrq)) as col_0_0_ from xpay_drls xpay_drls0_hibernate实体:
package com.levelappro.gbism.app.xpay.model;import java.io.Serializable;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
/**
 * 支付交易当日流水表
 * 
 * @author fanmin
 */@Entity(name="xpay_drls")
@IdClass(XpayDrlsId.class)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)  
public class Xpay_Drls implements Serializable { private static final long serialVersionUID = 1L; @Id
private XpayDrlsId xpayDrlsId;

/*账务日期*/
@Column(nullable=false,length=8)
private String zwrq;

/*业务日期*/
@Column(nullable=false,length=8)
private String ywrq;

/*业务流水号*/
@Column(nullable=false)
private Integer ywlsh;

/*平台流水号*/
@Column(nullable=false)
private Integer zhqzlsh;

/*银行代码*/
@Column(nullable=true,length=3)
private String yhdm; public XpayDrlsId getXpayDrlsId() {
return xpayDrlsId;
} public void setXpayDrlsId(XpayDrlsId xpayDrlsId) {
this.xpayDrlsId = xpayDrlsId;
} public String getZwrq() {
return zwrq;
} public void setZwrq(String zwrq) {
this.zwrq = zwrq;
} public String getYwrq() {
return ywrq;
} public void setYwrq(String ywrq) {
this.ywrq = ywrq;
} public Integer getYwlsh() {
return ywlsh;
} public void setYwlsh(Integer ywlsh) {
this.ywlsh = ywlsh;
} public Integer getZhqzlsh() {
return zhqzlsh;
} public void setZhqzlsh(Integer zhqzlsh) {
this.zhqzlsh = zhqzlsh;
} public String getYhdm() {
return yhdm;
} public void setYhdm(String yhdm) {
this.yhdm = yhdm;
}
}联合主键:
package com.levelappro.gbism.app.xpay.model;/**
 * 支付交易当日流水表联合主键
 * 
 * @author fanmin
 */public class XpayDrlsId implements java.io.Serializable { private static final long serialVersionUID = 1L;
private String ywrq;
private Integer ywlsh;
public XpayDrlsId() {
} public XpayDrlsId(String ywrq, Integer ywlsh) {
this.ywrq = ywrq;
this.ywlsh = ywlsh;
} public String getYwrq() {
return this.ywrq;
} public void setYwrq(String ywrq) {
this.ywrq = ywrq;
} public Integer getYwlsh() {
return this.ywlsh;
} public void setYwlsh(Integer ywlsh) {
this.ywlsh = ywlsh;
} public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof XpayDrlsId))
return false;
XpayDrlsId castOther = (XpayDrlsId) other; return ((this.getYwrq() == castOther.getYwrq()) || (this.getYwrq() != null
&& castOther.getYwrq() != null && this.getYwrq().equals(
castOther.getYwrq())))
&& ((this.getYwlsh() == castOther.getYwlsh()) || (this
.getYwlsh() != null
&& castOther.getYwlsh() != null && this.getYwlsh()
.equals(castOther.getYwlsh())));
} public int hashCode() {
int result = 17; result = 37 * result
+ (getYwrq() == null ? 0 : this.getYwrq().hashCode());
result = 37 * result
+ (getYwlsh() == null ? 0 : this.getYwlsh().hashCode());
return result;
}}查询方法:public int count(Session session, Class<?> searchClass, ISearch search) {
if (searchClass == null || search == null)
return 0; List<Object> paramList = new ArrayList<Object>();
String hql = generateRowCountQL(searchClass, search, paramList);
if (hql == null) { 
return 1;
}
Query query = session.createQuery(hql);
addParams(query, paramList); return ((Number) query.uniqueResult()).intValue();
}用debug模式查看执行的HQL和打印的SQL分别就是前面列的,解析出来的SQL把两个主键都放到count中数据库不能执行。HibernateHQL联合主键统计