JPA Object Update Error

I have the following domain model

Currency ----< Price >---- Product

Or in English

The product has one or more prices. Each price is expressed in a specific currency.

Pricehas a composite primary key (indicated below PricePK), which consists of foreign keys Currencyand Product. The relevant sections of JPA-annotated Java classes are below (getters and seters are mostly omitted):

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Currency {

    @Id 
    private Integer ix;

    @Column 
    private String name;

    @OneToMany(mappedBy = "pricePK.currency", cascade = CascadeType.ALL, orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.FALSE)
    private Collection<Price> prices = new ArrayList<Price>();
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @OneToMany(mappedBy = "pricePK.product", cascade = CascadeType.ALL, orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.FALSE)
    private Collection<Price> defaultPrices = new ArrayList<Price>();
}

@Embeddable
public class PricePK implements Serializable {

    private Product product;    
    private Currency currency;

    @ManyToOne(optional = false)
    public Product getProduct() {
        return product;
    }

    @ManyToOne(optional = false)
    public Currency getCurrency() {
        return currency;
    }    
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Price {

    private PricePK pricePK = new PricePK();

    private BigDecimal amount;

    @Column(nullable = false)
    public BigDecimal getAmount() {    
        return amount;
    }

    public void setAmount(BigDecimal amount) {    
        this.amount = amount;
    }

    @EmbeddedId
    public PricePK getPricePK() {
        return pricePK;
    }    

    @Transient
    public Product getProduct() {
        return pricePK.getProduct();
    }

    public void setProduct(Product product) {
        pricePK.setProduct(product);
    }

    @Transient
    public Currency getCurrency() {
        return pricePK.getCurrency();
    }

    public void setCurrency(Currency currency) {
        pricePK.setCurrency(currency);
    }
}

When I try to refresh the instance Product, I get a StackOverflowError, so I suspect there is some kind of loop (or other error) in the above display, can anyone define it?

+5
3

, . , PricePK ( @ManyToOne) @AssociationOverrides on Price.

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@AssociationOverrides({
    @AssociationOverride(name = "pricePK.product", 
                         joinColumns = @JoinColumn(name = "product_id")),
    @AssociationOverride(name = "pricePK.currency", 
                         joinColumns = @JoinColumn(name = "currency_id"))
})
public class Price extends VersionedEntity {
    [...]
}

, , , id Product Currency.

+2

, CascadeType.ALL, OneToMany. , , .

, , , :

@OneToMany(mappedBy = "pricePK.product", cascade = {
        CascadeType.PERSIST, CascadeType.MERGE
    }, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Collection<Price> defaultPrices = new ArrayList<Price>();
+1

Price ManyToOne Price @IdClass (PricePK)?

I don't know how Hibernate handles this, though, but I have successfully implemented this using OpenJPA. In this case, PricePK must declare its fields with the same names as in Price, but instead use a simple type (Integer instead of Currency or Product). In Price, you may need to annotate the product and currency using @Id. Note that this is outside the JPA specification (@IdClass only supports simple fields).

0
source

All Articles