Java Spring Data @CreatedDate 注释对我不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20483841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data @CreatedDate annotation doesn't work for me
提问by alicjab
I am working on project where I use Spring Data. I wanted to fill in creationTime
field using @CreatedDate
annotation instead using method with @PreUpdate
or @PrePersist
annotation (doing it this way it works perfectly). When I do it with @CreatedDate
it just leaves this field blank. I use postgresql database. Documentationis not very helpful.
我正在从事使用 Spring Data 的项目。我想creationTime
使用@CreatedDate
注释填充字段,而不是使用带有@PreUpdate
或@PrePersist
注释的方法(这样做可以完美地工作)。当我使用@CreatedDate
它时,只会将此字段留空。我使用 postgresql 数据库。文档不是很有帮助。
Do you have any idea how can I fix it? Thank you!
你知道我该如何解决吗?谢谢!
import org.springframework.data.annotation.CreatedDate;
@Entity
@Table(name = "software")
public class Software implements Serializable {
// ...
@Column(name = "creation_time")
@CreatedDate
private Date creationTime;
//...
}
My applicationContext
:
我的applicationContext
:
<jpa:repositories base-package="path.to.dao"/>
<context:component-scan base-package="path.to.dao"/>
<context:property-placeholder location="classpath:application.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="path.to.bean"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaAdapter"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
采纳答案by Tom Cross
I may have been in a similar situation where I wanted the Spring Data JPA @CreatedDate
annotation to work, but had no need for the user-level auditing that is otherwise described in their documentation.
我可能遇到过类似的情况,我希望 Spring Data JPA@CreatedDate
注释起作用,但不需要他们文档中另外描述的用户级审计。
To get the annotation-based auditing to work, I had to nonetheless add a class to my project that implemented org.springframework.data.domain.AuditorAware
. This is odd because you don't actually seem to use the value returned from the getCurrentAuditor()
method that you'll be implementing; mine just returns null
.
为了让基于注释的审计工作,我不得不向我的项目添加一个类,该类实现了org.springframework.data.domain.AuditorAware
. 这很奇怪,因为您实际上似乎并未使用从getCurrentAuditor()
您将要实现的方法返回的值;我的刚回来null
。
public class NullAuditorBean implements AuditorAware {
@Override
public Object getCurrentAuditor() {
return null;
}
}
I then needed to reference my "null object" AuditorAware
implementation in an entry in my applicationContext
to activate the JPA auditing. I had to make sure I did this before the line that specifies the jpa:repositories
. This looks something like:
然后我需要AuditorAware
在我的条目中引用我的“空对象”实现applicationContext
以激活 JPA 审计。我必须确保在指定jpa:repositories
. 这看起来像:
<bean id="auditorBean" class="your.package.subbed.here.NullAuditorBean"/>
<jpa:auditing auditor-aware-ref="auditorBean"/>
I also had to add an orm.xml
file, and needed to formally reference it as a property of my entityManagerFactory
bean, like so:
我还必须添加一个orm.xml
文件,并且需要将它正式引用为我的entityManagerFactory
bean的属性,如下所示:
<property name="mappingResources">
<value>META-INF/orm.xml</value>
</property>
Make sure this META-INF/orm.xml
entry is stored with your compile output (mine is in my WAR under WEB-INF/classes
.
确保META-INF/orm.xml
此条目与您的编译输出一起存储(我的在 WAR 下的WEB-INF/classes
.
That orm.xml
file, for the record, contained some boilerplate, which can be found in the answer to this related question.
orm.xml
作为记录,该文件包含一些样板文件,可以在此相关问题的答案中找到。
It was a fair amount of work when I got this working. You may prefer your previous working solution!
当我开始工作时,这是相当多的工作。您可能更喜欢以前的工作解决方案!
回答by Madbreaks
This question is quite old, but still relevant. For me the key was this, from the documentation
这个问题很老了,但仍然相关。对我来说,关键是这个,来自文档
Since Spring Data MongoDB 1.4 auditing can be enabled by annotating a configuration class with the @EnableMongoAuditing annotation.
由于 Spring Data MongoDB 1.4 审计可以通过使用 @EnableMongoAuditing 注释来注释配置类来启用。
For example:
例如:
@Configuration
@EnableMongoAuditing
class Config {
/**
* Optional, depending on your needs
*/
@Bean
public AuditorAware<AuditableUser> myAuditorProvider() {
return new AuditorAwareImpl();
}
}
Or, in XML:
或者,在 XML 中:
<mongo:auditing/>