Home > database >  JPA : How to handle mapping with a table that has relationship with two other tables?
JPA : How to handle mapping with a table that has relationship with two other tables?

Time:12-04

I have three tables, table A (product), table B (invoice) and table C (invoices_info) which contains two columns referencing invoice_id and product_id. How can i insert a new entry (a new invoice) while inserting the products to the appropriate table and inserting the invoice info to its table also ?

Here are the entity classes : Product

@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
}

Invoice

@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
}

InvoiceInfo

@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
}

CodePudding user response:

InvoiceInfo should be join table, Define relationship on entities Product & Invoice using annotations @OneToMany, @ManyToOne based on your requirement.

CodePudding user response:

To insert a new entry in the invoice and invoice_info tables while inserting the products to the product table, you can use the Session#persist method from the Hibernate API. The Session#persist method allows you to save an entity to the database and automatically generates the required SQL INSERT statement for you.

Here's an example of how you can use the Session#persist method to insert a new invoice and the associated products into the database:

// 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);

In the code above, we create new Product, Invoice, and InvoiceInfo entities and then save them to the database using the Session#persist method. This will automatically generate the required SQL INSERT statements to insert the new entities into the appropriate tables.

It's important to note that the Session#persist method does not commit the transaction automatically. This means that you will need to explicitly call the Session#commit method in order to save the changes to the database. You can do this by adding the following line of code after the Session#persist calls:

session.commit();

CodePudding user response:

You have to create relationships between your entities by using a set of annotations like: @ManyToOne, @OneToMany, @ManyToMany or @OneToOne... and other annotations if needed.

In your case I am not really sure you need an InvoiceInfo table, as the Invoice table can (or should) already contains the list of products.

I would suggest you the following relationships:

Product

@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
}

Invoice

@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
}

As your table InvoiceInfo no longer exists, you just have to insert you data in two table like this:

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