簡體   English   中英

jpa實體映射對表

[英]jpa entity mapping couple tables

我有用於JSON解析的實體

    @Entity
    public class Product{
    private int productId;
    private String productName;
    private BigDecimal productPrice;
    private int productVendorId;
    private String productVendorName;
    private int productCategoryId;
    private String productCategoryName;
    //getters setters here

在數據庫中創建了3個表:產品(product_id,product_name,product_price,product_vendor_id),product_category_id); 供應商(vendor_id,vendor_name); 類別(category_id,category_name); 在第一張表product_vendor_id fk->賣方中的vendor_id pk和product_category_id fk-> category中的category_id pk中,我嘗試了以下操作:

    @Entity
    @Table(name = "products, schema = "market")
    public class Product
    @Id
    @Column(updatable = false, nullable = false, name = "product_id")
    private int Id;
    @Column(name = "product_name")
    private String productName;
    @Column(name = "product_price")
    private BigDecimal productPrice;
    @Column(name = "product_vendor_id")
    private int productVendorId;
    @Columnt(table = "vendors", name = "vendor_name")
    private String vendor_name;
    @Column(name = "product_category_id")
    private int productCategoryId;
    @Column(table = "categories", name = "category_name")
    private String productCategorName;
    //getters setters here

收到很多錯誤:例如我在產品表等中沒有category_name列。使用時收到此錯誤

     @Table(name = "products", schema = "market" )
     @SecondaryTables({@SecondaryTable(name = "vendors", schema = "market"),
     @SecondaryTable(name = "categories", schema = "market")})
     @JsonIgnoreProperties(ignoreUnknown = true)
     public class Product {
     ....
     @JoinColumn(name = "product_vendor_id", referencedColumnName = "vendor_id")
     private int productVendorID;
     @JoinColumn(table = "vendors", name = "vendor_name")
     private String productVendorName;
     @JoinColumn(name = "product_category_id", referencedColumnName = 
     "product_category_id")
     private int productCategoryID;
     @JoinColumn(table = "categories",  name = "category_name")
     private String productCategoryName;    

例外:

     Caused by: org.postgresql.util.PSQLException: ERROR: column 
     product0_1_.product_id doesn't exist
     Hint: There may have been a link to the "product0_.product_id" column
     Position: 705     

如何在3個表上映射此實體? upd:我不想分離這個實體,我也需要這個來反序列化我的json對象,只想在不同的操作上重用這個實體。 json的例子

   {"productID":"1111111","productName":"Cool product","productPrice":"99.99","productVendorName":"Some store","productVendorID":"1337","productCategoryName":"Food","productCategoryID":"1"}

由於存在3個單獨的表,因此您需要創建三個單獨的實體類。 我還假設供應商和類別表與產品具有一對多的關系。 試試下面的代碼:

產品名稱

@Entity
public class Product {
  @Id
  private int productId;
  private String productName;
  private BigDecimal productPrice;
  private String productVendorName;
  private String productCategoryName;
  @ManyToOne
  @JoinColumn(name = "productCategoryId")
  private Category category;
  @ManyToOne
  @JoinColumn(name = "productVendorId")
  private Vendors vendor;
}

分類

@Entity
public class Category {
  @Id
  private Integer categoryId;
  private String categoryName;
  @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
  @NotEmpty
  private List<Product> products = new ArrayList<>();
}

供應商:

@Entity
public class Vendors {
  @Id
  private int vendorId;
  private String vendorName;
  @OneToMany(mappedBy = "vendor", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
  @NotEmpty
  private List<Product> products = new ArrayList<>();
}

不過,我建議您使用上述方法,如果您仍然希望擁有單個實體類和3個帶有冗余數據的單獨表,請使用以下方法:

@Entity
@SecondaryTables({ @SecondaryTable(name = "vendors"), @SecondaryTable(name = "categories") })
public class Product {
  @Id
  private int productId;
  private String productName;
  private BigDecimal productPrice;
  private String productVendorName;
  private String productCategoryName;
  @Column(table = "categories")
  private Integer categoryId;
  @Column(table = "categories")
  private String categoryName;
  @Column(table = "vendors")
  private int vendorId;
  @Column(table = "vendors")
  private String vendorName;
}

主表的id列將出現在所有3個表中,並用於連接它們。

很抱歉這個問題的措辭不好,只是不知道該如何改寫我想要的東西。 我所需要的全部只是為產品表中沒有的字段添加@transient注釋,並像建議的答案一樣將其分開。

@Entity
@Table(name = "products", schema = "store" )
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {
@Id
@Column(updatable = false, nullable = false, name = "product_id")
private int productId;
@Column(name = "product_name")
private String productName;
@Column(name = "product_price")
private BigDecimal productPrice;
@Transient
private String productVendorName;
@Transient
private String productCategoryName;
@Transient
private int vendorId;
@Transient
private int categoryId;
@ManyToOne
@JoinColumn(name = "product_category_id")
private Category category;
@ManyToOne
@JoinColumn(name = "product_vendor_id")
private Vendor vendor;
}

供應商表實體

@Entity
@Table(name = "vendors", schema = "store")
public class Vendor {
@Id
@Column(name = "vendor_id")
private int vendorId;
@Column(name = "vendor_name")
private String vendorName;
@OneToMany(mappedBy = "vendor", cascade = CascadeType.ALL, fetch = FetchType.LAZY, 
orphanRemoval = true)
@NotNull
private List<Product> products = new ArrayList<>();
}

和類別

@Entity
@Table(name = "categories", schema = "store")
public class Category {
@Id
@Column(name = "category_id")
private Integer categoryId;
@Column(name = "category_name")
private String categoryName;
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.LAZY, 
orphanRemoval = true)
@NotNull
private List<Product> products = new ArrayList<>();
}

想要在這里留下我的問題的完整答案,也許以后有人需要它。只需檢查toString的一些問題即可。 僅在Product.class中使用它,最好制作兩個版本的print json和jpa。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM