在引入了或声明了相关表或类后就能够使用它下面的方法,例如:
(1)@Table(name = "bsp_corp_spl", catalog = "bspdb")
(2)private BspCorpSplId id;
private BspCorp bspCorp;
因为要在dao类中使用HQL语句实现SELECT * FROM Table1 t1 WHERE t1.id.name=? AND t1.id.email=?
选出复合主键类的所有记录;所以要在类中实现get整体的方法,此时使用了复合主键类;
复合主键类给出实现equal,hashcode方法。
一、参照实体之间@OneToMany
1、在被参照端的POJO类中注释:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="bspCorp")//单向的声明被参照类 ,联级在被参照端声明?
public Set<BspMySplContact> getBspMySplContacts() {
return this.bspMySplContacts;
}
2、参照端的注释:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CORP_ID", nullable = false, insertable = false, updatable = false)//使用被参照类主键作为外键。
public BspCorp getBspCorp() {
return this.bspCorp;
}
@AttributeOverrides由多个@AttributeOverride注释组成,每个@AttributeOverride表示属性的映射,它的定义如以下所示:
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface AttributeOverride {
String name();
Column column();
}
二、复合主键实体类调用嵌套类:
在使用@AttributeOverride注释应注意以下几方面的问题:
1、了解嵌入类字段的含义
name属性表示被嵌入类中的属性名称。
column属性表示,对应所要嵌入的实体类的表,其中的name 为列字段的名称。
例如将tb_customer表中的customer_zip字段映射为Address嵌入类中的属性zip。代码如下所示:
@AttributeOverride(name = "zip",
column = @Column(name = "customer_zip")
)
2、嵌入类可以多次被引用
使用嵌入式类的好处是:多个实体中都可以共享一个嵌入式类,方便了对实体的操作。
例如现在ContactEO也嵌入Address类,就很方便的映射为以下所示:
public class CustomerEO implements Serializable {
private Integer id;
private String name;
private String nickname;
……getter和setter方法省略
private Address address;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name = "zip", column = @Column(name = "contact_zip")),
@AttributeOverride(name = "line1", column = @Column(name = "contact_line1")),
})
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
三、实现复合主键嵌套类中的定义:
package com.bsp.entity;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
/**
* BspCorpSplId entity.
* @author Java Web2.0项目开发进阶宝典
*/
@Embeddable
public class BspCorpSplId implements java.io.Serializable {
// 字段属性
private String corpId;
private String spl;
/**默认构造器 */
public BspCorpSplId() {
}
@NotNull(message = "编号不能为空")
@Length(min = 1, max = 5, message = "编号长度应在1至5之间")
@Column(name = "CORP_ID", nullable = false, length = 5)
//因为在调用了嵌套类的实体类中已经将其初始化,所以此处的字段为已知
public String getCorpId() {
return this.corpId;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
@Column(name = "SPL", nullable = false, length = 5)
public String getSpl() {
return this.spl;
}
public void setSpl(String spl) {
this.spl = spl;
}
public boolean equals( Object other) {//要实现的两个方法
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof BspCorpSplId))
return false;
BspCorpSplId castOther = (BspCorpSplId) other;
return ((this.getCorpId() == castOther.getCorpId()) || (this
.getCorpId() != null
&& castOther.getCorpId() != null && this.getCorpId().equals(
castOther.getCorpId())))
&& ((this.getSpl() == castOther.getSpl()) || (this.getSpl() != null
&& castOther.getSpl() != null && this.getSpl().equals(
castOther.getSpl())));
}
public int hashCode() {
int result = 17;
result = 37 * result
+ (getCorpId() == null ? 0 : this.getCorpId().hashCode());
result = 37 * result
+ (getSpl() == null ? 0 : this.getSpl().hashCode());
return result;
}
}
Hibernate注释中的参照完整性和复合主键类解析
最新推荐文章于 2022-10-20 01:19:25 发布