Home SQL to JPA connection of two tables with composite key
Reply: 0

SQL to JPA connection of two tables with composite key

user871
1#
user871 Published in August 14, 2018, 8:27 am

How to implement the simple typical case of two tables: as example I can suggest of Product and Order with connection table OrderProduct in MySQL. Can someone suggest (write code for) class OrderProduct in JPA with all necessary elements of JPA (like Composite Key equivalent, etc) and fields from my SQL example of table OrderProduct. Thank you.

CREATE TABLE Product
(
  id   INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE Order
(
  id          INT AUTO_INCREMENT PRIMARY KEY,
  customer_id INT,
  datum       DATE,
  FOREIGN KEY (customer_id) REFERENCES Customer (customer_id)
);

CREATE TABLE OrderProduct
(
  order_id   INT,
  product_id INT,
  quantity   INT,
  PRIMARY KEY (order_id, product_id),
  FOREIGN KEY (order_id) REFERENCES Orders (id),
  FOREIGN KEY (product_id) REFERENCES Product (id)
);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.292204 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO