使用Spring MVC中的Spring表单向多个表添加数据

时间:2012-11-02 13:05:42

标签: mysql spring hibernate spring-mvc jspx

我有以下数据库模式,我需要使用单个视图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;
}
}

Inventory.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;
}
}

1 个答案:

答案 0 :(得分:0)

这不是一个真正的问题,因为它更像是“我该怎么做”

你有什么尝试? 你在哪里遇到麻烦? 等

您的视图逻辑不应与您的域图层耦合,我的意思是,您编写的表单尽可能可用,仍然可以获得所需的信息。将信息发布到支持控制器后,您将执行所需的业务逻辑,以便排列实体的持久性等。

继续这种思路,您的控制器应该只担心Web层异常,并将信息传递到业务/服务层。从业务/服务层执行所需的逻辑,然后传递到域/存储库层。这样可以明确区分关注点,从而更容易进行测试。