jpa embedded composite primary key

jpa embedded composite primary key:

 

The entity generated by JPA requires a primary key. If the database does not have one, you can construct a primary key yourself, just to comply with the jpa specification, the xml is still dominated by the database

The class used to construct the entity of the primary key needs a parameterless constructor, override equals, hashcode ( the entity that constructs the primary key ) , and implement serialization

 

 

(1) Annotate the embedded primary key in the entity through the @EmbeddedId annotation

 

Notice:

1. You must implement the serializable interface

 

2. Requires a parameterless constructor

 

3. @Embeddable annotation, indicating that this class can be inserted into an entity

 

4. The column names of the two classes and Columns must be consistent with the column names in the table. When creating sql, the column names in TestPK will be automatically selected 

http://vaadin.iteye.com/blog/1317227

 

<resultMap type="com.esteel.web.beanvo.TbCusTradeWareBeanVo"

id="tbCusTradeWareMap">

<!-- <result property="id" column="id" typeHandler="com.esteel.web.entity.TbCusTradeWareBeanPK" /> -->不需要写

<result property="addTime" column="ADD_TIME" />

<result property="cusTradeKind" column="CUS_TRADE_KIND" />

<result property="currencyType" column="CURRENCY_TYPE" />

<result property="cusUserKey" column="CUS_USER_KEY" />/////////////这两个主键一定要写在结果集中否者主键类接不到返回值

<result property="customerKey" column="CUSTOMER_KEY" />

<result property="remark" column="remark" />

<result property="tradeType" column="TRADE_TYPE" />

<result property="warekindKey" column="WAREKIND_KEY" />

<result property="currencyName" column="WAREKIND_NAME" />

</resultMap>

 

 结果集中的主键是以一个对象嵌入返回(类似一对一关联)

 

 

 

 

 

 

 

 

 

package com.esteel.web.entity;

 

import java.io.Serializable;

import javax.persistence.*;

 

import oracle.net.aso.i;

 

import java.math.BigDecimal;

import java.util.Date;

 

 

/**

 * The persistent class for the TB_CUS_TRADE_WARE database table.

 * 

 */

@Entity

@Table(name="TB_CUS_TRADE_WARE")

@NamedQuery(name="TbCusTradeWareBean.findAll", query="SELECT t FROM TbCusTradeWareBean t")

public class TbCusTradeWareBean implements Serializable {

private static final long serialVersionUID = 1L;

 

@EmbeddedId

// @AttributeOverrides( {

//

//       @AttributeOverride(name = "cusUserKey", column = @Column(name = "CUS_USER_KEY")),

//

//       @AttributeOverride(name = "customerKey", column = @Column(name = "CUSTOMER_KEY")) })主键类中有写就不要

private TbCusTradeWareBeanPK id =new TbCusTradeWareBeanPK();//////////给主键赋值一定要new 符合主键

 

 

public TbCusTradeWareBeanPK getId() {

 

 return id;

 

}

 

@Temporal(TemporalType.DATE)

@Column(name="ADD_TIME")

private Date addTime;

 

@Column(name="CURRENCY_TYPE")

private String currencyType;

 

 

 

private String remark;

 

@Column(name="TRADE_TYPE")

private String tradeType;

 

@Column(name="WAREKIND_KEY")

private BigDecimal warekindKey;

 

public TbCusTradeWareBean() {

}

 

public Date getAddTime() {

return this.addTime;

}

 

public void setAddTime(Date addTime) {

this.addTime = addTime;

}

 

public String getCurrencyType() {

return this.currencyType;

}

 

public void setCurrencyType(String currencyType) {

this.currencyType = currencyType;

}

 

 

 

public void setId(TbCusTradeWareBeanPK id) {

this.id = id;

}

 

public String getRemark() {

return this.remark;

}

 

public void setRemark(String remark) {

this.remark = remark;

}

 

public String getTradeType() {

return this.tradeType;

}

 

public void setTradeType(String tradeType) {

this.tradeType = tradeType;

}

 

public BigDecimal getWarekindKey() {

return this.warekindKey;

}

 

public void setWarekindKey(BigDecimal warekindKey) {

this.warekindKey = warekindKey;

}

//@Column(name="CUS_USER_KEY")

public BigDecimal getCusUserKey() {

return id.getCusUserKey();

}

 

public void setCusUserKey(BigDecimal cusUserKey) {

id.setCusUserKey(cusUserKey);

}

//@Column(name="CUSTOMER_KEY")

public BigDecimal getCustomerKey() {

return id.getCustomerKey();

}

 

public void setCustomerKey(BigDecimal customerKey) {

id.setCustomerKey(customerKey); 

}

}

 

 

 

 

 

 

 

 

(2)作为嵌入式主键类,要满足以下几点要求。

 

1.必须实现Serializable接口、必须有默认的public无参数的构造方法、必须覆盖equals 和hashCode方法,这些要求与使用复合主键的要求相同。

 

2.将嵌入式主键类使用@Embeddable标注,表示这个是一个嵌入式类。

 

package com.esteel.web.entity;

 

import java.io.Serializable;

import java.math.BigDecimal;

 

import javax.persistence.Column;

import javax.persistence.Embeddable;

 

 

/**

 * The persistent class for the TB_CUS_TRADE_WARE database table.

 * 

 */

@Embeddable

public class TbCusTradeWareBeanPK implements Serializable {

private static final long serialVersionUID = 1L;

 

@Column(name="CUS_USER_KEY")

private BigDecimal cusUserKey;

 

@Column(name="CUSTOMER_KEY")

private BigDecimal customerKey;

 

 

 

public TbCusTradeWareBeanPK() {

}

 

public TbCusTradeWareBeanPK(BigDecimal cusUserKey,BigDecimal customerKey) {

this.customerKey=cusUserKey;

this.cusUserKey=cusUserKey;

}

public BigDecimal getCusUserKey() {

return cusUserKey;

}

 

public void setCusUserKey(BigDecimal cusUserKey) {

this.cusUserKey = cusUserKey;

}

 

public BigDecimal getCustomerKey() {

return customerKey;

}

 

public void setCustomerKey(BigDecimal customerKey) {

this.customerKey = customerKey;

}

public boolean equals(Object other) {

if (this == other) {

return true;

}

if (!(other instanceof TbCusTradeWareBeanPK)) {

return false;

}

TbCusTradeWareBeanPK castOther = (TbCusTradeWareBeanPK)other;

return 

(this.cusUserKey == castOther.cusUserKey)

&& this.customerKey.equals(castOther.customerKey);

}

 

public int hashCode() {

final int prime = 31;

int hash = 17;

hash = this.cusUserKey.hashCode();

hash = hash * prime + this.customerKey.hashCode();

return hash;

}

 

 

}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326504017&siteId=291194637