博客
关于我
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启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基础
查看>>
mysql基础---mysql查询机制
查看>>
MySQL基础5
查看>>
MySQL基础day07_mysql集群实例-MySQL 5.6
查看>>