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

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

@Inheritance(strategy=InheritanceType.JOINED)

父类存放子类和父类的公共字段, 父类和子类中存在一一对应的关系,子类的id参考父类的id

@MappedSuperclass

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

2.标注为@MappedSuperclass的类将不是一个完整的实体类,他将不会映射到数据库表,但是他的属性都将映射到其子类的数据库字段中。

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

@PreUpdate

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

举个例子每次更新时自动更新更新时间字段

/**	 * 修改日期	 */	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

可以用来在使用jpa的时记录一些业务无关的字段,比如最后更新时间等等。 生命周期方法注解(delete没有生命周期事件)

/**	 * 创建日期	 */	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时必须在实体类上加上该注解实体类才会在更新的时候修改更新时间

@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/

你可能感兴趣的文章
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>