博客
关于我
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基础之面向对象2(封装)
查看>>
Python基础之给函数增加元信息
查看>>
python基础之字符编码
查看>>
python基础之多线程与多进程(二)
查看>>
Python基础——MySQL数据库基本操作
查看>>
Python基础_列表 list
查看>>
python基础---->常用模块
查看>>
Python基础(2):控制结构
查看>>
Python基础 | 关于“循环”那些事
查看>>
python系列【仅供参考】:python 远程rdp
查看>>
python基于flask搭建http服务(四)—— Docker容器化部署
查看>>
Python块键盘/鼠标输入
查看>>
python在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>...
查看>>
python在gpu上运行_【python】python开启GPU加速
查看>>
Python在def函数中使用for循环
查看>>
Python在def函数中使用for循环
查看>>
Python在Conda环境中,但在Windows虚拟环境中没有激活
查看>>
python系列【仅供参考】:python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法
查看>>
python图像条状状噪声,使用PYTHON PIL从验证码图像中删除背景嘈杂的线条
查看>>
Python图像处理:从内存加载jpeg
查看>>