我想让Spring Data审计在我的Spring 3.2.8 / Spring Data 1.5 / Hibernate 4项目中工作。
根据Spring Data审计文档,我已经向我的实体添加了@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>
JavaConfig:
@Configuration
@EnableJpaAuditing
public class AuditConfig {
@Bean
public AuditorAware<User> auditorProvider(){
return new SpringSecurityAuditorAware();
}
}
实体:
@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;
...
}
我在SpringSecurityAuditorAware
类中设置了一个断点,但它从未被触发。
我是否仍需要一个orm.xml文件?这个文件从EntityManager的哪里引用?
@EntityListeners()
but when jRebel was loading the classes, it was using the classpath pointing to the original entities and not the augmented classes. I had to remove the rebel.xml file that the jRebel maven plugin generated, and everything seems to be working properly now.翻译:事实证明这是一个 jRebel 的问题。我一直在使用 aspectJ 将@EntityListeners()
编译时织入,但是当 jRebel 加载类时,它使用的是指向原始实体而不是增强类的类路径。我不得不删除 jRebel Maven 插件生成的 rebel.xml 文件,现在一切都正常了。 - Eric B.Jrebel
并йҒҮеҲ°зӣёеҗҢзҡ„й—®йўҳгҖӮжңүд»Җд№ҲеҠһжі•и®©jrebel
дёҺSpring Data Auditing
е…је®№еҗ—пјҹ - oak