spring-data mongodb POJO mappings in XML instead of annotation - spring-data

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.

Related

Trying to use Crate.io NoSql database with an existing Spring Data / Mysql project

I'm attempting to add Crate.IO capability to an existing Spring Data/Eclipselink/MySql web application. For this specific use case, we want to persist data to both MySql AND Crate (for evaluation purposes) in the most painless way possible. I'm using the Spring-Data-Crate project in order to be able to use Spring Data Repositories with Crate.
I've been able to setup a separate Crate specific entity manager with a filter to only utilize repos that implement CrateRepository. The problem I'm having is determining how to use the existing Spring Data/MySql entity classes with Crate. (or derive from them)
1) If I annotate an existing Spring Data #Entity class with the Spring-Data-Crate
#Table annotation, the mapping to the crate DB will fail because EclipseLink/JPA adds hidden persistence fields to entities objects that start with an underscore, which is apparently not allowed by the spring-data-crate adapter
2) I tried to use entity inheritance, with a base class that both the MySql and Crate entity can extend, with only the MySql entity having the spring data #Entity annotation. Unfortunately, this causes Spring Data to lose visibility of the base class fields unless the base class is annotated with #MappedSuperClass. But adding this annotation introduces the hidden "_"-prefixed persistence properties to the derived crate entity.
3) I could use separate entities entirely and have them implement a common interface, but I can't assign the interface as the type of the spring data crate repository.
... Not sure where to go from here
Spring Data Crate adapter project - https://github.com/KPTechnologyLab/spring-data-crate
Spring Data Crate Tutorial - https://crate.io/a/using-sprint-data-crate-with-your-java-rest-application/
i'm johannes from crate.
we didn't test the use of spring data crate in that manner so we can't state any information if this should or shouldn't work.
sorry, johannes

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.

Set the fetch size with Spring Data

For large result sets, it’s important to increase the fetch size. There have been numerous discussions on how to set the fetch size for Spring’s JdbcTemplate. However, we usually use Spring Data for database access. How can I set the fetch size for a Spring Data query, assuming we use JPA with Hibernate as provider?
It depends on what you want to set. If you want it globally simply add it as a property to your persistence.xml (or what your way of configuration the EntityManagerFactory is). For hibernate that means adding the hibernate.jdbc.fetch_size property.
<property name="hibernate.jdbc.fetch_size" value="50" />
If you want to specify it for certain queries use query hints from JPA on the Query object.
TypedQuery<Foo> q = em.createTypedQuery("some hql here", Foo.class);
q.setHint("org.hibernate.fetchSize", "100");
Or when using Spring Data JPA use a #QueryHints annotation on the interface method. Can be applied to both methods with and without #Query.
#QueryHints(#javax.persistence.QueryHint(name="org.hibernate.fetchSize", value="50"))
List<Foo> findAll();
Links
Hibernate documentation
Spring Data JPA reference | javadoc
JPA 2 Query Hints javadoc
List of query hints for Hibernate

Spring Data MongoDB prevent persisting certain fields

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.