Spring Data MongoDB prevent persisting certain fields - mongodb

I am using Spring Data for MongoDB to persist my domain objects. I was wondering if there is a way (perhaps with an Annotation?) to prevent Spring Data from persisting certain fields into MongoDB?
Does someone know how to do that or do I have to write my own Mapper?
Thanks.

In this case use the #Transient annotation for the field you need to ignore.
Look more over here - Transient

In case you are looking for the actual package like I was, this one will work:
import org.springframework.data.annotation.Transient;
Which is from the Spring framework API documentation.
But this one, which is a JPA annotation, will not work for Spring Data's MongoDB:
import javax.persistence.Transient;
Which is part of the Java Persistence API.

Related

How to ignore #Indexed annotation for Spring Data Mongo

I have multiple projects that use the same class User which has the following annotation:
#Indexed(direction=IndexDirection.DESCENDING)
private Date created = null;
I don't want each project to generate a call to mongo to create the index as this causes issues (e.g. if I want to change the index).
Is there a way to ask Spring Data Mongo to ignore the #Indexed annotation via the configuration file (or other way)?
The upcoming Spring Data MongoDB 2.2 allows to turn off annotated index creation via MongoConfigurationSupport#autoIndexCreation or directly by calling mongoMappingContext.setAutoIndexCreation(false).
Please see the reference documentation for 2.2.0.RC1 for more details.

Using the same POJO for a JPA entity and an Elasticsearch document

I have annotated a JPA entity both with #Entity and #Document (from Spring Data Elasticsearch) so I am basically indexing into Elasticsearch the same POJO as JPA.
Is this a good and recommended practice?
If not, what is the alternative? Having two distinct POJOs and mapping the JPA entity to the ES document using something like Dozer?
I think it depends on your use case and the complexity of your object model. For a simple object model, I think that's fine. For a more complex object model, there are a few things to think about:
Do you really want/need to index all of the properties on the entity?
Do you need to transform the object before indexing it? For example, flattening relationships.
Will the serialization to ES cause lazy relationships to be loaded when you don't want them to be loaded?
If not, what is the alternative? Having two distinct POJOs and mapping
the JPA entity to the ES document using something like Dozer?
It looks like Spring Data Elasticsearch is using Jackson for serialization. That's a pretty basic default configuration. If that doesn't work for you and you don't want to introduce a DTO type object, you can always implement your own mapper.

EventListener like AbstractMongoEventListener in Spring Data JPA?

We are migrating from a Spring Data MongoDB repository to a Spring Data JPA repository. We were using the AbstractMongoEventListener to capture onBeforeConvert and onAfterLoad events to enhance the data objects before returning them from our Service layer.
I cannot find similar EventListeners in Spring Data JPA. Are there hooks in Spring Data JPA that I can use to do the same thing? It would be great if I can avoid modifying our service layer to make this change from MongoDB to JPA.
Thanks!
The #PrePersist annotation is exactly what I was looking for.

spring-data mongodb POJO mappings in XML instead of annotation

Spring-Data provides Mapping annotation support for MongoDB. Annotations like #Id, #DBRef, #Field to help in mapping the objects to documents.
How do I provide the mapping information in XML instead of annotating the POJO.
There is no support for XML mapping in Spring Data MongoDB. For simple scenarios you can just save your POJO without any mapping and it will be persisted in Mongo.
Take a look at MongoTemplate save method Javadocs.

Auditing with Spring Data JPA

I am using Spring Data JPA in an application in which all entity objects need auditing. I know that I can have each either implement Auditable or extend AbstractAuditable, but my problem is coming with the overall auditing implementation.
The example on the Spring Data JPA reference pages seems to indicate that you need an AuditableAware bean for each entity. Is there any way to avoid this extra code and handle it in one place or through one configuration?
The generic parameter of AuditorAware is not the entity you want to capture the auditing information for but rather the creating/modifying one. So it will typically be the user currently logged in or the like.