今天研究Open Jpa的文档时发现 它的 @PrePersist @PostPersist @PostLoan这些Callback 方法 非常有用,让我们可以在entity persist 或者 load 前后做一些我们的处理,比如它解决常见的boolean field 不能跟DB 里field Mapping 的问题(Oracle and DB2 没有相应的boolean field Type)。
注意因为displayFlag是一个页面显示字段,我们是用deleteFlag来跟db里的DELETE_FLAG来mapping ,所以我们要给displayFlag加上@Transient声明,不让它成为一个可persist的field,要不然运行时会抛exception.
Example Code 如下
@Entity
@NamedQueries({
@NamedQuery(name="magsOverPrice",
query="SELECT x FROM Magazine x WHERE x.price > ?1"),
@NamedQuery(name="magsByTitle",
query="SELECT x FROM Magazine x WHERE x.title = :titleParam")
})
@EntityListeners({MagazineListener.class})
@IdClass(Magazine.MagazineId.class)
@Table(name="MAGAZINE")
public class Magazine {
@Id private String isbn;
@Id private String title;
@Column(name="PRICE")
private BigDecimal price;
@Column(name="SOLDCOPY")
private int copySold;
@Column(name="VERS")
private int version;
@Column(name="DELETE_FLG")
private String deleteFlag;
@Transient
private boolean displayFlag;
public static class MagazineId {
private String isbn;
private String title;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final MagazineId other = (MagazineId) obj;
if (isbn == null) {
if (other.isbn != null)
return false;
} else if (!isbn.equals(other.isbn))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
public MagazineId() {
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
@PrePersist
private void covertDisplayToDB() {
if (this.displayFlag) {
this.deleteFlag = "Y";
}else {
this.deleteFlag = "N";
}
}
@PostLoad
private void covertDBValueToDisplay() {
if (this.deleteFlag != null && !this.deleteFlag.equals("") && this.deleteFlag.equals("Y")) {
this.displayFlag = true;
}else {
this.displayFlag = false;
}
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getCopySold() {
return copySold;
}
public void setCopySold(int copySold) {
this.copySold = copySold;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public Magazine() {
super();
}
public String getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(String deleteFlag) {
this.deleteFlag = deleteFlag;
}
public boolean isDisplayFlag() {
return displayFlag;
}
public void setDisplayFlag(boolean displayFlag) {
this.displayFlag = displayFlag;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Magazine other = (Magazine) obj;
if (isbn == null) {
if (other.isbn != null)
return false;
} else if (!isbn.equals(other.isbn))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
这个class 里的covertDisplayToDB 方法 用来在Magazine persist 前把页面displayFlag(boolean) 转换成DB存储的field类型 varchar.
covertDBValueToDisplay 方法用来把从DB抓出来的varchar类型的deleteFlag 转换成页面显示的dsiplayFlag.
当然我们也可以给这个entity class加上一个listener,然后把所有的Annotation的方法放在listener里。
Example Code 如下 首先给entity加上EntityListener Annotation @EntityListeners({MagazineListener.class}) 然后把我们的prePersist 跟PostLoad 的Annotation 方法移到listener中
public class MagazineListener {
private static final Logger log = Logger.getLogger(MagazineListener.class);
@PrePersist
private void covertDisplayToDB(Object persistObject) {
Magazine magazine = (Magazine) persistObject;
if (magazine.isDisplayFlag()) {
magazine.setDeleteFlag("Y");
}else {
magazine.setDeleteFlag("N");
}
}
@PostLoad
private void covertDBValueToDisplay(Object persistObject) {
Magazine magazine = (Magazine) persistObject;
if (magazine != null && magazine.getDeleteFlag() != null && !magazine.getDeleteFlag().equals("") && magazine.getDeleteFlag().equals("Y")) {
magazine.setDisplayFlag(true);
}else {
magazine.setDisplayFlag(false);
}
}
}
分享到:
相关推荐
用 OpenJPA 开发企业应用时,保持数据库和对象视图的一致性是非常重要的工作,OpenJPA 支持三种模式处理数据库和对象视图的一致性:正向映射(Forward Mapping)、反向映射(Reverse Mapping)、中间匹配(Meet-in-...
NULL 博文链接:https://lushuifa.iteye.com/blog/1286043
Java objects to a relational database. Even though proprietary mapping products like Hibernate and TopLink still exist, they are now focused on providing their functionality through the JPA API, ...
Invasive Computing for Mapping Parallel Programs to Many-Core Architectures (Computer Architecture and Design Methodologies) By 作者: Andreas Weichslgartner – Stefan Wildermann – Michael Glaß – J...
Java Language to IDL Mapping
Mapping Objects to Relational Databases O-R Mapping In Detail.doc
open_quadtree_mapping.bag的bag文件,用于算法测试的数据
Open Firmware Recommended Practice: Interrupt Mapping Version 0.9
Desktop GIS Mapping the Planet with Open Source Tools.pdf
Desktop GIS is a comprehensive survey of open source software for GIS users. Everyone from casual ...anyone interested in a hands-on approach to learning the latest in open source GIS technology.
Bayesian Disease Mapping - Hierarchical Modeling in Spatial Epidemiology
本文是介绍Spring-data-jpa的PPT的学习笔记,...https://www.w3cschool.cn/java/jpa-field-mapping.html 轻松愉快之玩转SpringData-慕课网 (免费视频) https://www.imooc.com/learn/821 优秀实践项目GitHub源码分享: ...
LOAM ,Lidar Odometry and mapping in real time;一个开源的3d slam建图方法,值得参考
Overview Integration Builder Mapping – Introduction
Mapping Language Models to Grounded Conceptual Spaces.pdf
Python Passive Network Mapping is ideally suited for use as a text in a variety of academic programs to expose and engage students in the art of passively mapping enterprise networks, with the added ...
Projective texture mapping refers both to the way texture coordinates are assigned to vertices, and the way they are computed during rasterization of primitives. We usually think of texture mapping as...
SQL to Mongo Mapping Chart,SQL与mongo 查询对应关系
使用Hibernate的JPA实例,包含jar文件
effective SNR mapping for modeling frame error rates in multiple-state channels 3GPP2