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永続化する必要があります。Employeetasks

于 2012-10-13T15:16:53.857 に答える