Hello everyone and thank you for any help or advice.
I’ve been stuck for a few days trying to figure out how to make a new relation between an Entity with a Composite ID which references another Entity class as part of the Composite ID.
Originally I had the following code, which works fine:
@Entity
public class Order {
@Id
private Integer id;
@OneToMany(mappedBy = "id.order")
private List<OrderProduct> comandaProductes;
...
}
@Data
@Embeddable
public class OrderProductID implements Serializable {
private String productId;
@ManyToOne
private Order order;
...
}
@Entity
public class OrderProduct implements Serializable {
@EmbeddedId
private OrderProductID id;
...
}
But now I want to add another one-to-many relation from OrderProduct to a new class OrderProductExtras:
@Entity
public class OrderProduct implements Serializable {
@EmbeddedId
private OrderProductID id;
@OneToMany(mappedBy = "orderProduct", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderProductExtras> orderProductExtras;
...
}
@Embeddable
public class OrderProductExtrasPK implements Serializable {
private Integer extrasId;
@ManyToOne
private OrderProduct orderProduct;
...
}
@Entity
@Data
public class OrderProductExtras {
@EmbeddedId
private OrderProductExtrasPK id;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "product_fk", referencedColumnName = "productId"),
@JoinColumn(name = "order_fk", referencedColumnName = "orderId")
})
private OrderProduct orderProduct;
...
}
It does create the tables on the database but it fails to generate the FK constraint:
ERROR: foreign key constraint "fkg37ti7qy3rk7ywie9sxwh2fg1" cannot be implemented
Detail: Key columns "order_id" and "product_id" are of incompatible types: numeric and character varying.
I’ve tried many things but I can’t figure out how to properly reference the OrderId from OrderProductExtras.
There’s nothing similar on ORM User Guide and haven’t found anything online.
Any help would be appreciated.