jpa实体映射对表

时间:2018-11-24 18:26:05

标签: java hibernate jpa

我有用于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_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"}

2 个答案:

答案 0 :(得分: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个表中,并用于连接它们。

答案 1 :(得分:0)

很抱歉,您的问题措辞不好,只是不知道该如何改写我想要的东西。 我所需要做的只是为产品表中没有的字段添加@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。