gpt4 book ai didi

java - jpa实体映射情侣表

转载 作者:行者123 更新时间:2023-12-02 10:33:36 25 4
gpt4 key购买 nike

我有用于 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 个表:产品(产品 ID、产品名称、产品价格、产品供应商 ID)、产品类别 ID);供应商(供应商 ID,供应商名称);类别(类别 ID,类别名称);在第一个表中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;

异常(exception):

     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 个表中并用于连接它们。

关于java - jpa实体映射情侣表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53461159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com