• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

3.1K
Vistas
mongodb auditing in spring boot for saving createdDate, lastModifiedDate, createdBy, lastModifiedBy

I am using spring boot, therefore I am not using any xml files for configurations. What I have to do is to EnableMongoAuditing for saving createdDate, lastModifiedDate etc while saving data using MongoRepositories.

My model class

@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;
}

I have used @CreateDate annotation for saving createDate. and I have used jodatime dependency for DateTime

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

spring-data-mongodb is also added in the dependencies.

This is my main application class

 @SpringBootApplication
    @EnableMongoAuditing
   public class Application {

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

} 

Where I am wrong in this impelmentation as the date is not getting saved in database? Also I know that for saving @createdBy you need to write AuditorAware bean but for now I am just trying to save createdBy.

Where should @EnableMongoAuditing be used?

about 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In my application I configure through Java code. I use @EnableMongAuditing this way and also create convertes for 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;
    }
}
about 3 years ago · Santiago Trujillo Denunciar

0

@EnableMongoAuditing can actually be placed anywhere in configurations (next to @Configuration annotation)

about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda