博客
关于我
JPA 常用注解(工作中遇到的)记录
阅读量:644 次
发布时间:2019-03-14

本文共 2329 字,大约阅读时间需要 7 分钟。

@Inheritance(strategy=InheritanceType.JOINED)

父类用于存储子类和父类的公共字段,子类与父类之间存在一一对应关系,子类的id字段通常会参考父类的id字段。

@MappedSuperclass

@MappedSuperclass注解只能标准在类上(@Target({java.lang.annotation.ElementType.TYPE}))。

标注为@MappedSuperclass的类并不是完整的实体类,它不会映射到数据库表,但其属性会映射到其子类的数据库字段中。

标注为@MappedSuperclass的类不能再标注@Entity或@Table注解,也无需实现序列化接口。

@PreUpdate

@PreUpdate注解用于为相应的生命周期事件指定回调方法,可应用于实体类、映射超类或回调监听器类的方法。

示例:每次更新时自动更新更新时间字段。

/**	 * 修改日期	 */	protected Date updateDate;		@Column(name = "update_date")	public Date getUpdateDate() {		return updateDate;	}		public void setUpdateDate(Date updateDate) {		this.updateDate = updateDate;	}		    @PreUpdate    public void preUpdate() {    	this.setUpdateDate(new Date());    }

@PrePersist

@PrePersist注解可以用于在使用JPA时记录一些业务无关的字段,如创建日期和最后更新时间等。

示例:自动更新创建时间和更新时间。

/**	 * 创建日期	 */	protected Date createDate;		/**	 * 修改日期	 */	protected Date updateDate;		@Column(name = "create_date", updatable = false)	public Date getCreateDate() {		return createDate;	}		public void setCreateDate(Date createDate) {		this.createDate = createDate;	}			@Column(name = "update_date")	public Date getUpdateDate() {		return updateDate;	}		public void setUpdateDate(Date updateDate) {		this.updateDate = updateDate;	}		//自动更新创建时间和更新时间	@PrePersist    public void prePersist() {		if(this.createDate == null){			this.setCreateDate(new Date());		}		this.setUpdateDate(new Date());    }

@DynamicUpdate

当MySQL字段类型为timestamp时,建议在实体类上加上@DynamicUpdate注解以确保实体类在更新时会更新更新时间字段。

@GenericGenerator 自定义生成策略

通过@GenericGenerator注解可以定义自定义的生成策略。

@Id    @GeneratedValue(generator = "fsId")    @GenericGenerator(name = "fsId", strategy = "com.fs.AssignedGUIDGenerator")
package com.fs; import org.hibernate.HibernateException;import org.hibernate.engine.spi.SharedSessionContractImplementor;import org.hibernate.id.IdentifierGenerator;import org.hibernate.id.UUIDHexGenerator; import java.io.Serializable; public class AssignedGUIDGenerator extends UUIDHexGenerator implements IdentifierGenerator{     @Override    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {        Serializable id = session.getEntityPersister(ENTITY_NAME, object).getIdentifier(object, session);        if (id==null || id.toString().equals("")) {            id = super.generate(session,object);        }        return id;    }}

转载地址:http://xmulz.baihongyu.com/

你可能感兴趣的文章
Python WebDriver如何打印整个页面源(html)
查看>>
Python WebSocket自动化测试:构建高效接口测试框架
查看>>
Python Web开发
查看>>
Python 优化提速的 8 个小技巧
查看>>
Python 余弦相似度与皮尔逊相关系数 计算
查看>>
python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
查看>>
python 使用filetype校验文件
查看>>
Python 使用flush函数将缓冲区数据立即写磁盘
查看>>
python 使用in判断不准确,in不好使
查看>>
Python 使用pandas 进行查询和统计详解
查看>>
Redis 配置文件redis.conf详细解释
查看>>
python网络爬虫(2)——scrapy框架的基础使用
查看>>
python 倒计时 9,8,7,。。。。。。0
查看>>
Python 内嵌函数:它们有什么用处?
查看>>
Python 基础语法:基本数据类型(集合)
查看>>
python 复习计划
查看>>
Python 多处理 apply_async 永远不会在 Windows 7 上返回结果
查看>>
Python 多处理 Numpy 随机
查看>>
Python 多处理 >= 125 列表永远不会完成
查看>>
Python 多处理:在第一个子错误时中止映射
查看>>