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

hibernate关联关系-一对一

购买流程:注册论坛账号------->在线充值购买论坛交易币------->下载毕业设计将直接扣除交易币
  Java代码

  package Domain;

  public class Person {

  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 IdCard getIdCard() {

  return idCard;

  }

  public void setIdCard(IdCard idCard) {

  this.idCard = idCard;

  }

  private int id;

  private String name;

  private IdCard idCard;

  }

  Java代码

  package Domain;

  import java.util.Date;

  public class IdCard {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  this.id = id;

  }

  public Date getUsefulLife() {

  return usefulLife;

  }

  public void setUsefulLife(Date usefulLife) {

  this.usefulLife = usefulLife;

  }

  public Person getPerson() {

  return person;

  }

  public void setPerson(Person person) {

  this.person = person;

  }

  private int id;

  private Date usefulLife;

  private Person person;

  }

  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="Person" table="person">

  <id name="id">

  <generator class="native" />

  </id>

  <property name="name" />

  <one-to-one name="idCard"/>

  </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="IdCard" table="id_card">

  <id name="id">

  <generator class="foreign">

  <param name="property">person</param>

  </generator>

  </id>

  <property name="usefulLife" column="useful_Life"/>

  <one-to-one name="person" constrained="true"/>

  </class>

  </hibernate-mapping>

  Java代码

  package Dao.Test;

  import org.hibernate.Session;

  import org.hibernate.Transaction;

  import Utils.hibernateUtil;

  import Domain.Department;

  import Domain.Employee;

  public class One2ManyTest {

  public static void main(String[] args) {

  Department depart = add();

  queryDepart(depart.getId());

  }

  static Department queryDepart(int departId) {

  Session s = null;

  try {

  s = hibernateUtil.getSession();

  Department depart = (Department) s.get(Department.class, departId);

  System.out.println("emp  size: " + depart.getEmps().size());

  return depart;

  } finally {

  if (s != null)

  s.close();

  }

  }

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

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