2

我在休眠中有以下实体,使用 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 指定的名称????

4

2 回答 2

1

映射似乎是正确的。这很可能会发生HHH-4256Hibernate 不尊重带有 IdClass 的 @Column(name=...) 注释)。如果是这样,那么更新到更新版本的 Hibernate 提供了解决方案。

@Column同样根据使用注释的错误报告IdClass是解决方法。

于 2013-01-29T18:55:26.137 回答
-1

我不确定我是否理解正确,但 @Column 仅指定了将哪一列映射到您的 java 数据。

因此,您不应将 @Column 注释应用于您的 getter,而应将其应用于您的变量声明本身。

@ID
@Column (name="customer_Id")
Integer customerId;
@ID
@Column (name="purchase_Id")
Integer purchaseId;
于 2013-01-29T18:01:09.323 回答