我使用的是 Boot ,因此我没有使用任何xml文件进行配置。我必须做的是EnableMongoAuditing保存createdDate,lastModifiedDate等,同时使用MongoRepository保存数据。
我的模特班
@Component
@Document(collection = "CAPPING")
public class TemporaryCapping extends BaseEntity {
@Field("contract_id")
private BigInteger contractId;
@Field("period_id")
private BigInteger periodId;
@Field("user_id")
private BigInteger userId;
@Field("amount")
private Double amount;
@Field("type_of_capping")
private TypeOfCapping typeOfCapping;
public BigInteger getContractId() {
return contractId;
}
public void setContractId(BigInteger contractId) {
this.contractId = contractId;
}
public BigInteger getPeriodId() {
return periodId;
}
public void setPeriodId(BigInteger periodId) {
this.periodId = periodId;
}
public BigInteger getUserId() {
return userId;
}
public void setUserId(BigInteger userId) {
this.userId = userId;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
public TypeOfCapping getTypeOfCapping() {
return typeOfCapping;
}
public void setTypeOfCapping(TypeOfCapping typeOfCapping) {
this.typeOfCapping = typeOfCapping;
}
}
public class BaseEntity implements Serializable{
@Id
@Indexed(unique = true)
private BigInteger id;
@CreatedDate
private DateTime createdDate;
@Field("modified_date")
private BigInteger modifiedDate;
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public DateTime getCreatedDate() {
return createdDate;
}
public void setCreatedDate(DateTime createdDate) {
this.createdDate = createdDate;
}
public BigInteger getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(BigInteger modifiedDate) {
this.modifiedDate = modifiedDate;
}
字符串
我使用@CreateDate注解保存createDate。我使用了jodatime依赖于DateTime
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>
型
spring-data-mongodb也被添加到依赖项中。
这是我的主要应用类
@SpringBootApplication
@EnableMongoAuditing
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
型
我在这个推动错误的地方,因为日期是没有得到保存在数据库?我还知道,为了保存@createdBy,你需要编写AuditorAware bean,但现在我只是试图保存createdBy。
@EnableMongoAuditing应该在哪里使用?
3条答案
按热度按时间u2nhd7ah1#
在我的应用程序中,我通过Java代码进行配置。我以这种方式使用@EnableMongAuditing,并为ZonedDateTime创建转换。
字符串
shyt4zoc2#
@EnableMongoAuditing实际上可以放置在配置中的任何位置(在@Configuration annotation旁边)
oo7oh9g93#
简单的解决方案,如果
@CreatedDate or @LastModifiedDate
不像预期的那样工作(它对我来说根本不起作用)。1.为审计添加统一类,然后您将扩展到任何实体(文档):
字符串
1.添加配置,通知Spring auditable已启用
型
1.扩展我们的Audiable界面-成功,在创建/编辑时,您将在DB中获得预期的数据。
型
如果您使用Joda时间(或任何其他特定类型),则应添加额外的CustomConversions。