0

私は次のクラスを持っています:

@Entity
@Table(name="ripa_request")
public class ReturningInvoiceRequest implements EntityKey<Long> {

public enum ReturningInvoiceRequestStatus {
    /**
     * This status means that the products for this request are chosen, but not confirmed at all. 
     */
    WAITING_TO_SEND("Aguardando envio para o fornecedor"),

    /**
     * This status means that the returning request is done but the supplier needs to approve the chosen products. 
     */
    WAITING_FOR_ACCEPTANCE("Aguardando aprovação do fornecedor"),

    /**
     * This status means that the invoice was generated with the returning request products and the products are dispatched.
     */
    INVOICE_DISPATCHED("Nota Fiscal de devolução emitida"), 

    /**
     * This status means that something wrong happened on the returning request process. 
     */
    INVALID("Requisição inválida");

    private String message;

    private ReturningInvoiceRequestStatus(String message) {
        this.message = message;
    }

    /* (non-Javadoc)
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        return message;
    }
}

@Id
@GeneratedValue
private Long id;

/**
 * Mapping for the Products associated to this request with related information: Product Invoice's included with chosen count.
 */
@ElementCollection(targetClass=ProductRequestContext.class)
@MapKeyJoinColumn(name="product_id")
@CollectionTable(name="ripa_request_product", joinColumns=@JoinColumn(name="request_id"))
@Columns(columns={@Column(name="invoice_id"), @Column(name="count")})
private Map<Product, Collection<ProductRequestContext>> productCountMap;

@Temporal(TemporalType.TIMESTAMP)
private Date date;

@Enumerated(EnumType.STRING)
private ReturningInvoiceRequestStatus status;


public ReturningInvoiceRequest() {
    this.status = ReturningInvoiceRequestStatus.WAITING_TO_SEND;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}    

public Collection<Product> getProductList() {
    return this.productCountMap == null ? new ArrayList<Product>() : this.productCountMap.keySet();
}

public void setProductList(Collection<Product> productList) {
    this.productCountMap = new HashMap<>();
    for (Product product : productList) {
        if (product.getInvoiceList() == null || product.getInvoiceList().isEmpty()) {
            setStatus(ReturningInvoiceRequestStatus.INVALID);
            break;
        } else {
            List<ProductRequestContext> productContextList = new ArrayList<>();
            for (Invoice invoice : product.getInvoiceList()) {
                productContextList.add(new ProductRequestContext(invoice.getId(), product.getCount(invoice)));
            }
            this.productCountMap.put(product, productContextList);  
        }
    }
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public ReturningInvoiceRequestStatus getStatus() {
    return status;
}

public void setStatus(ReturningInvoiceRequestStatus status) {
    this.status = status;
}

/**
 * Return the Invoice associated to the informed Product of this request
 * @param product Product business entity
 * @return <code>Invoice</code> Invoice business entity
 */
public Invoice getInvoice(Product product) {
    for (Product requestProduct : getProductList()) {
        if (product.equals(requestProduct)) {
            Collection<ProductRequestContext> contextList = (Collection<ProductRequestContext>) this.productCountMap.get(requestProduct);
            for (ProductRequestContext context : contextList) {
                Long invoiceID = context.getRequestInvoiceID();
                for (Invoice reqInvoice : requestProduct.getInvoiceList()) {
                    if (invoiceID.equals(reqInvoice.getId())) {
                        return reqInvoice;
                    }
                }
            }
        }
    }
    return null;
}

/**
 * Return the quantity of the informed Product selected for this request.
 * @param product Product business entity
 * @return
 */
public Integer getCount(Product product) {
    for (Product requestProduct : getProductList()) {
        if (product.equals(requestProduct)) {
            Collection<ProductRequestContext> contextList = (Collection<ProductRequestContext>) this.productCountMap.get(requestProduct);
            for (ProductRequestContext context : contextList) {
                Long invoiceID = context.getRequestInvoiceID();
                for (Invoice reqInvoice : requestProduct.getInvoiceList()) {
                    if (invoiceID.equals(reqInvoice.getId())) {
                        return context.getRequestCount();
                    }
                }
            }
        }
    }
    return null;
}    


/* (non-Javadoc)
 * @see br.com.mls.eanf.entity.ifc.EntityKey#getKey()
 */
public Long getKey() {
    return id;
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ReturningInvoiceRequest other = (ReturningInvoiceRequest) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    return true;
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return this.id == null ? super.toString() : this.id.toString();
}



@Embeddable
public static class ProductRequestContext {
    /**
     * Sequence identifier for the Invoice registry on this context.
     */
    @Column(name="invoice_id")
    private long requestInvoiceID;

    @Column(name="count")
    private int requestCount;

    public ProductRequestContext() {
    }

    ProductRequestContext(Long invoiceID, Integer count) {
        this.requestInvoiceID = invoiceID;
        this.requestCount = count;
    }

    public Long getRequestInvoiceID() {
        return requestInvoiceID;
    }

    public void setRequestInvoiceID(Long invoiceID) {
        this.requestInvoiceID = invoiceID;
    }

    public int getRequestCount() {
        return requestCount;
    }

    public void setRequestCount(int count) {
        this.requestCount = count;
    }
}

}

この問題は、ReturningInvoiceRequestエンティティの挿入中に(EntityManagerのpersitメソッドによって)、要素のコレクションとのマッピング関係で発生します。

javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of br.com.mls.ripa.domain.model.product.ReturningInvoiceRequest$ProductRequestContext.requestCount

私の調査では、マップの値に関するコレクションのHibernateのサポートがないこと、およびhttp://blog.xebia.com/2007/10/05/mapping-multimaps-with-hibernate/でMultiMapアプローチに関する情報が見つかりました。、しかしそれが私の問題を解決するかどうかはわかりません

また、GoogleコレクションのMultimapクラスを使用してCollection値を抑制しようとしましたが、休止状態で理解できるjava.util.Mapを拡張せず、apache-collections MultiMapにないため、機能しません。 Hibernateマッピングに必要なGenericsのサポート

Hibernate 3.6.9を使用していますが、バージョン4.1.8でもエラーが発生します。

ありがとう

4

1 に答える 1

0

私が見つけた解決策は、次のリストを使用することでしたProductRequestContext

@ElementCollection
@CollectionTable(name="ripa_request_product", joinColumns=@JoinColumn(name="request_id"))
@Columns(columns={@Column(name="product_id"), @Column(name="invoice_id"), @Column(name="count")})
private List<ProductRequestContext> productCountList
于 2012-11-28T21:59:18.633 に答える