`
y806839048
  • 浏览: 1106499 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

jpa嵌入式复合主键

    博客分类:
  • jpa
 
阅读更多

jpa嵌入式复合主键:

 

JPA生成的实体要求有主键,如果数据库没有的话可以自己构造一个主键,只是为了符合jpa规范,xml中还是以数据库为主

用于构造主键的实体的那个类需要无参数构造函数,重写equals,hashcode(构造主键的实体,实现序列化

 

 

(1)通过@EmbeddedId注释标注实体中的嵌入式主键

 

注意:

1.必须要实现serializable接口

 

2.需要有无参的构造函数

 

3.@Embeddable注释,表示此类可以被插入某个entity中

 

4.两个class和Column的列名都必需与表里的列名一致,建立sql时,会自动选择TestPK里的列名 

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;

}

 

 

}

 

 

 

分享到:
评论

相关推荐

    hibernate3 注释生成复合主键或者嵌入式主键的方法及实例.doc

    这篇文档将介绍如何使用Hibernate注解来生成复合主键或嵌入式主键。 复合主键(Composite Key)是指由两个或更多个列共同构成的唯一标识,而嵌入式主键(Embedded Key)则是将主键字段嵌入到实体类内部。在不使用...

    JPA_5_联合主键

    然后在实体类上使用`@IdClass`指定这个复合主键类: ```java @Entity @IdClass(CompositeKeyClass.class) public class MyEntity { private String field1; private int field2; // getters and setters } ``` ...

    JPA 标注 JPA标签手册

    - `@EmbeddedId`:当实体使用复合主键时,标识复合主键类。 - `@Inheritance`:控制继承策略,如单一表继承或多表继承。 2. **属性映射(Attribute Mapping)**: - `@Basic`:用于基本类型的字段,表明其为简单...

    jpa2.0jar包

    6. **复合主键(Composite Key)**: 支持由多个字段组成的复合主键。 7. **嵌入式对象(Embedded Objects)**: 可以将一个对象嵌入到另一个对象中,作为一个整体处理。 8. **增强的注解支持**: 提供了更多的注解,...

    JPA标签(注解)详细说明

    - `@PrimaryKeyJoinColumn`, `@PrimaryKeyJoinColumns`:用于指定复合主键的外键列。 - `@JoinTable`: 用于多对多关系的中间表配置。 - `@UniqueConstraint`: 定义表中的唯一约束。 3. **身份(主键)**: - `@...

    JPA批注参考(主要介绍JPA中的相关注解)

    - **`@EmbeddedId`**:用于嵌入式的复合主键。 - **`@GeneratedValue`**:用于指定主键生成策略,如 AUTO、SEQUENCE 等。 - **`@SequenceGenerator`** 和 **`@TableGenerator`**:用于定义生成主键值的序列或表。 #...

    JPA学习总结(五)--JPACompositePK联合主键

    这篇博客文章将深入探讨JPA中如何处理复合主键,以及它们在实际开发中的应用。 ### 一、JPA 联合主键(Composite Key) 联合主键(Composite Key)是指由两个或更多个属性组成的主键,这些属性共同决定了实体的...

    JPA全面讲解

    如果实体使用复合主键,可以使用`@EmbeddedId`或`@IdClass`。 3. 直接映射:`@Basic`批注用于基本类型的映射,`@Temporal`用于日期时间类型,`@Lob`用于大对象(BLOB/CLOB),`@Transient`则标记不需持久化的字段。...

    第25章_JPA概述.

    29.2 复合主键 29.3嵌入式主键 29.4 一对一关系的配置和使用 29.5 多对一和一对多关系的配置和使用 29.6 多对多的关系 29.7 把查询的多个值封装成对象 29.8 批量更新和删除 29.9 使用存储过程 29.10 实体生命周期回...

    JPA注解参考文档.txt

    - **@IdClass**:用于复合主键的情况,指定复合主键类。 - **@EmbeddedId**:用于嵌入式主键的情况。 - **@GeneratedValue**:用于指定主键的生成策略。 - **@SequenceGenerator**:用于指定序列生成器。 - **@...

    openJPA官方手册

    - **`@IdClass`**:介绍如何使用复合主键(Composite Key)。 - **`@MappedSuperclass`**:如何将通用属性抽取到基类中。 - **`@Embeddable`**:解释嵌入式对象的使用场景和定义方式。 - **实体监听器**:如何...

    JPA 批注参考Java JPA

    - `@IdClass`:用于复合主键,指定一个包含主键字段的类。 - `@EmbeddedId`:将一个嵌入式类作为主键。 - `@GeneratedValue`:定义主键生成策略,如自动递增、序列等。 - `@SequenceGenerator` 和 `@...

    SUN公司的JPA课件

    - **定义**:当一个实体需要多个字段共同作为主键时,可以使用复合主键。 - **实现方式**:可以通过`@Embeddable`和`@EmbeddedId`注解实现。 综上所述,SUN公司的JPA课件全面介绍了JPA的相关概念和技术要点,对于...

    Toplink_JPA注解参考

    对于复合主键,可以使用@EmbeddedId注解配合一个嵌入式类来表示。例如: ```java @Embeddable public class OrderId { private String orderId; private int customerId; } @Entity public class Order { @...

    JPA 批注参考

    - **@EmbeddedId**: 用于标记嵌入式复合主键。 - **@GeneratedValue**: 用于指定主键生成策略。 - **@SequenceGenerator**: 用于定义序列生成器。 - **@TableGenerator**: 用于定义基于表的主键生成器。 **4. 直接...

    jpa用户使用指南 英文版

    - **身份类:** 使用复合主键的情况。 - **身份层次结构:** 在具有共同父类的实体中定义主键。 ##### 4.3 生命周期回调 - **回调方法:** 如 prePersist、postPersist 等。 - **使用回调方法:** 自定义实体的...

    JPA-全面讲解.docx

    2. **身份管理**:JPA要求每个实体有一个主键,可以使用`@Id`批注指定主键字段,或者使用`@EmbeddedId`或`@IdClass`来定义复合主键。`@GeneratedValue`批注用于控制主键的生成策略,比如自动增长。 3. **直接映射**...

    JPA 批注总结参考

    - `@IdClass`:用于复合主键,定义一个类来存储多个主键字段。 - `@EmbeddedId`:将一个嵌入式ID类作为主键。 - `@GeneratedValue`:控制主键的生成策略,如自动增量或序列。 - `@SequenceGenerator` 和 `@...

Global site tag (gtag.js) - Google Analytics