JPA - Join another entity by EmbeddedId with 2 columns

Multi tool use


JPA - Join another entity by EmbeddedId with 2 columns
Basically. I have a ProductEntity, like this:
@Entity
@Table(name = "product", schema = "shop")
public class ShopProductEntity {
@EmbeddedId
private ProductEntityId id;
@Column(name = "product_name")
private String name;
@Column(name = "product_category_id")
private int categoryId;
@Column(name = "product_language_id")
private int languageId;
// TODO: Get category by categoryId and languageId.
private CategoryEntity category;
I have another CategoryIdentity:
@Entity
@Table(name = "category", schema = "shop")
public class CategoryEntity {
@EmbeddedId
private CategoryEntityId id;
@Column(name = "category_name")
private String name;
@Column(name = "category_url")
private String url;
It has an EmbeddedId like this:
@Embeddable
public class CategoryEntityId implements Serializable {
@Column(name = "category_id", nullable = false)
private int categoryId;
@Column(name = "language_id", nullable = false)
private int languageId;
public int getCategoryId() {
return categoryId;
}
public int getLanguageId() {
return languageId;
}
Now, every product has a category. Categories are unique by their id and language. The shop connects to a category by both the categoryId and languageId columns. How do I add the CategoryEntity to my ProductEntity so I can use the category's url value for my product?
I tried adding this to ShopProductEntity:
@ManyToOne
@JoinColumns({
@JoinColumn(name="categoryId", referencedColumnName="categoryId"),
@JoinColumn(name="languageId", referencedColumnName="languageId"),
})
private CategoryEntity category;
@ManyToOne ShopProductEntity.category
I updated the original. As of now I want the category to be connected to every product. So I guess it should be ManyToOne since once category can have multiple products? And it needs to look so ShopProductEntity.id.languageId = CategoryEntity.id.languageId && ShopProductEntity.categoryId = CategoryEntity.id.categoryId.
– PadaKatel
1 min ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please show what you've tried so far. Is
@ManyToOne ShopProductEntity.category
not working?– crizzis
11 mins ago