I need to add multiple entity objects into many-to-many entity.
I have the Component entity:
@Entity
@Table(name = "components", schema = "public", catalog = "inventory_db")
public class Component {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Integer id;
private Integer qty;
@OneToMany(mappedBy = "component")
@ToString.Exclude
List<PurchaseOrder> purchaseOrders;
}
Also have an User entity:
@Entity
@Table(name = "users")
@Getter
@Setter
@Accessors(chain = true)
@ToString
public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Integer id;
@OneToMany(mappedBy = "user")
List<PurchaseOrder> purchaseOrders;
}
And finally I have a PurchaseOrder entity:
@Entity
@Table(name = "purchase_order")
public class PurchaseOrder {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Integer id;
@Enumerated
@Column(name = "status")
private PurchaseOrderStatus purchaseOrderStatus;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "user_id", nullable = false,
foreignKey = @ForeignKey(name = "FK_PO_USER"))
private User user;
//TODO: I need to store here List<Component> - multiple components in one order of the user
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "component_id", nullable = false,
foreignKey = @ForeignKey(name = "FK_RENT_BOOK_INFO_BOOK"))
Component component;
I need an ability to store multiple components (List) in one PurchaseOrder for the User. How can I solve this via Spring JPA ?
You could drop the
@JoinColumn
annotation, and simply change the relationship to@ManyToMany
in both entities. Then in PurchaseOrder wrap component field with List. JPA should then automatically create a table namedpurchase_order_component
that will save ids of both entities.PurchaseOrder should be something like:
And Component:
You could get spring to generate the database schema for you adding this property:
Or here is the SQL equivalent: