首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

关于施用联合主键

2012-09-02 
关于使用联合主键由于项目中,要用的联合主键,故使用了一下,记录一下学习的心得:1.(1)首先定义一个,PK类,如

关于使用联合主键

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

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;
??? }
}

?

这样就可以了,呵呵!!

热点排行