关于使用联合主键

由于项目中,要用的联合主键,故使用了一下,记录一下学习的心得:

1.

(1)首先定义一个,PK类,如FriendPK类,这个类必须实现了serializable序列化接口

(2)必须有一个无参的构造函数 如: public FriendPK() {
    }

(3)必须重写hashCode和equals方法.

下面给出我的PK类的详细写法:

package com.hna.sns.entities.member;

import java.io.Serializable;
import javax.persistence.Column;

import javax.persistence.Embeddable;


/**
 * <pre>
 * 说明:联合主键定义类.
 * </pre>
 *
 * @author 徐浩  2011-3-30
 */
@Embeddable
public class FriendPK implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 7988302031950919652L;

    // 用户ID
    private long mlUId;

    // 用户好友ID
    private long mnFuid;

    /**
     * Instantiates a new friend pk.
     *
     * @author 徐浩 2011-3-30
     */
    public FriendPK() {
    }

    /**
     * <pre>
     * Gets the UId.
     * </pre>
     *
     * @return the UId
     * @author 徐浩 2011-3-30
     */
    @Column(name = "uid", length = 8, nullable = false)
    public long getMlUId() {
        return mlUId;
    }

    /**
     * <pre>
     * Sets the mlUId.
     * </pre>
     *
     * @param plUId
     *            the new UId
     * @author 徐浩 2011-3-30
     */
    public void setMlUId(long plUId) {
        this.mlUId = plUId;
    }

    /**
     * <pre>
     * Gets the Fuid.
     * </pre>
     *
     * @return the Fuid
     * @author 徐浩 2011-3-30
     */
    @Column(name = "fuid", length = 8, nullable = false)
    public long getMnFuid() {
        return mnFuid;
    }

    /**
     * <pre>
     * Sets the mnFuid.
     * </pre>
     *
     * @param pnFuid
     *            the new Fuid
     * @author 徐浩 2011-3-30
     */
    public void setMnFuid(long pnFuid) {
        this.mnFuid = pnFuid;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return (int) (mlUId + mnFuid);
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object pobj) {
        if (pobj == this)
            return true;
        if (!(pobj instanceof FriendPK))
            return false;

        FriendPK pk = (FriendPK) pobj;
        return pk.mlUId == mlUId && pk.mnFuid == mnFuid;
    }
}

 

(3) 定义一个实体类,如Friend


package com.hna.sns.entities.member;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.hna.framework.entities.base.BaseEntity;
import com.hna.sns.dto.member.FriendDTO;

/**
 * <pre>
 * The Class Friend.
 * </pre>
 *
 * @author 徐浩  2011-3-30
 */
@Entity
@Table(name = "uchome_friend")
public class Friend extends BaseEntity<Friend, FriendDTO> {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    // id
    private FriendPK mobjid;

    // 用户好友名
    private String mstrfusername;

    // 用户好友关系状态:'0'添加好友没有等待通过,'1'双方已经是好友
    private int mnstatus;

    // 好友所在的好友组ID
    private int mnGid;

    // 好友描述
    private String mstrnote;

    // 好友之间的活动关系数
    private long mlNum;

    // 加好友的时间戳
    private int mnDateline;

    /**
     * Instantiates a new friend.
     *
     * @author 徐浩  2011-3-30
     */
    public Friend() {
    }

    /**
     * Instantiates a new friend.
     *
     * @param plUId the pl u id
     * @param plFuid the pl fuid
     * @author 徐浩  2011-3-30
     */
    public Friend(long plUId, long plFuid) {
        FriendPK id = new FriendPK();
        id.setMlUId(plUId);
        id.setMnFuid(plFuid);
        this.mobjid = id;
    }

    /**
     * <pre>
     * Gets the mstrfusername.
     * </pre>
     *
     * @return the mstrfusername
     * @author 徐浩 2011-3-30
     */
    @Column(name = "fusername")
    public String getMstrfusername() {
        return mstrfusername;
    }

    /**
     * <pre>
     * Sets the pstrfusername.
     * </pre>
     *
     * @param pstrfusername the new fusername
     * @author 徐浩 2011-3-30
     */
    public void setMstrfusername(String pstrfusername) {
        this.mstrfusername = pstrfusername;
    }

    /**
     * <pre>
     * Gets the mnGid.
     * </pre>
     *
     * @return the mnGid
     * @author 徐浩 2011-3-30
     */
    @Column(name = "gid")
    public int getMnGid() {
        return mnGid;
    }

    /**
     * <pre>
     * Sets the mnGid.
     * </pre>
     *
     * @param pnGid the new Gid
     * @author 徐浩 2011-3-30
     */
    public void setMnGid(int pnGid) {
        this.mnGid = pnGid;
    }

    /**
     * <pre>
     * Gets the mstrnote.
     * </pre>
     *
     * @return the mstrnote
     * @author 徐浩 2011-3-30
     */
    @Column(name = "note")
    public String getMstrnote() {
        return mstrnote;
    }

    /**
     * <pre>
     * Sets the mstrnote.
     * </pre>
     *
     * @param pstrnote the new note
     * @author 徐浩 2011-3-30
     */
    public void setMstrnote(String pstrnote) {
        this.mstrnote = pstrnote;
    }

    /**
     * <pre>
     * Gets the mlNum.
     * </pre>
     *
     * @return the mlNum
     * @author 徐浩 2011-3-30
     */
    @Column(name = "num", length = 8)
    public long getMlNum() {
        return mlNum;
    }

    /**
     * <pre>
     * Sets the mlNum.
     * </pre>
     *
     * @param plNum the new Num
     * @author 徐浩 2011-3-30
     */
    public void setMlNum(long plNum) {
        this.mlNum = plNum;
    }

    /**
     * <pre>
     * Gets the id.
     * </pre>
     *
     * @return the friend pk
     * @author 徐浩  2011-3-30
     */
    @EmbeddedId
    public FriendPK getId() {
        return mobjid;
    }

    /**
     * <pre>
     * Sets the id.
     * </pre>
     * @param pid the new id
     * @author 徐浩 2011-3-30
     */
    public void setId(FriendPK pid) {
        this.mobjid = pid;
    }

    /**
     * <pre>
     * Gets the mlNum.
     * </pre>
     *
     * @return the mlNum
     * @author 徐浩 2011-3-30
     */
    @Column(name = "dateline")
    public int getMnDateline() {
        return mnDateline;
    }

    /**
     * <pre>
     * Sets the mnDateline.
     * </pre>
     *
     * @param pnDateline the new Dateline
     * @author 徐浩 2011-3-30
     */
    public void setMnDateline(int pnDateline) {
        this.mnDateline = pnDateline;
    }

    /**
     * <pre>
     * Gets the mnstatus.
     * </pre>
     *
     * @return the mnstatus
     * @author 徐浩 2011-3-30
     */
    @Column(name = "status")
    public int getMnstatus() {
        return mnstatus;
    }

    /**
     * <pre>
     * Sets the mnstatus.
     * </pre>
     *
      * @param pnstatus the new status
     * @author 徐浩 2011-3-30
     */
    public void setMnstatus(int pnstatus) {
        this.mnstatus = pnstatus;
    }
}

 

这样就可以了,呵呵!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值