hibernate 无主键操作——联合主键

今天给了两个表,但是没有主键,傻眼了,没有配到过没有主键的表(哦承认自己少见多怪了),上网查了一下要用联合主键,把一条数据看做一个主键,上网查了一下都说做好不要用联合主键,不用联合主键应该怎么做呢?思考中......

下来说说使用联合主键的方法吧。

用Myeclipse用具生成了四个类,分别是:TradeList.java,TradeListId.java,TradeInfo.java和TradeInfoId.java

具体的让我们来看一下:

package com.iplamp.enterprise.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * TradeListId entity. @author MyEclipse Persistence Tools
 */
@Embeddable
public class TradeListId implements java.io.Serializable {

	// Fields

	private String tradeId;
	private String categoryId;
	private String tradeName;
	private String applier;
	private Integer EId;

	// Property accessors

	@Column(name = "trade_id", length = 20)
	public String getTradeId() {
		return this.tradeId;
	}

	public void setTradeId(String tradeId) {
		this.tradeId = tradeId;
	}

	@Column(name = "category_id", length = 20)
	public String getCategoryId() {
		return this.categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	@Column(name = "trade_name")
	public String getTradeName() {
		return this.tradeName;
	}

	public void setTradeName(String tradeName) {
		this.tradeName = tradeName;
	}

	@Column(name = "applier")
	public String getApplier() {
		return this.applier;
	}

	public void setApplier(String applier) {
		this.applier = applier;
	}

	@Column(name = "e_id", nullable = false)
	public Integer getEId() {
		return this.EId;
	}

	public void setEId(Integer EId) {
		this.EId = EId;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof TradeListId))
			return false;
		TradeListId castOther = (TradeListId) other;

		return ((this.getTradeId() == castOther.getTradeId()) || (this
				.getTradeId() != null && castOther.getTradeId() != null && this
				.getTradeId().equals(castOther.getTradeId())))
				&& ((this.getCategoryId() == castOther.getCategoryId()) || (this
						.getCategoryId() != null
						&& castOther.getCategoryId() != null && this
						.getCategoryId().equals(castOther.getCategoryId())))
				&& ((this.getTradeName() == castOther.getTradeName()) || (this
						.getTradeName() != null
						&& castOther.getTradeName() != null && this
						.getTradeName().equals(castOther.getTradeName())))
				&& ((this.getApplier() == castOther.getApplier()) || (this
						.getApplier() != null && castOther.getApplier() != null && this
						.getApplier().equals(castOther.getApplier())))
				&& ((this.getEId() == castOther.getEId()) || (this.getEId() != null
						&& castOther.getEId() != null && this.getEId().equals(
						castOther.getEId())));
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result
				+ (getTradeId() == null ? 0 : this.getTradeId().hashCode());
		result = 37
				* result
				+ (getCategoryId() == null ? 0 : this.getCategoryId()
						.hashCode());
		result = 37 * result
				+ (getTradeName() == null ? 0 : this.getTradeName().hashCode());
		result = 37 * result
				+ (getApplier() == null ? 0 : this.getApplier().hashCode());
		result = 37 * result
				+ (getEId() == null ? 0 : this.getEId().hashCode());
		return result;
	}

}


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

/**
 * TradeList entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "ie_trade_list", catalog = "iplamp_enterprise")
public class TradeList implements java.io.Serializable {

    public static String FIELD_EID = "id.EId" ;
    public static String FIELD_TRADEID = "id.tradeId" ;
	// Fields

	private TradeListId id;

	// Property accessors
	@EmbeddedId
	@AttributeOverrides({
			@AttributeOverride(name = "tradeId", column = @Column(name = "trade_id", length = 20)),
			@AttributeOverride(name = "categoryId", column = @Column(name = "category_id", length = 20)),
			@AttributeOverride(name = "tradeName", column = @Column(name = "trade_name")),
			@AttributeOverride(name = "applier", column = @Column(name = "applier")),
			@AttributeOverride(name = "EId", column = @Column(name = "e_id", nullable = false)) })
	public TradeListId getId() {
		return this.id;
	}

	public void setId(TradeListId id) {
		this.id = id;
	}
}



import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * TradeInfoId entity. @author MyEclipse Persistence Tools
 */
@Embeddable
public class TradeInfoId implements java.io.Serializable {

	// Fields

	private String regNo;
	private String catId;
	private String applyDate;
	private String applierCn;
	private String applierAddrCn;
	private String applierEn;
	private String applierAddrEn;
	private String tradeImgUrl;
	private String tradeImgThumbUrl;
	private String imgUrl;
	private String imgThumbUrl;
	private String serviceList;
	private String similarGroup;
	private String csggId;
	private String zcggId;
	private String csggDate;
	private String zcggDate;
	private String validYear;
	private String hqzdDate;
	private String gjczDate;
	private String priorityDate;
	private String agentName;
	private String specifiedColor;
	private String tradeType;
	private String isShare;
	private String remark;
	private String tradeflow;

	// Constructors

	// Property accessors

	@Column(name = "regNo", length = 20)
	public String getRegNo() {
		return this.regNo;
	}

	public void setRegNo(String regNo) {
		this.regNo = regNo;
	}

	@Column(name = "catId", length = 20)
	public String getCatId() {
		return this.catId;
	}

	public void setCatId(String catId) {
		this.catId = catId;
	}

	@Column(name = "applyDate", length = 20)
	public String getApplyDate() {
		return this.applyDate;
	}

	public void setApplyDate(String applyDate) {
		this.applyDate = applyDate;
	}

	@Column(name = "applierCN")
	public String getApplierCn() {
		return this.applierCn;
	}

	public void setApplierCn(String applierCn) {
		this.applierCn = applierCn;
	}

	@Column(name = "applierAddrCN")
	public String getApplierAddrCn() {
		return this.applierAddrCn;
	}

	public void setApplierAddrCn(String applierAddrCn) {
		this.applierAddrCn = applierAddrCn;
	}

	@Column(name = "applierEN")
	public String getApplierEn() {
		return this.applierEn;
	}

	public void setApplierEn(String applierEn) {
		this.applierEn = applierEn;
	}

	@Column(name = "applierAddrEN")
	public String getApplierAddrEn() {
		return this.applierAddrEn;
	}

	public void setApplierAddrEn(String applierAddrEn) {
		this.applierAddrEn = applierAddrEn;
	}

	@Column(name = "tradeImgUrl")
	public String getTradeImgUrl() {
		return this.tradeImgUrl;
	}

	public void setTradeImgUrl(String tradeImgUrl) {
		this.tradeImgUrl = tradeImgUrl;
	}

	@Column(name = "tradeImgThumbUrl")
	public String getTradeImgThumbUrl() {
		return this.tradeImgThumbUrl;
	}

	public void setTradeImgThumbUrl(String tradeImgThumbUrl) {
		this.tradeImgThumbUrl = tradeImgThumbUrl;
	}

	@Column(name = "imgUrl")
	public String getImgUrl() {
		return this.imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	@Column(name = "imgThumbUrl")
	public String getImgThumbUrl() {
		return this.imgThumbUrl;
	}

	public void setImgThumbUrl(String imgThumbUrl) {
		this.imgThumbUrl = imgThumbUrl;
	}

	@Column(name = "serviceList")
	public String getServiceList() {
		return this.serviceList;
	}

	public void setServiceList(String serviceList) {
		this.serviceList = serviceList;
	}

	@Column(name = "similarGroup")
	public String getSimilarGroup() {
		return this.similarGroup;
	}

	public void setSimilarGroup(String similarGroup) {
		this.similarGroup = similarGroup;
	}

	@Column(name = "csggId")
	public String getCsggId() {
		return this.csggId;
	}

	public void setCsggId(String csggId) {
		this.csggId = csggId;
	}

	@Column(name = "zcggId")
	public String getZcggId() {
		return this.zcggId;
	}

	public void setZcggId(String zcggId) {
		this.zcggId = zcggId;
	}

	@Column(name = "csggDate")
	public String getCsggDate() {
		return this.csggDate;
	}

	public void setCsggDate(String csggDate) {
		this.csggDate = csggDate;
	}

	@Column(name = "zcggDate")
	public String getZcggDate() {
		return this.zcggDate;
	}

	public void setZcggDate(String zcggDate) {
		this.zcggDate = zcggDate;
	}

	@Column(name = "validYear")
	public String getValidYear() {
		return this.validYear;
	}

	public void setValidYear(String validYear) {
		this.validYear = validYear;
	}

	@Column(name = "hqzdDate")
	public String getHqzdDate() {
		return this.hqzdDate;
	}

	public void setHqzdDate(String hqzdDate) {
		this.hqzdDate = hqzdDate;
	}

	@Column(name = "gjczDate")
	public String getGjczDate() {
		return this.gjczDate;
	}

	public void setGjczDate(String gjczDate) {
		this.gjczDate = gjczDate;
	}

	@Column(name = "priorityDate")
	public String getPriorityDate() {
		return this.priorityDate;
	}

	public void setPriorityDate(String priorityDate) {
		this.priorityDate = priorityDate;
	}

	@Column(name = "agentName")
	public String getAgentName() {
		return this.agentName;
	}

	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}

	@Column(name = "specifiedColor")
	public String getSpecifiedColor() {
		return this.specifiedColor;
	}

	public void setSpecifiedColor(String specifiedColor) {
		this.specifiedColor = specifiedColor;
	}

	@Column(name = "tradeType")
	public String getTradeType() {
		return this.tradeType;
	}

	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}

	@Column(name = "isShare")
	public String getIsShare() {
		return this.isShare;
	}

	public void setIsShare(String isShare) {
		this.isShare = isShare;
	}

	@Column(name = "remark")
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Column(name = "tradeflow")
	public String getTradeflow() {
		return this.tradeflow;
	}

	public void setTradeflow(String tradeflow) {
		this.tradeflow = tradeflow;
	}

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof TradeInfoId))
			return false;
		TradeInfoId castOther = (TradeInfoId) other;

		return ((this.getRegNo() == castOther.getRegNo()) || (this.getRegNo() != null
				&& castOther.getRegNo() != null && this.getRegNo().equals(
				castOther.getRegNo())))
				&& ((this.getCatId() == castOther.getCatId()) || (this
						.getCatId() != null && castOther.getCatId() != null && this
						.getCatId().equals(castOther.getCatId())))
				&& ((this.getApplyDate() == castOther.getApplyDate()) || (this
						.getApplyDate() != null
						&& castOther.getApplyDate() != null && this
						.getApplyDate().equals(castOther.getApplyDate())))
				&& ((this.getApplierCn() == castOther.getApplierCn()) || (this
						.getApplierCn() != null
						&& castOther.getApplierCn() != null && this
						.getApplierCn().equals(castOther.getApplierCn())))
				&& ((this.getApplierAddrCn() == castOther.getApplierAddrCn()) || (this
						.getApplierAddrCn() != null
						&& castOther.getApplierAddrCn() != null && this
						.getApplierAddrCn()
						.equals(castOther.getApplierAddrCn())))
				&& ((this.getApplierEn() == castOther.getApplierEn()) || (this
						.getApplierEn() != null
						&& castOther.getApplierEn() != null && this
						.getApplierEn().equals(castOther.getApplierEn())))
				&& ((this.getApplierAddrEn() == castOther.getApplierAddrEn()) || (this
						.getApplierAddrEn() != null
						&& castOther.getApplierAddrEn() != null && this
						.getApplierAddrEn()
						.equals(castOther.getApplierAddrEn())))
				&& ((this.getTradeImgUrl() == castOther.getTradeImgUrl()) || (this
						.getTradeImgUrl() != null
						&& castOther.getTradeImgUrl() != null && this
						.getTradeImgUrl().equals(castOther.getTradeImgUrl())))
				&& ((this.getTradeImgThumbUrl() == castOther
						.getTradeImgThumbUrl()) || (this.getTradeImgThumbUrl() != null
						&& castOther.getTradeImgThumbUrl() != null && this
						.getTradeImgThumbUrl().equals(
								castOther.getTradeImgThumbUrl())))
				&& ((this.getImgUrl() == castOther.getImgUrl()) || (this
						.getImgUrl() != null && castOther.getImgUrl() != null && this
						.getImgUrl().equals(castOther.getImgUrl())))
				&& ((this.getImgThumbUrl() == castOther.getImgThumbUrl()) || (this
						.getImgThumbUrl() != null
						&& castOther.getImgThumbUrl() != null && this
						.getImgThumbUrl().equals(castOther.getImgThumbUrl())))
				&& ((this.getServiceList() == castOther.getServiceList()) || (this
						.getServiceList() != null
						&& castOther.getServiceList() != null && this
						.getServiceList().equals(castOther.getServiceList())))
				&& ((this.getSimilarGroup() == castOther.getSimilarGroup()) || (this
						.getSimilarGroup() != null
						&& castOther.getSimilarGroup() != null && this
						.getSimilarGroup().equals(castOther.getSimilarGroup())))
				&& ((this.getCsggId() == castOther.getCsggId()) || (this
						.getCsggId() != null && castOther.getCsggId() != null && this
						.getCsggId().equals(castOther.getCsggId())))
				&& ((this.getZcggId() == castOther.getZcggId()) || (this
						.getZcggId() != null && castOther.getZcggId() != null && this
						.getZcggId().equals(castOther.getZcggId())))
				&& ((this.getCsggDate() == castOther.getCsggDate()) || (this
						.getCsggDate() != null
						&& castOther.getCsggDate() != null && this
						.getCsggDate().equals(castOther.getCsggDate())))
				&& ((this.getZcggDate() == castOther.getZcggDate()) || (this
						.getZcggDate() != null
						&& castOther.getZcggDate() != null && this
						.getZcggDate().equals(castOther.getZcggDate())))
				&& ((this.getValidYear() == castOther.getValidYear()) || (this
						.getValidYear() != null
						&& castOther.getValidYear() != null && this
						.getValidYear().equals(castOther.getValidYear())))
				&& ((this.getHqzdDate() == castOther.getHqzdDate()) || (this
						.getHqzdDate() != null
						&& castOther.getHqzdDate() != null && this
						.getHqzdDate().equals(castOther.getHqzdDate())))
				&& ((this.getGjczDate() == castOther.getGjczDate()) || (this
						.getGjczDate() != null
						&& castOther.getGjczDate() != null && this
						.getGjczDate().equals(castOther.getGjczDate())))
				&& ((this.getPriorityDate() == castOther.getPriorityDate()) || (this
						.getPriorityDate() != null
						&& castOther.getPriorityDate() != null && this
						.getPriorityDate().equals(castOther.getPriorityDate())))
				&& ((this.getAgentName() == castOther.getAgentName()) || (this
						.getAgentName() != null
						&& castOther.getAgentName() != null && this
						.getAgentName().equals(castOther.getAgentName())))
				&& ((this.getSpecifiedColor() == castOther.getSpecifiedColor()) || (this
						.getSpecifiedColor() != null
						&& castOther.getSpecifiedColor() != null && this
						.getSpecifiedColor().equals(
								castOther.getSpecifiedColor())))
				&& ((this.getTradeType() == castOther.getTradeType()) || (this
						.getTradeType() != null
						&& castOther.getTradeType() != null && this
						.getTradeType().equals(castOther.getTradeType())))
				&& ((this.getIsShare() == castOther.getIsShare()) || (this
						.getIsShare() != null && castOther.getIsShare() != null && this
						.getIsShare().equals(castOther.getIsShare())))
				&& ((this.getRemark() == castOther.getRemark()) || (this
						.getRemark() != null && castOther.getRemark() != null && this
						.getRemark().equals(castOther.getRemark())))
				&& ((this.getTradeflow() == castOther.getTradeflow()) || (this
						.getTradeflow() != null
						&& castOther.getTradeflow() != null && this
						.getTradeflow().equals(castOther.getTradeflow())));
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result
				+ (getRegNo() == null ? 0 : this.getRegNo().hashCode());
		result = 37 * result
				+ (getCatId() == null ? 0 : this.getCatId().hashCode());
		result = 37 * result
				+ (getApplyDate() == null ? 0 : this.getApplyDate().hashCode());
		result = 37 * result
				+ (getApplierCn() == null ? 0 : this.getApplierCn().hashCode());
		result = 37
				* result
				+ (getApplierAddrCn() == null ? 0 : this.getApplierAddrCn()
						.hashCode());
		result = 37 * result
				+ (getApplierEn() == null ? 0 : this.getApplierEn().hashCode());
		result = 37
				* result
				+ (getApplierAddrEn() == null ? 0 : this.getApplierAddrEn()
						.hashCode());
		result = 37
				* result
				+ (getTradeImgUrl() == null ? 0 : this.getTradeImgUrl()
						.hashCode());
		result = 37
				* result
				+ (getTradeImgThumbUrl() == null ? 0 : this
						.getTradeImgThumbUrl().hashCode());
		result = 37 * result
				+ (getImgUrl() == null ? 0 : this.getImgUrl().hashCode());
		result = 37
				* result
				+ (getImgThumbUrl() == null ? 0 : this.getImgThumbUrl()
						.hashCode());
		result = 37
				* result
				+ (getServiceList() == null ? 0 : this.getServiceList()
						.hashCode());
		result = 37
				* result
				+ (getSimilarGroup() == null ? 0 : this.getSimilarGroup()
						.hashCode());
		result = 37 * result
				+ (getCsggId() == null ? 0 : this.getCsggId().hashCode());
		result = 37 * result
				+ (getZcggId() == null ? 0 : this.getZcggId().hashCode());
		result = 37 * result
				+ (getCsggDate() == null ? 0 : this.getCsggDate().hashCode());
		result = 37 * result
				+ (getZcggDate() == null ? 0 : this.getZcggDate().hashCode());
		result = 37 * result
				+ (getValidYear() == null ? 0 : this.getValidYear().hashCode());
		result = 37 * result
				+ (getHqzdDate() == null ? 0 : this.getHqzdDate().hashCode());
		result = 37 * result
				+ (getGjczDate() == null ? 0 : this.getGjczDate().hashCode());
		result = 37
				* result
				+ (getPriorityDate() == null ? 0 : this.getPriorityDate()
						.hashCode());
		result = 37 * result
				+ (getAgentName() == null ? 0 : this.getAgentName().hashCode());
		result = 37
				* result
				+ (getSpecifiedColor() == null ? 0 : this.getSpecifiedColor()
						.hashCode());
		result = 37 * result
				+ (getTradeType() == null ? 0 : this.getTradeType().hashCode());
		result = 37 * result
				+ (getIsShare() == null ? 0 : this.getIsShare().hashCode());
		result = 37 * result
				+ (getRemark() == null ? 0 : this.getRemark().hashCode());
		result = 37 * result
				+ (getTradeflow() == null ? 0 : this.getTradeflow().hashCode());
		return result;
	}

}



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

/**
 * TradeInfo entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "ie_trade_info", catalog = "iplamp_enterprise")
public class TradeInfo implements java.io.Serializable {

    public static String FIELD_REGNO = "id.regNo";

    // Fields

    private TradeInfoId id;

    // Constructors

    /**
     * default constructor
     */
    public TradeInfo() {
    }

    /**
     * full constructor
     */
    public TradeInfo(TradeInfoId id) {
        this.id = id;
    }

	// Property accessors
	@EmbeddedId
	@AttributeOverrides({
			@AttributeOverride(name = "regNo", column = @Column(name = "regNo", length = 20)),
			@AttributeOverride(name = "catId", column = @Column(name = "catId", length = 20)),
			@AttributeOverride(name = "applyDate", column = @Column(name = "applyDate", length = 20)),
			@AttributeOverride(name = "applierCn", column = @Column(name = "applierCN")),
			@AttributeOverride(name = "applierAddrCn", column = @Column(name = "applierAddrCN")),
			@AttributeOverride(name = "applierEn", column = @Column(name = "applierEN")),
			@AttributeOverride(name = "applierAddrEn", column = @Column(name = "applierAddrEN")),
			@AttributeOverride(name = "tradeImgUrl", column = @Column(name = "tradeImgUrl")),
			@AttributeOverride(name = "tradeImgThumbUrl", column = @Column(name = "tradeImgThumbUrl")),
			@AttributeOverride(name = "imgUrl", column = @Column(name = "imgUrl")),
			@AttributeOverride(name = "imgThumbUrl", column = @Column(name = "imgThumbUrl")),
			@AttributeOverride(name = "serviceList", column = @Column(name = "serviceList")),
			@AttributeOverride(name = "similarGroup", column = @Column(name = "similarGroup")),
			@AttributeOverride(name = "csggId", column = @Column(name = "csggId")),
			@AttributeOverride(name = "zcggId", column = @Column(name = "zcggId")),
			@AttributeOverride(name = "csggDate", column = @Column(name = "csggDate")),
			@AttributeOverride(name = "zcggDate", column = @Column(name = "zcggDate")),
			@AttributeOverride(name = "validYear", column = @Column(name = "validYear")),
			@AttributeOverride(name = "hqzdDate", column = @Column(name = "hqzdDate")),
			@AttributeOverride(name = "gjczDate", column = @Column(name = "gjczDate")),
			@AttributeOverride(name = "priorityDate", column = @Column(name = "priorityDate")),
			@AttributeOverride(name = "agentName", column = @Column(name = "agentName")),
			@AttributeOverride(name = "specifiedColor", column = @Column(name = "specifiedColor")),
			@AttributeOverride(name = "tradeType", column = @Column(name = "tradeType")),
			@AttributeOverride(name = "isShare", column = @Column(name = "isShare")),
			@AttributeOverride(name = "remark", column = @Column(name = "remark")),
			@AttributeOverride(name = "tradeflow", column = @Column(name = "tradeflow")) })
	public TradeInfoId getId() {
		return this.id;
	}

	public void setId(TradeInfoId id) {
		this.id = id;
	}

}
 
 

