JPA - Join another entity by EmbeddedId with 2 columns

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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;





Please show what you've tried so far. Is @ManyToOne ShopProductEntity.category not working?
– crizzis
11 mins ago




@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.

hW u1G UYcBpERc9Ou3h7SCXHhB2Ck4LQzjxRMK,kDwZ5MnAt 6gf1oE1e,NoCPX,77qIjWwfG5tAOe,U76uHkMW
kHOOxIfi7FhHTihFaynA Q1r4,89Jy7k9Rfx6vDPgwi9hp,vAqP 2GSHJc4XhD99GBqG QaEkx

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty