Using SOQL queries in Spring JPA - spring

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.

Related

Spring Boot Rest API + JPA

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.

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.

How to use projection with Query by Example in Spring Data Jpa?

I'm trying to filter data using query by example. It's working for entities and primitives, but not for projections. Do You know if such functionality available in Spring Data Jpa?
At this moment it's not implemented in Spring Data Jpa 2.1.0.RC2, but as workaround it's possible to use extension like specification-with-projection

JpaSpecificationExecutor vs QueryDslPredicateExecutor

I want to know which approach is better when to use JpaSpecificationExecutor and QueryDslPredicateExecutor?, yes we can use both of them at the same time.
using queryDSL will create many additional classes, is it bad or good for memory usage and performance?
It's a matter of taste.
If you use JpaSecificationExecutor you relay on the standard JPA Criteria and Metamodel API. There the Metamodel API will create a meta model based on your mapping that you should use to be typesafe at compile time.
Where as QueryDSL is an additional framework that is not limited to JPA.
You can use it with JPA, SQL, Collections, MongoDB, Lucene and maybe more.

Is a Spring Data JPA #Query dynamic or named?

JPA #NamedQuery is translated to SQL only once when application is deployed and generated SQL is cached.
EntityManager.createQuery translates query every time our method is called.
What Spring-data-jpa is doing with query defined in #Query annotation? Is it translated to SQL once during deployment (like NamedQuery) or translated every time (like dynamic query) ?
Spring Data JPA calls EntityManager.createQuery(…) for every invocation of a query method annotated with #Query. The reason for that is quite simple: the Query instances returned by the EntityManager are not thread-safe and actually stateful as they contain bound parameter information.
That said, most of the JPA persistence provider perform some kind of text-based query caching anyway so that they basically build the actual SQL query once for a certain JPQL query and reuse the former on subsequent invocations.
As an interesting side note, when we started building the support for #Query in 2008 we looked into possibilities to rather register the manually declared queries as named queries with JPA. Unfortunately there wasn't - and up until today - there's no way to manually register a named query programmatically via the JPA.

Resources