2017-01-19 15 views
1

私は春のブートを使用しているため、設定にxmlファイルを使用していません。 私は、MongoRepositoriesを使ってデータを保存しながら、createdDate、lastModifiedDateなどを保存するためにEnableMongoAuditingを行う必要があります。mongodbの保存用の春ブートでの監査createdDate、lastModifiedDate、createdBy、lastModifiedBy

私のモデルクラス

@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注釈を使用していました。 及びIは日時

<dependency> 
<groupId>joda-time</groupId> 
<artifactId>joda-time</artifactId> 
<version>2.9.7</version> 
</dependency> 

ばねデータのMongoDBも依存性で添加されるためjodatime依存関係を使用しています。

これは私のメインのアプリケーションクラス日付はデータベースに保存されて取得されていないとして、私はこのimpelmentationに間違っている

@SpringBootApplication 
    @EnableMongoAuditing 
    public class Application { 

    public static void main(String[] args) { 
    SpringApplication.run(Application.class, args); 
    } 

} 

のですか? また、私は@createdByを保存するためにAuditorAware beanを書く必要があることを知っていますが、今はcreatedByを保存しようとしています。

@EnableMongoAuditingはどこで使用する必要がありますか?

答えて

1

私のアプリケーションでは、Javaコードを使用して設定します。私はこの方法で@EnableMongAuditingを使用し、ZonedDateTimeの変換も作成します。

@Configuration 
@EnableMongoAuditing 
@EnableMongoRepositories(basePackages = { BASE_PACKAGE }) 
public class MongoConfiguration extends AbstractMongoConfiguration { 

    public static final String BASE_PACKAGE = "package.with.aggregates"; 

    @Value("${spring.data.mongodb.uri}") 
    private String mongoUri; 

    @Value("${spring.data.mongodb.database}") 
    private String databaseName; 

    @Override 
    protected String getDatabaseName() { 
     return databaseName; 
    } 

    @Override 
    public Mongo mongo() throws Exception { 
     return new MongoClient(new MongoClientURI(mongoUri)); 
    } 

    // Here you must add converters to Joda datetypes. In my solution is ZonedDateTime 
    @Override 
    public CustomConversions customConversions() { 
     List<Converter<?, ?>> converterList = new ArrayList<>(); 
     converterList.add(new DateToZonedDateTimeConverter()); 
     converterList.add(new ZonedDateTimeToDateConverter()); 
     return new CustomConversions(converterList); 
    } 

    @Override 
    protected String getMappingBasePackage() { 
     return BASE_PACKAGE; 
    } 
} 
+0

はどこMongoConfigurationクラスが使用されない(次の@Configuration注釈に)構成のどこにでも配置することができますか? –

+0

アノテーション@Configurationは、クラスがXMLと同様の設定であるが、Javaコードであることを示しています。多くのコンフィグレーションクラスを持つことができます。パッケージクラスにpackageScanを置くだけで、そこにコンフィグレーションクラスがあります。 – Cyva

+0

Springスキャンアプリにパッケージスキャンを追加する方法。 @SpringBootApplication(scanBasePackages = {"com.some.package"}) – Cyva

2

@EnableMongoAuditingは実際に

関連する問題