两个啊,,Employee 表建立一个外键和Department的主键关联就可以了啊。

解决方案 »

  1.   

    Create table department(primary key id, name)
    Create table employee(primary key id, foreign key department_id, name)Department.hbm.xml
    <hibernate-mapping>
    <class name="Department" table="department">
    <id name="departmentId" type="integer" unsaved-value="null" >
    <column name="id" sql-type="int" not-null="true"/>
    <generator class="increment"/>
    </id>
    <property name="name" type="string" not-null="true" />
    </class>
    </hibernate-mapping>
    Employee.hbm.xml
    <hibernate-mapping>
    <class name="Employee" table="employee">
    <id name="EmployeeId" type="integer" unsaved-value="null" >
    <column name="id" sql-type="int" not-null="true"/>
    <generator class="increment"/>
    </id>
    <property name="name" type="string" not-null="true" />

    <many-to-one name="departmentId"
    column="department_id"
    class="Department"
    lazy="false" />
    </class>
    </hibernate-mapping>类的方法如下面所示:Department {
      public void setId(int id);
      public int getId();
      public void setName(String name);
      public String getName();
    }Employee {
      public void setId(int id);
      public int getId();
      public void setName(String name);
      public String getName();
      //? 
      public void setDepartment(Department department);
      public Department getDepartment();
      //?
    }
      

  2.   

    在 Employee 中 set get Department 的方法对不对,many-to-one 的配置文件对不对?
      

  3.   

    Department.javapackage org.hibernate.examples.association;/**
     * @author 
     *
     */
    public class Department {
    private int id;
    private String name;

    public Department() {

    }

    /**
     * @return the id
     */
    public int getId() {
    return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
    this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
    return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
    this.name = name;
    }
    }
      

  4.   

    <id name="departmentId" type="integer" unsaved-value="null" >
    <column name="id" sql-type="int" not-null="true"/>
    <generator class="increment"/>
    </id><id name="EmployeeId" type="integer" unsaved-value="null" >
    <column name="id" sql-type="int" not-null="true"/>
    <generator class="increment"/>
    </id>在 id 用 auto_increment 时,上面文件中中的 increment 应该改成 identity.