复合主键映射

如果数据库中表使用了复合主键,应该怎样映射?

需要使用一个类来描述主键信息



1、建立表
DROP TABLE user_course ;



CREATE TABLE user_course (

    userid            varchar(20)          ,

    cid               int               ,

    status            int               ,

    primary key (userid,cid)

);




2、生成表映射


如果使用了复合主键,则该表生成映射时可以不选择主键生成方式,会自动按照复合主键处理。生成出现了两个类,一个为对应表的pojo类,另一个为UserCourseId类用来描述UserCourse的主键,主键有两个,因此在UserCourseId中包含userid和cid属性。

由于按ID查询时,需要传入一个可以序列化的id值,因此id类必须实现序列化接口

注意:主键类中覆写了equals与hashCode方法,用来判断主键是否重复。

package org.liky.pojo;







public class UserCourse implements java.io.Serializable {



    // Fields



    private UserCourseId id;



    private Integer status;



    // Constructors



  

    public UserCourse() {

    }



  

    public UserCourse(UserCourseId id) {

       this.id = id;

    }



  

    public UserCourse(UserCourseId id, Integer status) {

       this.id = id;

       this.status = status;

    }



    // Property accessors



    public UserCourseId getId() {

       return this.id;

    }



    public void setId(UserCourseId id) {

       this.id = id;

    }



    public Integer getStatus() {

       return this.status;

    }



    public void setStatus(Integer status) {

       this.status = status;

    }



}

package org.liky.pojo;







public class UserCourseId implements java.io.Serializable {



    // Fields



    private String userid;



    private Integer cid;



    // Constructors



  

    public UserCourseId() {

    }



  

    public UserCourseId(String userid, Integer cid) {

       this.userid = userid;

       this.cid = cid;

    }



    // Property accessors



    public String getUserid() {

       return this.userid;

    }



    public void setUserid(String userid) {

       this.userid = userid;

    }



    public Integer getCid() {

       return this.cid;

    }



    public void setCid(Integer cid) {

       this.cid = cid;

    }



    public boolean equals(Object other) {

       if ((this == other))

           return true;

       if ((other == null))

           return false;

       if (!(other instanceof UserCourseId))

           return false;

       UserCourseId castOther = (UserCourseId) other;



       if (this.getUserid() != null

              && this.userid.equals(castOther.getUserid())) {

           if (this.cid != null && this.cid.equals(castOther.getCid())) {

              return true;

           } else {

              return false;

           }

       } else {

           return false;

       }

    }



    public int hashCode() {

       int result = 17;



       result = 37 * result

              + (getUserid() == null ? 0 : this.getUserid().hashCode());

       result = 37 * result

              + (getCid() == null ? 0 : this.getCid().hashCode());

       return result;

    }



}




3、查看映射文件
在映射文件中,会通过composite-id对主键属性进行配置,语法与之前的实体映射类似

<?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">

<!--

    Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

    <class name="org.liky.pojo.UserCourse" table="user_course" catalog="testdb">

    <!-- 表示UserCourse类中id属性为复合主键值,类型为UserCourseId -->

        <composite-id name="id" class="org.liky.pojo.UserCourseId">

        <!-- 其中的userid属性与userid字段对应 -->

            <key-property name="userid" type="java.lang.String">

                <column name="userid" length="20" />

            </key-property>

            <key-property name="cid" type="java.lang.Integer">

                <column name="cid" />

            </key-property>

        </composite-id>

        <property name="status" type="java.lang.Integer">

            <column name="status" />

        </property>

    </class>

</hibernate-mapping>






4、测试
    public static void main(String[] args) {

       UserCourseDAO ucdao = DAOFactory.getTestDAOInstance();

     

       UserCourse uc = new UserCourse();

       uc.setStatus(1);

       uc.setId(new UserCourseId());

       uc.getId().setCid(1);

       uc.getId().setUserid("123456789123456789");

     

       try {

           ucdao.doCreate(uc);

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }    

    }



你可能感兴趣的:(.net,xml,Hibernate,MyEclipse)