How to match the last child of a collection with JPA and Hibernate

I have an entity class Productand I want it to join the table Price.

My goal is to keep the old prices for reporting, and when I receive the object Product, it must be compared with the last price in accordance with the last date.

Please explain to me how I can achieve this with respect to Hibernate JPA. If possible, share a piece of code.

+4
source share
1 answer

This is an amazing question, so I decided to include this answer in the article .

Your domain module will look like this:

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "product", orphanRemoval = true)
    private List<Price> prices = new ArrayList<>();

    @ManyToOne
    @JoinFormula(
        "(SELECT id FROM price ORDER BY created_on DESC LIMIT 1)"
    )
    private Price latestPrice;

    public void setName(String name) {
        this.name = name;
    }

    public List<Price> getPrices() {
        return prices;
    }

    public void addPrice(BigDecimal priceValue) {
        Price price = new Price();
        price.setPrice(priceValue);
        prices.add(price);
        price.setProduct(this);
        latestPrice = price;
    }

    public Price getLatestPrice() {
        return latestPrice;
    }
}

@Entity(name = "Price")
public class Price {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Product product;

    @Column(name = "created_on", nullable=false, updatable=false)
    private Date createdOn;

    private BigDecimal price;

    public void setProduct(Product product) {
        this.product = product;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @PrePersist
    public void prePersist() {
        createdOn = new Date();        
    }
}

:

Long id = ...;
BigDecimal newPriceValue = ...;

Product product = entityManager.find(Product, id);
Price oldPrice = product.getLatestPrice();    

product.addPrice(newPriceValue);
+5

All Articles