繁体   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