最新帖子 精华区 社区服务 会员列表 统计排行
主题 : hibernate关联关系-多对一
ceshi123 离线
级别: 管理员
宣传大使奖 特殊贡献奖 灌水天才奖
显示用户信息 
0  发表于: 2010-08-05 07:38

hibernate关联关系-多对一

购买流程:注册论坛账号------->在线充值购买论坛交易币------->下载毕业设计将直接扣除交易币
  模型:员工Employee — 部门Department

  Java代码

  package Domain;

  public class Employee {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this.id = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  public Department getDepart() {

  return depart;

  }

  public void setDepart(Department depart) {

  this.depart = depart;

  }

  private int id;

  private String name;

  private Department depart;

  }

  Java代码

  package Domain;

  import java.util.Set;

  public class Department {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this.id = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  public Set<Employee> getEmps() {

  return emps;

  }

  public void setEmps(Set<Employee> emps) {

  this.emps = emps;

  }

  private int id;

  private String name;

  private Set<Employee> emps ;

  }

  Xml代码

  <?xml version="1.0"?>

  <!DOCTYPE hibernate-mapping PUBLIC

  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  <hibernate-mapping package="Domain">

  <class name="Employee" table="employee">

  <id name="id">

  <generator class="native" />

  </id>

  <property name="name" unique="true"/>

  <many-to-one name="depart" column="depart_id"/>

  </class>

  </hibernate-mapping>

  Xml代码

  <?xml version="1.0"?>

  <!DOCTYPE hibernate-mapping PUBLIC

  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  <hibernate-mapping package="Domain">

  <class name="Department" table="department">

  <id name="id">

  <generator class="native" />

  </id>

  <property name="name" unique="true"/>

  <set name="emps">

  <key column="depart_id"/>

  <one-to-many class="Employee"/>

  </set>

  </class>

  </hibernate-mapping>

  Java代码

  package Dao;

  import Domain.Employee;

  public interface EmployeeDAO {

  public void saveEmployee(Employee emp);

  public Employee findEmployeeByName(String name);

  public Employee findEmployeeById(int id);

  public void updateEmployee(Employee emp);

  public void removeEmployee(Employee emp);

  }

快速回复 顶端
内容
HTML 代码不可用
使用签名
Wind Code自动转换

验证问题:本站域名是什么?答案:cccbbs.net  正确答案:cccbbs.net
按"Ctrl+Enter"直接提交