我在休眠中有以下实体,使用 JPA 注释
@Entity
@IdClass(PurchaseCounter.PurchaseCounterPK.class)
@Table(name = "customer_purchases_counter")
public class PurchaseCounter {
public static class PurchaseCounterPK implements Serializable {
Integer customerId;
Integer purchaseId;
public PurchaseCounterPK(Integer customerId, Integer purchaseId) {
this.customerId = customerId;
this.purchaseId = purchaseId;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public Integer getPurchaseId() {
return purchaseId;
}
public void setPurchaseId(Integer purchaseId) {
this.purchaseId = purchaseId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PurchaseCounterPK that = (PurchaseCounterPK) o;
if (customerId != null ? !customerId.equals(that.customerId) : that.customerId != null) return false;
if (purchaseId != null ? !purchaseId.equals(that.purchaseId) : that.purchaseId != null) return false;
return true;
}
@Override
public int hashCode() {
int result = customerId != null ? customerId.hashCode() : 0;
result = 31 * result + (purchaseId != null ? purchaseId.hashCode() : 0);
return result;
}
}
Integer customerId;
Integer purchaseId;
Integer count = 0;
@Id
@Column(name = "customer_id")
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
@Id
@Column(name = "purchase_id")
public Integer getPurchaseId() {
return purchaseId;
}
public void setPurchaseId(Integer purchaseId) {
this.purchaseId = purchaseId;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
当我使用 Criteria 进行查询并使用 purchaseId 和 customerId 作为 Restriction.eq 过滤器时,这就是生成的查询:
select this_.customerId as customerId137_0_, this_.purchaseId as purchaseId137_0_, this_.count as count137_0_ from customer_purchases_counter this_ where this_.purchaseId=? and this_.customerId=?
这当然是错误的,因为字段 customerId 和 purchaseId 没有重命名为我使用 @Column 指定的名称????