首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

rnate 联结主键应用及配置用xml方式实现

2012-07-18 
rnate 联合主键应用及配置用xml方式实现?复合(联合)主键映射通常将复合主键相关的属性,单独放到一个类中*

rnate 联合主键应用及配置用xml方式实现

?

复合(联合)主键映射通常将复合主键相关的属性,单独放到一个类中* 此类必须实现序列化接口 implements java.io.Serializable* 覆写hashcode和equals方法类:import java.io.Serializable;public classNamePK implements Serializable{ //主键类private static final long serialVersionUID = 8313037022835155821L;private String firstName;private String lastName;public String getFirstName() {   return firstName;}public void setFirstName(String firstName) {   this.firstName = firstName;}public String getLastName() {   return lastName;}public void setLastName(String lastName) {   this.lastName = lastName;}//重写,交给eclipse自己来写,source/generator hashCode() and equals()public int hashCode() {   final int PRIME = 31;   int result = 1;   result = PRIME * result + ((firstName == null) ? 0 : firstName.hashCode());   result = PRIME * result + ((lastName == null) ? 0 : lastName.hashCode());   return result;}//重写,交给eclipse自己来写public boolean equals(Object obj) {   if (this == obj)    return true;   if (obj == null)    return false;   if (getClass() != obj.getClass())    return false;   final NamePK other = (NamePK) obj;   if (firstName == null) {    if (other.firstName != null)     return false;   } else if (!firstName.equals(other.firstName))    return false;   if (lastName == null) {    if (other.lastName != null)     return false;   } else if (!lastName.equals(other.lastName))    return false;   return true;}}public classUserInfo{privateNamePK namePk;private String sex;private int age;}hbm.xmlUserInfo.hbm.xml<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.zd.model">    <class name="UserInfo" table="user_info" >       <composite-id name="namePk">            <key-property name="firstName" column="first_name" length="12"/>            <key-property name="lastName" column="last_name" length="12"/>        </composite-id>        <property name="sex" length="12"/>        <property name="age" />    </class></hibernate-mapping>测试用例:public void testCompositeId(){   Session session = null;   Transaction ta = null;   try{    session = HibernateUtil.getSession();    ta = session.beginTransaction();     NamePK namePk = new NamePK();      namePk.setFirstName("Zhang");      namePk.setLastName("san");      UserInfo ui = new UserInfo();     ui.setNamePk(namePk);      ui.setSex("M");      ui.setAge(12);     session.save(ui);    ta.commit();   }catch(Exception e){    e.printStackTrace();    if(ta != null){     ta.rollback();    }   }finally{    //关闭session, user变为detached离线对象    HibernateUtil.closeSession(session);   }  }public void testgetId(){   Session session = null;   Transaction ta = null;   try{    session = HibernateUtil.getSession();    ta = session.beginTransaction();      NamePK namePk = new NamePK();      namePk.setFirstName("Zhang");      namePk.setLastName("san");     UserInfo ui = (UserInfo) session.get(UserInfo.class, namePk); //get or load均是要序列化的类,所以要把主键implements Serializable      System.out.println(ui.getAge());      System.out.println(ui.getSex());        ta.commit();   }catch(Exception e){    e.printStackTrace();    if(ta != null){     ta.rollback();    }   }finally{    //关闭session, user变为detached离线对象    HibernateUtil.closeSession(session);   }  }

热点排行