Eri*_* B. 14 java audit spring jpa spring-data
我正在尝试让Spring Data Auditing在我的Spring 3.2.8/Spring Data 1.5/Hibernate 4项目中运行.
根据Spring Data Auditing文档,我已经@CreatedBy
为我的实体添加了等等注释,由AuditorAware
实现创建,并在我的JavaConfig中实例化.然而,似乎永远不会开火.
我发现文档有点令人困惑.似乎JavaConfig条目替换了xml条目,但我不确定.
orm.xml
我的申请目前没有任何文件.说实话,我甚至不确定在何处/如何配置它,或者为什么我需要它.我的所有实体都在使用注释.我尝试将@EntityListeners(AuditingEntityListener.class)添加到实体,但这没有帮助.
我的当前实体管理器是在没有persistence.xml文件的情况下定义的:
<!-- entity manager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="packagesToScan" value="com.ia.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.query.substitutions">true '1', false '0'</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
JavaConfig:
@Configuration
@EnableJpaAuditing
public class AuditConfig {
@Bean
public AuditorAware<User> auditorProvider(){
return new SpringSecurityAuditorAware();
}
}
Run Code Online (Sandbox Code Playgroud)
实体:
@EntityListeners({AuditingEntityListener.class})
@Entity
public class User
{
@TableGenerator(name="UUIDGenerator", pkColumnValue="user_id", table="uuid_generator", allocationSize=1)
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="UUIDGenerator")
@Column(name="id")
private Long id;
@NotNull
private String username;
@CreatedDate
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name="created_date", nullable=false)
private Date createdDate;
@LastModifiedDate
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name="last_modified_date", nullable=false)
private Date lastModifiedDate;
@CreatedBy
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="created_by")
private User createdBy;
@LastModifiedBy
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="last_modified_by")
private User lastModifiedBy;
private String password;
private Boolean enabled;
...
}
Run Code Online (Sandbox Code Playgroud)
我在SpringSecurityAuditorAware
课堂上放了一个断点,但它永远不会受到打击.
我还需要一个orm.xml文件吗?从EntityManager引用的方式/位置是什么?
从JPA 2.0开始,如果没有XML文件(orm.xml
),就无法定义这样的实体监听器.
可以通过XML描述符指定应用于持久性单元中所有实体的默认实体侦听器 - 实体侦听器.(第93页)
如果项目中的所有实体都扩展了一个AbstractAuditable
超类,那么你就可以穿上 @EntityListeners({AuditingEntityListener.class})
它AbstractAuditable
.附加到实体类的侦听器由其子类继承.
继承层次结构中的多个实体类和映射的超类可以直接在类上定义侦听器类和/或生命周期回调方法.(第93页)
请注意,子类可以使用@ExcludeSuperclassListeners
注释显式排除继承的侦听器.
我想引用的规范中有一个最后一个有趣的脚注:
可以通过在EntityListeners批注或XML实体监听器元素中明确列出它们,将排除的侦听器重新引入实体类.(脚注[45] p.97)
以下是一些用于说明解决方法的代码:
AbstractAuditableEntity.java
import java.util.Date;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class}) // AuditingEntityListener will also audit any subclasses of AbstractAuditable...
public abstract class AbstractAuditableEntity {
@Id
@GeneratedValue
private Long id;
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
}
Run Code Online (Sandbox Code Playgroud)
MyEntity.java
@Entity
public abstract class MyEntity extends AbstractAuditableEntity {
}
Run Code Online (Sandbox Code Playgroud)
我认为Auditable
可以使用界面(@EntityListeners
可以出现在界面上)而不是AbstractAuditable
类,但我没有尝试...
参考:JSR-000317 Java Persistence 2.0 - 最终版本
使用Stephan的答案,/sf/answers/1836805421/,
我使用自定义监听器工作了.
@Configurable
public class TimestampedEntityAuditListener {
@PrePersist
public void touchForCreate(AbstractTimestampedEntity target) {
Date now = new Date();
target.setCreated(now);
target.setUpdated(now);
}
@PreUpdate
public void touchForUpdate(AbstractTimestampedEntity target) {
target.setUpdated(new Date());
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的基类中引用它:
@MappedSuperclass
@EntityListeners({TimestampedEntityAuditListener.class})
public abstract class AbstractTimestampedEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
private Date updated;
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
FWIW,我在spring-boot项目中使用它,没有orm.xml文件.