Hibernate: How to reference part of CompositeUserType in CompositeKey?

19 Views Asked by At

I have a database view which shows the total order amount by customer and currency:

CREATE VIEW v_total_order_amount
AS SELECT
  o.customer_id,
  SUM(o.order_amount) AS order_amount,
  o.order_currency
FROM orders o
GROUP BY o.customer_id, o.order_currency

I want to represent this as a Hibernate entity and map the amount and currency as a javax.money.MonetaryAmount using a CompositeType. It should look like this:

@Entity
@Table("v_total_order_amount")
@IdClass(TotalOrderAmountCompositeKey.class)
public class TotalOrderAmount {

  @Id
  private Long customerId;

  @CompositeType(MonetaryAmountType.class)
  @AttributeOverride(name = "amount", column = @Column(name = "order_amount"))
  @AttributeOverride(name = "currency", column = @Column(name = "order_currency"))
  private MonetaryAmount orderAmount;

  @Id
  private String orderCurrency;

  // ... constructors, getters, setters
  
  public static class TotalOrderAmountCompositeKey implements Serializable {

    private Long customerId;

    private String orderCurrency;

    // ... constructors, getters, setters
  }
}

This fails with the following error:

Table [v_total_order_amount] contains physical column name [order_currency] referred to by multiple logical column names: [order_currency], [orderCurrency]

However, I cannot remove the field String orderCurrency because then the composite key throws an error. How can I use the currency both as part of the MonetaryAmount field and the composite key?

0

There are 0 best solutions below