JPA:如何处理与其他两个表有关系的表的Map?

y1aodyip  于 2022-12-04  发布在  其他
关注(0)|答案(3)|浏览(144)

我有三个表,表A(产品)、表B(发票)和表C(发票信息),其中包含引用发票标识和产品标识的两列。现在,如何插入新条目(新发票),同时将产品插入相应的表并将发票信息插入相应的表?
以下是实体类:产品名称

@Entity
@Table(name = "product")
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "family_id")
private long familyId;   
@Column(name = "product_name")
private String productName;
@Column(name = "product_category")
private String productCategory;
@Column(name = "product_quantity")
private int productQuantity;
//getters and setters
}

发票

@Entity
@Table(name = "invoice")
public class Invoice {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "invoice_id")
private Long id;
@Column(name = "provider_id")
private Long providerId;
@Column(name = "total")
private int invoiceTotal;
@Column(name = "date")
private Date invoiceDate;
//getters and setters
}

发票信息

@Entity
@Table(name = "invoice_info")
public class InvoiceInfo {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id")
private long id;   
@Column(name = "product_id")
private long productId;
@Column(name = "invoice_id")
private long invoiceId;

//getters and setters
}
nwwlzxa7

nwwlzxa71#

InvoiceInfo应为联接表,根据您要求使用@OneToMany,@ManyToOne注解定义实体Product和Invoice关系

h9a6wy2h

h9a6wy2h2#

要在将产品插入到product表时在invoice和invoice_info表中插入新条目,可以使用Hibernate API中的Session#persist方法。Session#persist方法允许您将实体保存到数据库中,并自动生成所需的SQL INSERT语句。
下面是一个示例,说明如何使用Session#persist方法将新发票和相关产品插入数据库:

// create a new Product entity
Product product = new Product();
product.setFamilyId(1);
product.setProductName("Product 1");
product.setProductCategory("Category 1");
product.setProductQuantity(10);

// create a new Invoice entity
Invoice invoice = new Invoice();
invoice.setProviderId(1);
invoice.setInvoiceTotal(100);
invoice.setInvoiceDate(new Date());

// create a new InvoiceInfo entity
InvoiceInfo invoiceInfo = new InvoiceInfo();
invoiceInfo.setProductId(product.getId());
invoiceInfo.setInvoiceId(invoice.getId());

// get the current Hibernate session
Session session = sessionFactory.getCurrentSession();

// save the Product, Invoice, and InvoiceInfo entities to the database
// using the persist method
session.persist(product);
session.persist(invoice);
session.persist(invoiceInfo);

在上面的代码中,我们创建了新的Product、Invoice和InvoiceInfo实体,然后使用Session#persist方法将它们保存到数据库中。这将自动生成所需的SQL INSERT语句,以便将新实体插入到相应的表中。
需要注意的是,Session#persist方法不会自动提交事务。这意味着您需要显式调用Session#commit方法来保存对数据库的更改。您可以通过在Session#persist调用后添加以下代码行来实现这一点:

session.commit();
kzipqqlq

kzipqqlq3#

您必须使用一组注解来建立实体之间的关系,例如:@ManyToOne@OneToMany@ManyToMany@OneToOne ...以及其他注解(如果需要)。
在您的情况下,我不确定是否需要InvoiceInfo表,因为Invoice表已经可以(或应该)包含产品列表。
我建议您建立以下关系:

产品名称

@Entity
@Table(name = "product")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "family_id")
    private long familyId;   
    @Column(name = "product_name")
    private String productName;
    @Column(name = "product_category")
    private String productCategory;
    @Column(name = "product_quantity") 
    private int productQuantity;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_id", referencedColumnName = "id")
    private Invoice invoice;
    //getters and setters
}

发票

@Entity
@Table(name = "invoice")
public class Invoice {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "invoice_id")
    private Long id;
    @Column(name = "provider_id")
    private Long providerId;
    @Column(name = "total")
    private int invoiceTotal;
    @Column(name = "date")
    private Date invoiceDate;
    @OneToMany(mappedBy = "product")
    private List<Product> products;
    //getters and setters
}

由于您的表InvoiceInfo已不存在,您只需将数据插入两个表中,如下所示:

Invoice invoice = invoiceRepository.save(invoice);
Product product = new Product();
// Set the other properties
product.setInvoice(invoice);
productRepository.save(product);

相关问题