问题描述
我正在研究使用Spring Data的项目。我想使用 @CreatedDate
注释填写 creationTime
字段,而不是使用 @PreUpdate 或
@PrePersist
注释(以这种方式完美地运行)。当我使用 @CreatedDate
执行此操作时,它只会将此字段留空。我使用postgresql数据库。 不是很有帮助。
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. Documentation is not very helpful.
您有什么想法我该如何解决?谢谢!
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;
//...
}
我的
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>
推荐答案
我可能一直处于类似的情况,我希望Spring Data JPA
@CreatedDate
注释可以工作,但是不需要进行用户级审计否则在他们的文档中描述。
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.
为了让基于注释的审计工作,我不得不在我的项目中添加一个实现
org的类.springframework.data.domain.AuditorAware
。这很奇怪,因为您实际上似乎并没有使用您将要实现的 getCurrentAuditor()
方法返回的值;我只返回 null
。
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
.
public class NullAuditorBean implements AuditorAware {
@Override
public Object getCurrentAuditor() {
return null;
}
}
然后我需要引用我的空对象
AuditorAware
在我的 applicationContext
的条目中实现,以激活JPA审核。我必须确保在指定 jpa:repositories
的行之前执行此操作。这看起来像:
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:
<bean id="auditorBean" class="your.package.subbed.here.NullAuditorBean"/>
<jpa:auditing auditor-aware-ref="auditorBean"/>
我还必须添加
orm.xml
file,并且需要正式引用它作为我的 entityManagerFactory
bean的属性,如下所示:
I also had to add an
orm.xml
file, and needed to formally reference it as a property of my entityManagerFactory
bean, like so:
<property name="mappingResources">
<value>META-INF/orm.xml</value>
</property>
确保
META-INF / orm.xml
条目与您的编译输出一起存储(我的WAR在我的WAR下 WEB-INF / classes
。
Make sure this
META-INF/orm.xml
entry is stored with your compile output (mine is in my WAR under WEB-INF/classes
.
记录中的
orm.xml
文件包含一些样板文件,可以在。
That
orm.xml
file, for the record, contained some boilerplate, which can be found in the answer to this related question.
这是我开始工作时需要做相当多的工作。您可能更喜欢以前的工作解决方案!
It was a fair amount of work when I got this working. You may prefer your previous working solution!
这篇关于Spring Data @CreatedDate注释对我不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!