0

我正在尝试映射 1:M 员工可以使用 Spring MVC 和 Hibernate 分配许多任务我被困在将数据持久化到 Java DB 有什么想法吗?

雇员.java

 public class Employee  implements java.io.Serializable {


 private int empid;
 private String name;
 private int salary;
 private int roleid;
 private Set tasks;

public Set getTasks() {
    return tasks;
}

public void setTasks(Set tasks) {
    this.tasks = tasks;
}
public Employee() {
    tasks = new HashSet();
}

 public void addTask(Task task) {
    this.tasks.add(task);
}

public void removeTask(Task task) {
    this.tasks.remove(task);
}

public Employee(int empid, String name, int salary, int roleid) {
   this.empid = empid;
   this.name = name;
   this.salary = salary;
   this.roleid = roleid;
}

public int getEmpid() {
    return this.empid;
}

public void setEmpid(int empid) {
    this.empid = empid;
}
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
public int getSalary() {
    return this.salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}
public int getRoleid() {
    return this.roleid;
}

public void setRoleid(int roleid) {
    this.roleid = roleid;
}
}

任务.java

public class Task implements java.io.Serializable {

private int taskid;
private String title;
private String description;
private int empid;

public Task() {
}

public Task(int taskid, String title, String description, int empid) {
    this.taskid = taskid;
    this.title = title;
    this.description = description;
    this.empid = empid;
}

public int getTaskid() {
    return this.taskid;
}

public void setTaskid(int taskid) {
    this.taskid = taskid;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getEmpid() {
    return this.empid;
}

public void setEmpid(int empid) {
    this.empid = empid;
}}

员工.hbm

  <hibernate-mapping>
    <class name="Models.Employee" table="EMPLOYEE" schema="APP">
        <id name="empid" type="int">
            <column name="EMPID" />
            <generator class="increment" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="30" not-null="true" />
        </property>
        <property name="salary" type="int">
            <column name="SALARY" not-null="true" />
        </property>
        <property name="roleid" type="int">
            <column name="ROLEID" not-null="true" />
        </property>
        <set cascade="all" name="tasks" table="TASK">
            <key column="EMPID"/>
            <one-to-many class="Models.Task"/>
        </set>

    </class>
</hibernate-mapping>

任务.hbm

<hibernate-mapping>
    <class name="Models.Task" table="TASK" schema="APP">
        <id name="taskid" type="int">
            <column name="TASKID" />
            <generator class="increment" />
        </id>
        <property name="title" type="string">
            <column name="TITLE" length="30" not-null="true" />
        </property>
        <property name="description" type="string">
            <column name="DESCRIPTION" length="50" not-null="true" />
        </property>
        <property name="empid" type="int">
            <column name="EMPID" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

EmployeeAssignTaskController.java

public class EmployeeAssignTaskController extends SimpleFormController {

    public EmployeeAssignTaskController() {
        setCommandClass(Employee.class);
    }

    @Override
    public void doSubmitAction(Object command) {
//        Employee employee = (Employee) command;
//        try {
//            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//            session.beginTransaction();
//            session.save(employee);
//            session.getTransaction().commit();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
4

1 回答 1

0

在任务上映射emp_id为 ManyToOne:

   private Employee emp;
   public Employee getEmpid() {
        return this.emp;
   }

   public void setEmployee(Employee employee) {
       this.employee= employee;
   }

更新Task.hbm配置文件以反映相同。

     <many-to-one 
          name="employee" 
          class="Employee" 
          column="EMPID"/>

addTask在课堂上更新你的方法Employee如下:

public void addTask(Task task) {
   task.setEmployee(this);//<--This will set employee as parent in your task class 
          this.tasks.add(task);
}

完成后,您doSubmitAction应该坚持Employee使用tasks.

于 2012-10-13T15:16:53.857 回答