次のデータベース スキーマがあり、単一のビューhttp://i.stack.imgur.com/3HXhC.pngを使用して 3 つのテーブルすべてにデータを追加する必要があります(stackoverflow の規則により、画像を直接リンクすることはできません)。
私が達成したいのは、オーダーを作成し、Workshop オーダー ID を指定して、LineItems にリンクし、ユーザーが Inventory テーブルからオーダーに追加するアイテムの数量を指定できるようにすることです。
データベースでワークショップ注文を作成し、ワークショップ注文 ID を使用してラインアイテムを作成し、在庫アイテムの ID と数量をラインアイテム テーブルに追加してから、添付のコードを使用して各ラインアイテムのオーダーラインを合計で表示できます。商品の数量、どの商品が注文されているか、合計金額、顧客名など。
この方法で注文を作成できるビューを作成するにはどうすればよいですか? 私が想像する流れは次のとおりです。ワークショップ注文を作成します->在庫から品目を追加します->注文を保存します。
Spring と Hibernate に取り組んだのはわずか 2 週間だけで、これを解決するスマートなアプローチを実際には理解できていませんが、ここにいる誰かが解決できることを願っています。ぜひ、私のデータベース スキーム、クラス、その他何でも自由に批判してください。これはばかげた設計であり、実際の運用システムにはあまり適していない可能性があります。
これに関連する主要なクラスを添付しました。
LineItems.java
@Entity
@Table(name = "LINE_ITEMS")
@AssociationOverrides({
@AssociationOverride(name = "pk.inventory",
joinColumns = @JoinColumn(name = "INVENTORY_Id")),
@AssociationOverride(name = "pk.workshop",
joinColumns = @JoinColumn(name = "WORKSHOP_ORDERS_Id"))
})
public class LineItems implements Serializable {
private static final long serialVersionUID = 5703588914404465647L;
@EmbeddedId
private LineItemsPK pk = new LineItemsPK();
private int quantity;
public LineItems() {
}
public LineItemsPK getPK() {
return pk;
}
public void setPK(LineItemsPK pk) {
this.pk = pk;
}
@Column(name = "WORKSHOP_ORDERS_Id", nullable=false, updatable=false,
insertable=false)
public Long getWorkshopOrdersId() {
return getPK().getWorkshop().getId();
}
@Column(name = "Id")
@JoinColumn(name="INVENTORY_Id", nullable=false, updatable=false, insertable=false)
public Long getInventoryId() {
return getPK().getInventory().getId();
}
@ManyToOne
public Workshop getWorkshop() {
return getPK().getWorkshop();
}
public void setWorkshop(Workshop workshop) {
getPK().setWorkshop(workshop);
}
@ManyToOne
@JoinColumn(name = "INVENTORY_Id")
public Inventory getInventory() {
return getPK().getInventory();
}
public void setInventory(Inventory inventory) {
getPK().setInventory(inventory);
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LineItems that = (LineItems) o;
if (getPK() != null ? !getPK().equals(that.getPK())
: that.getPK() != null) {
return false;
}
return true;
}
public int hashCode() {
return (getPK() != null ? getPK().hashCode() : 0);
}
}
LineItemsPK.java
@Embeddable
public class LineItemsPK implements Serializable {
private static final long serialVersionUID = -4285130025882317338L;
@ManyToOne
private Inventory inventory;
@ManyToOne
private Workshop workshop;
public Workshop getWorkshop() {
return workshop;
}
public void setWorkshop(Workshop workshop) {
this.workshop = workshop;
}
public Inventory getInventory() {
return inventory;
}
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
@Override
public boolean equals(Object o) {
if(this == o) {
return true;
}
if(o == null || getClass() != o.getClass()) {
return false;
}
LineItemsPK that = (LineItemsPK) o;
if(workshop != null ? !workshop.equals(that.workshop) : that.workshop != null) {
return false;
}
if(inventory != null ? !inventory.equals(that.inventory) : that.inventory != null) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result;
result = (workshop != null ? workshop.hashCode() : 0);
result = 31 * result + (inventory != null ? inventory.hashCode() : 0);
return result;
}
}
Workshop.java
@Entity
@Table(name = "WORKSHOP_ORDERS")
public class Workshop implements Serializable {
private static final long serialVersionUID = -8106245965993313684L;
public Long id;
public Long inventoryItemId;
public String workshopService;
public String workshopNotes;
public Long customersId;
public Long paymentId;
private Customer customer;
private Payment payment;
private Set<LineItems> lineItems = new HashSet<LineItems>(0);
public Workshop() {
}
public Workshop(Long inventoryItemId, String workshopService, String workshopNotes,
Customer customer, Payment payment) {
this.inventoryItemId = inventoryItemId;
this.workshopService = workshopService;
this.workshopNotes = workshopNotes;
this.customer = customer;
this.payment = payment;
}
public Workshop(Long inventoryItemId, String workshopService, String workshopNotes,
Customer customer, Payment payment, Set<LineItems> lineItems) {
this.inventoryItemId = inventoryItemId;
this.workshopService = workshopService;
this.workshopNotes = workshopNotes;
this.customer = customer;
this.payment = payment;
this.lineItems = lineItems;
}
@OneToMany(mappedBy = "pk.workshop", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Set<LineItems> getLineItems() {
return this.lineItems;
}
public void setLineItems(Set<LineItems> lineItems) {
this.lineItems = lineItems;
}
@ManyToOne
@JoinColumn(name="CUSTOMERS_Id", nullable = false, insertable = false, updatable = false)
public Customer getCustomer() {
return customer;
}
public void setCustomer(final Customer customer) {
this.customer = customer;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PAYMENT_Id", insertable = false, updatable = false, nullable = false)
public Payment getPayment() {
return payment;
}
public void setPayment(final Payment payment) {
this.payment = payment;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Id", nullable = false)
public Long getId() {
return id;
}
@Column(name = "InventoryItemId")
public Long getInventoryItemId() {
return inventoryItemId;
}
@Column(name = "WorkshopService")
public String getWorkshopService() {
return workshopService;
}
@Column(name = "WorkshopNotes")
public String getWorkshopNotes() {
return workshopNotes;
}
@Column(name = "CUSTOMERS_Id")
public Long getCustomersId() {
return customersId;
}
@Column(name = "PAYMENT_Id")
public Long getPaymentId() {
return paymentId;
}
public void setId(Long id) {
this.id = id;
}
public void setInventoryItemId(Long inventoryItemId) {
this.inventoryItemId = inventoryItemId;
}
public void setWorkshopService(String workshopService) {
this.workshopService = workshopService;
}
public void setWorkshopNotes(String workshopNotes) {
this.workshopNotes = workshopNotes;
}
public void setCustomersId(Long customersId) {
this.customersId = customersId;
}
public void setPaymentId(Long paymentId) {
this.paymentId = paymentId;
}
public String toString() {
return "Customer id: " + this.customersId + "Notes: " + workshopNotes;
}
}
在庫.java
@Entity
@Table(name = "INVENTORY")
public class Inventory implements Serializable {
private static final long serialVersionUID = -8907719450013387551L;
private Long id;
private String itemName;
private String itemVendorName;
private Long itemInventoryStatus;
private Double itemBuyPrice;
private Double itemSellPrice;
private Set<LineItems> lineItems = new HashSet<LineItems>(0);
public Inventory() {
}
public Inventory(String itemName, String itemVendorName, Long itemInventoryStatus,
Double itemBuyPrice, Double itemSellPrice) {
this.itemName = itemName;
this.itemVendorName = itemVendorName;
this.itemInventoryStatus = itemInventoryStatus;
this.itemBuyPrice = itemBuyPrice;
this.itemSellPrice = itemSellPrice;
}
public Inventory(String itemName, String itemVendorName, Long itemInventoryStatus,
Double itemBuyPrice, Double itemSellPrice, Set<LineItems> lineItems) {
this.itemName = itemName;
this.itemVendorName = itemVendorName;
this.itemInventoryStatus = itemInventoryStatus;
this.itemBuyPrice = itemBuyPrice;
this.itemSellPrice = itemSellPrice;
this.lineItems = lineItems;
}
@OneToMany(mappedBy = "pk.inventory", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Set<LineItems> getLineItems() {
return this.lineItems;
}
public void setLineItems(Set<LineItems> lineItems) {
this.lineItems = lineItems;
}
@Id
@Column(name = "Id", nullable = false)
@GeneratedValue(strategy = IDENTITY)
public Long getId() {
return this.id;
}
@Column(name = "ItemName")
public String getItemName() {
return this.itemName;
}
@Column(name = "ItemVendorName")
public String getItemVendorName() {
return this.itemVendorName;
}
@Column(name = "ItemInventoryStatus")
public Long getItemInventoryStatus() {
return this.itemInventoryStatus;
}
@Column(name = "ItemBuyPrice")
public Double getItemBuyPrice() {
return this.itemBuyPrice;
}
@Column(name = "ItemSellPrice")
public Double getItemSellPrice() {
return this.itemSellPrice;
}
public void setId(Long id) {
this.id = id;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public void setItemVendorName(String itemVendorName) {
this.itemVendorName = itemVendorName;
}
public void setItemInventoryStatus(Long itemInventoryStatus) {
this.itemInventoryStatus = itemInventoryStatus;
}
public void setItemBuyPrice(Double itemBuyPrice) {
this.itemBuyPrice = itemBuyPrice;
}
public void setItemSellPrice(Double itemSellPrice) {
this.itemSellPrice = itemSellPrice;
}
public String toString() {
return "Item id:" + this.id + " ItemName: " + this.itemName +
" ItemInventoryStatus: " + this.itemInventoryStatus +
" ItemBuyPrice: " + this.itemBuyPrice + " ItemSellPrice " + this.itemSellPrice;
}
}