0

我有以下数据库架构,我需要使用单个视图http://i.stack.imgur.com/3HXhC.png将数据添加到所有三个表中(由于 stackoverflow 规则,我无法直接链接图像)。

我希望实现的是创建一个订单,为其提供一个 Workshop 订单 ID,并将其链接到 LineItems,这将让用户从 Inventory 表中指定要添加到订单中的项目数量。

我可以在我的数据库中创建一个车间订单,并使用车间订单 id 创建一个 lineitem,并将库存项目中的 id 和数量添加到 lineitem 表中,然后使用附加的代码显示每个 lineitem 订单行,总计商品数量,订单中的商品,总价,客户名称等。

如何创建一个视图,让我以这种方式创建订单?我想象的流程是:创建车间订单->从库存中添加行项目->保存订单。

在 Spring 和 Hibernate 上工作了几周,我还没有真正想出一个聪明的方法来解决这个问题,但希望这里有人知道。无论如何,请随意批评我的数据库方案、我的课程和其他任何事情。这可能是一个愚蠢的设计,不太适合实际的生产系统。

我已经附上了与此相关的主要课程。

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;
}
}
4

1 回答 1

0

This isn't really a question as it is more of a "how would I do this"

What have you tried already? Where are you running into trouble? etc.

Your view logic should not be coupled with your domain layer, what I mean is, you write your forms to be as usable as possible yet, still get the information you need. Once you post the information to the backing Controller, you do the required business logic in order to line up how the entities persist, etc.

Continuing this line of thinking, your controller should only be worried about web layer exceptions, and passing information on to the Business / Service Layer. From the Business / Service layer you execute required logic, and pass on to the Domain / Repository layer. This gives a clear separation of concerns allowing for easier testing.

于 2012-11-02T19:12:43.920 回答