如何在hibernate / jpa中保存一对多?

时间:2015-01-30 02:27:10

标签: java hibernate jsp

我不确定我是否正确描述了与JPA的一对多关系。 我有3张桌子: 杂工表包含姓名,地址,电话号码等。 公司表有:公司名称,地址,电话号码等。 WorkOrder表:workOrder编号,工作地点地址,FK到杂工的ID,FK到公司的ID。 添加新的工作订单时,我使用select从数据库中选择公司和杂工名称,然后填写工作订单表单。 问题是如何保存它?

Handyman.java

  @OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="Handymans_list_id")
private Set<WorkOrder> workOrders;
public Set<WorkOrder> getWorkOrders() {
    return workOrders;
}
public void setWorkOrders(Set<WorkOrder> workOrders) {
    this.workOrders = workOrders;
}

Company.java

    @OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="Companys_list_id")
private Set<WorkOrder> workOrders;
public Set<WorkOrder> getWorkOrders() {
    return workOrders;
}
public void setWorkOrders(Set<WorkOrder> workOrders) {
    this.workOrders = workOrders;
}

WorkOrder.java

    @ManyToOne(optional=false)
@JoinColumn(name="Companys_list_id", referencedColumnName="id")
//@ManyToOne  
//@JoinColumn(name = "Companys_list_id")
private Company company;

@ManyToOne(optional=false)
@JoinColumn(name="Handymans_list_id", referencedColumnName="id")

//@ManyToOne  
//@JoinColumn(name = "Handymans_list_id") 
private Handyman handyman;

WorkOrderDAOHibernate.java

    public WorkOrder saveWorkOrder(WorkOrder workOrder)
{
    System.out.println("Open session for saving work order ...");
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session =  sf.openSession();
    Transaction trans = session.beginTransaction();
    //Integer id = (Integer) session.save(workOrder);
    //Company company = workOrder.getCompany();
    //Handyman handyman = new Handyman();
    //String name  = handyman.getHandymanName();
    //System.out.println("Printing company =" + company);
    //System.out.println("Printing handyman = "+ handyman);
    //System.out.println("Handymans name = " + name);

    //

    //workOrder.setCompany(company);

    //workOrder.setHandyman(handyman);
    System.out.println("Workorder "+workOrder);
    Integer id = (Integer) session.save(workOrder);

    trans.commit();
    workOrder.setId(id);
    session.close();
    return workOrder;

}

workorder3.jsp

    <s:form action="resultAction1" namespace="/">

        <s:actionerror/>
        <s:textfield name="workOrder.workOrder"      label="Work order number" />
        <s:textfield name="workOrder.address"        label="Adress/Location" />
        <s:textfield name="workOrder.email"          label="Email" />
        <s:textfield name="workOrder.comment"        label="Comment" />
        <s:textfield name="workOrder.dateReceived"   label="Date receved" />
        <s:textfield name="workOrder.price"          label="Price" />

<h4>
<s:select label="Pick the company name " 
    headerKey="-1" headerValue="Select Company name"
    list="companyList" 
    name="yourCompanyList" 
     />
</h4> 

<h4>
<s:select label="Pick handyman name " 
    headerKey="-1" headerValue="Select Handyman name"
    list="handymanList" 
    name="yourHandymanList" 
     />
</h4> 


        <s:submit value="submit" name="submit" />

1 个答案:

答案 0 :(得分:3)

您正试图通过在HandyMan and Company中放置对WorkOrder的引用来定义双向关系。在双向实现中,只有一方是该关系的所有者。所有者方将具有@JoinColumn注释,而另一方将在@OneToMany中具有mappedBy属性,该属性引用映射@JoinColumn的bean属性。

所以你的类映射如下:

Handyman.java

@OneToMany(cascade=CascadeType.ALL,mappedBy="handyman")
private Set<WorkOrder> workOrders;

<强> Company.java

@OneToMany(cascade=CascadeType.ALL,mappedBy="company")
private Set<WorkOrder> workOrders;

WorkOrder.java

@ManyToOne(optional=false)
@JoinColumn(name="Companys_list_id", referencedColumnName="id")
private Company company;

@ManyToOne(optional=false)
@JoinColumn(name="Handymans_list_id", referencedColumnName="id")
private Handyman handyman;

保存时

HandyMan handyMan = new HandyMan();
handyMan.setId() // Set the selected id.

Company company = new Company();
company.setId() // Set the selected company id.

workOrder.setCompany(company);
workOrder.setHandyMan(handyMan);

// Populate other details of work order and save using hibernate api.