熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

hibernate關聯關系-一對一

2013-11-23 20:10:49  來源: Java開源技術 

  Java代碼

  package Domain;

  public class Person {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public String getName() {

  return name;

  }

  public void setName(String name) {

  thisname = name;

  }

  public IdCard getIdCard() {

  return idCard;

  }

  public void setIdCard(IdCard idCard) {

  thisidCard = idCard;

  }

  private int id;

  private String name;

  private IdCard idCard;

  }

  Java代碼

  package Domain;

  import javautilDate;

  public class IdCard {

  public int getId() {

  return id;

  }

  public void setId(int id) {

  thisid = id;

  }

  public Date getUsefulLife() {

  return usefulLife;

  }

  public void setUsefulLife(Date usefulLife) {

  thisusefulLife = usefulLife;

  }

  public Person getPerson() {

  return person;

  }

  public void setPerson(Person person) {

  thisperson = person;

  }

  private int id;

  private Date usefulLife;

  private Person person;

  }

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  mappingdtd>

  <hibernatemapping package=Domain>

  <class name=Person table=person>

  <id name=id>

  <generator class=native />

  </id>

  <property name=name />

  <onetoone name=idCard/>

  </class>

  </hibernatemapping>

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  mappingdtd>

  <hibernatemapping 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/>

  <onetoone name=person constrained=true/>

  </class>

  </hibernatemapping>

  Java代碼

  package DaoTest;

  import orghibernateSession;

  import orghibernateTransaction;

  import UtilshibernateUtil;

  import DomainDepartment;

  import DomainEmployee;

  public class OneManyTest {

  public static void main(String[] args) {

  Department depart = add();

  queryDepart(departgetId());

  }

  static Department queryDepart(int departId) {

  Session s = null;

  try {

  s = hibernateUtilgetSession();

  Department depart = (Department) sget(Departmentclass departId);

  Systemoutprintln(emp  size: + departgetEmps()size());

  return depart;

  } finally {

  if (s != null)

  sclose();

  }

  }

  static Department add() {

  Session s = null;

  Transaction tx = null;

  try {

  Department depart = new Department();

  departsetName(+Manager+);

  Employee emp = new Employee();

  empsetDepart(depart);// 對象模型建立兩個對象的關聯

  empsetName(emp name);

  Employee emp = new Employee();

  empsetDepart(depart);// 對象模型建立兩個對象的關聯

  empsetName(emp name);

  /*Set<Employee> emps = new HashSet<Employee>();

  empsadd(emp);

  empsadd(emp);

  departsetEmps(emps);*/

  s = hibernateUtilgetSession();

  tx = sbeginTransaction();

  ssave(depart);

  ssave(emp);

  ssave(emp);

  Systemoutprintln();

  mit();

  return depart;

  } finally {

  if (s != null)

  sclose();

  }

  }

  }

  Java代碼

  package DaoTest;

  import javautilDate;

  import orghibernateSession;

  import orghibernateTransaction;

  import UtilshibernateUtil;

  import DomainIdCard;

  import DomainPerson;

  public class OneOneQueryTest {

  public static void main(String[] args) {

  add();

  query();

  }

  static Person query(int id){

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtilgetSession();

  tx = sbeginTransaction();

  // 一對一 查詢主對象

  // Person p = (Person)sget(Personclass id);

  // Systemoutprintln( pgetIdCard()getUsefulLife() + pgetIdCard()getUsefulLife());

  // 一對一 查詢從對象

  IdCard idCard = (IdCard)sget(IdCardclass id);

  Systemoutprintln(idCardgetPerson()getName() + idCardgetPerson()getName());

  mit();

  //return p;

  return null;

  }finally{

  if(s != null){

  sclose();

  }

  }

  }

  static Person add(){

  Session s = null;

  Transaction tx = null;

  try{

  s = hibernateUtilgetSession();

  IdCard idCard = new IdCard();

  idCardsetUsefulLife(new Date());

  Person p = new Person();

  psetName(Chen Huiyi);

  psetIdCard(idCard);

  // 用Person對象來指定IdCard的id

  idCardsetPerson(p);

  tx = sbeginTransaction();

  ssave(p);

  ssave(idCard);

  mit();

  return p;

  }finally{

  if(s != null){

  sclose();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28080.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.