Spring Boot Rest API + JPA - spring

I have a CRUD based application, which uses Spring Boot REST Services and JPA. For JPA we have POJO objects mapped to RBMS - PostgreSQL.
Some of my pages require data to be fetched using joins of multiple POJO objects. I just wanted to know what is a good architectural practice to do the same. Following are some of the options i have been informed of, but not sure what are the pros and cons of each especially for a large data volume application.
Use Transient Variables in POJOs and use JPA joins
Use additional Spring View Objects to combine POJOs
Write native/HQL to join tables/POJOs
Any insight would be helpful. If any more details required from me, would be glad to provide.

I think it's better to go with Entity Mappings.
This will enable you to easily fetch the parent and its nested entities using either JPA methods or using hibernate.
You can also specify the fetch type to actually control the behaviour of this fetch.
In case, you are looking for any complex joins or fetch patterns, Entity Graphs and HQL will be very useful.

Related

JPA Repository, specify the fetch mode for specific methods

I'm using JPA and Hibernate for my Spring Project.
I created my db/entities and for some specific API I would like to improve my queries.
I think that for these, only for these specific scenarios, I need to use some joins. So, in practically, I need to have a different fetch mode (from LAZY to EAGER).
Is there a way to specify the fetch mode into my JPA repository for a specific method? Or have I to write the JPQL queries (or Criteria queries)?
You can use Named entity graphs to control fetch mode in any level of the object graph.

Onion Architecture using JPA Entity as Domain Entity

I've been struggling to choose to work with JPA entities as separated classes than domain entities in a single bounded context. I've faced the following choices
Use separated domain classes for Aggregate roots/Aggregates..etc with domain repositories to wrap Spring JPA repositories and use converters to map JPA entities <> Domain Entities with only required data
Lazy loading is about to be given away unless in mappers/converters are handling this inside domain repositories but this is overkill.
When saving objects, there might be related Aggregate roots (one to many relationship) which later in complex logic, I had to extremly take care of the state of the Domain entity to pass it to the domain repository and either fill it with all related data or simply map it (another method in the converter) with out relationship data (cascading not applied on JPA persisting)
A lot of duplicated code to avoid such situations even for very simple use cases
Or Use JPA entities as my domain entities and so far there are multiple examples/opinions of this like
https://github.com/citerus/dddsample-core/tree/Spring_Annotations_Autowire
http://www.javamagazine.mozaicreader.com/MayJune2018/Twitter#&pageSet=50&page=0
Should JPA entities and DDD entities be the same classes?
DDD, domain entities/VO and JPA
How to implement DDD using Spring Crud/Jpa Repository
On the other hand, there are opinions like this
Is it a good practice to use JPA entities as domain models?
My question, on the long run, from experience
What would cost more effort & time ?
Are both approaches are acceptable as practices ?
What are the pros and cons of both ?
What would cost more effort & time ?
Decoupling almost always does. It's trade-off !
Are both approaches are acceptable as practices ?
Yes. I see there are many conflicted opinions on both approaches but really, they're just opinions. Both are applied and cost.
What are the pros and cons of both ?
Using JPA entities as domain entities approach really 1- reduces the time cost notionally. 2- Also lets you use lazy loading with relationships avoiding more code in application service, that if you're not following referencing other aggregates by id instead which also is opinion based but really costs the lazy loading of JPA.
One down side to this approach is unit testing as I see it. Unit test should not depend on starting up container, database...etc. Should purely test business logic. But that's not optimally possible with such frameworks. See this answer for example
JPA Entity must be unit tested and how?
Using JPA as separated entities in the infrastructure with wrapper repositories will make unit tests easier to mock data and test purely the domain (business rules) with comfort. It will reversely to the previous pros, cost you the mapping effort and time, too much duplicated code for mapping, wrapping repositories..etc. It brings the headache (and this should be a pro) of caring what is the state of your domain entity because mapping of nulls to JPA entity will effect the relationships mapping to your persistence source, and you REALLY SHOULD CARE for the state of your domain entity.
Also automatic lazy loading of ORM will not be used and done easily. Either
1- You put a reference to other aggregates as member in your aggregate root (Breaking the aggregate ID reference rule) and handle that in the mappers
2- You get from repository only wanted data of aggregate root with other aggregate's ID as reference members. This is done by well defined queries in the repository implementation so, this is a lot of writing & customizing queries. Avoiding using default ones which returns full JPA entities with ready lazy loading related references.

What is the purpose & advantage of spring specifications

I would like to know why we are using spring jpa specifications in spring boot application. I could see something like specifications (classname).and , .or these kind of operations what exactly it is?
Spring JPA specifications allow you to define a predicate (a part of WHERE clause of a query).
This helps you create complex queries in an object oriented style, allow you to create reusable predicates that you may use in different queries without repeating yourself, and it can help you create dynamic queries that can be constructed at runtime.
One example of a good use case for specifications is a search page with optional filtering criteria (think any eCommerce website). Using JPA specifications you can define a predicate for each filter and at runtime you can include in your query only the predicates that are related to the filters that the user has applied.

Using SOQL queries in Spring JPA

I have to use SOQL queries within a Spring Data Repository, Is there a way to do so by using #Query annotation ? If not is there a alternative way ?
As far as I know salesforce doesn't expose the table structures. Rather they expose their objects and you can write queries on them. spring-data-jpa is used on top of an entity framework like hibernate. Unless you have entity objects mapped to actual database tables, spring-data-jpa is not useful.
The best way would be to use a query builder like jooq and construct SOQL queries easily using query builders.

How should I define non-entity repositories with Spring Data MongoDB?

On my domain I have the usual entities (User, Company, etc) and also "entities" that doesn't change, I mean they are fixed values but stored on data base. My backend is Mongo so I make use of MongoRepository. I'm also using Spring Data Rest.
Let's say I have defined Sector as entity, which is nothing more than a String wrapped on a Java object.
So this is how I define the repository.
#RepositoryRestResource
public interface SectorRepo extends MongoRepository<Sector,String>{
}
The thing is that this seems to be inappropriate, as I should not define an object that only wraps an string and treat it as an entity, it isn't. The only purpose for Sector collection is to be loaded on a combo box, nothing more.
The problem gets serious when you have more and more of these non-entities objects.
How I should approach this situation so I can still use MongoRepository + Spring Data Rest?
This is similar to couple of other questions. Please see my answers for both. Hope it helps
Spring Data MongoDB eliminate POJO's
Storing a JSON schema in mongodb with spring

Resources