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 を使用し、restriction.eq フィルターとして purchaseId と customerId を使用してクエリを実行すると、次のクエリが生成されます。

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-4256 ( Hibernate は IdClass で @Column(name=...) アノテーションを尊重しません) の発生です。その場合、新しいバージョンの Hibernate に更新すると解決策が提供されます。

@Columnまた、アノテーションを使用したバグレポートによると、IdClass回避策があります。

于 2013-01-29T18:55:26.137 に答える
-1

私が正しく理解しているかどうかはわかりませんが、@Column は単に Java データにマップする列を指定しただけです。

したがって、@Column アノテーションをゲッターに適用するのではなく、変数宣言自体に適用する必要があります。

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