Spring data JPA get child collection attributes using projections - spring-data-jpa

I would like to fetch only few attributes from child collection entitites. So, is it possible using Spring JPA projections and Spring JPA repository?
Also, it should not execute multiple queries. Just one query to fetch the selected attributes form child collection.
I want to evaluate options first if it is available in Spring JPA before considering any other alternative.

Related

How can i get the reference objects in spring boot with mongodb

When we use SQL with Spring boot, we can use hibernate and add #OneToMany relationships. It helps us to get the reference objects from another entity.
As an example, suppose Order and OrderDetails entities. When I query the Order entity, It automatically maps with the OrderDetails entity and it brings the whole document together. How can I do such a thing with Springboot+mongodb? Is there any easy way to solve this problem? Is it called ORM?

spring data jpa multiple dynamic criteria search

I am looking for the best way (or pro/con of different methods) to do a search with multiple dynamic criteria. It means that the number of search criteria might be different.
I have found some method in spring:
MethodNameQuery (fixed number of criteria)
#Query(fixed number of criteria)
Specification
Querydsl
ExampleMatcher
Are there other options that I didn't list?
Which one should I use in spring boot with spring data JPA?
How to perform IN search with ExampleMatcher.
Thanks.

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.

JPA parents without children

I have a JPA entity named Child which extends another entity Parent.
I want a way of retrieving all Parent objects without any Child objects.
I am using Spring Data in my projects. I would also like to know if there are built-in or more appropriate ways of achieving this using Spring Data as well.
JPA supports polymorphic queries. It also supports limiting results to a particular type. It can be achieved with TYPE operator in WHERE clause of a query.
SELECT p FROM Parent p WHERE TYPE(p) = Parent

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