JPA-通过EmbeddedId与2列联接另一个实体

时间:2018-07-27 11:32:38

标签: spring jpa persistence one-to-one

基本上。我有一个ProductEntity,像这样:

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

我还有另一个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;

它具有如下所示的EmbeddedId:

@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;
    }

现在,每个产品都有一个类别。类别通过其ID和语言是唯一的。商店通过categoryId和languageId列连接到类别。如何将CategoryEntity添加到我的ProductEntity中,以便可以为我的产品使用类别的url值?

我尝试将其添加到ShopProductEntity:

@ManyToOne
@JoinColumns({
        @JoinColumn(name="categoryId", referencedColumnName="categoryId"),
        @JoinColumn(name="languageId", referencedColumnName="languageId"),
})
private CategoryEntity category;

0 个答案:

没有答案