上面四个类是工具自动生成的。

下面做一个用根据e_id查询,带分页,按trade_id排序

  /**
     * 获取所有的对象
     * @return
     */
public List<TradeList> getAll() {
        return this.tradeListDao.loadAll();
    }

    public List<TradeList> getTradeList(PageBean pageBean, Integer eId) {
        HibernateSearchCondition hsc = new HibernateSearchCondition();
        hsc.addCriterion(Restrictions.eq(TradeList.FIELD_EID, eId));
        // 按tradeId排序
        hsc.addDESC(TradeList.FIELD_TRADEID);  //降序
        return getTradeListByPage(pageBean, hsc);
    }

 上面这个查询条件就相当于

Criteria c = this.getSession().createCriteria(TradeList.class)
                    .add(Restrictions.eq(TradeList.FIELD_EID, eId))
                    .addOrder(Order.desc(TradeList.FIELD_TRADEID));

条件就通过id.XXX(字段)来描述就可以了,其他的与有主键的相同

但是使用联合主键注意点    操作的时候自己注意点

建议:如非必要,不要使用联合主键,表中没有主键,但是在实体类中可以给某个字段建立主键关联,就像表中有主键一样就行了,但是必须保证作为主键的那个字段没有重复的值。

对视图的映射一般都没有主键,如果使用工具自动映射的话就会出现联合主键的,所以在映射的实体类中,我们可以把某个不能没有重复的值的字段当做主键来映射

猜你喜欢

转载自yingruochen.iteye.com/blog/1604345
今日推荐