Spring Boot JdbcTemplate Filtering Mechanism - java

I've got a Spring Boot application with an API which retrieves entities from a data source. This is implemented using JdbcTemplate which for the most part is all I need.
However, I've got an endpoint which takes many query parameters which will need to be reflected in the database query. Something like
GET /api/v1/people?age=10&height=125&weight=40&eyecolor=blue&...
To save me spending the rest of my life creating the SQL query for this - does Spring provide some easy filtering mechanism? I'm aware that Spring JPA would do this but I'm hesitant to change all of my code to go down that route.

Spring does not offer a way to Process/Make a Filter on 'many query parameters" however you may wanna look at GraphQL http://graphql.org

Related

what is the difference between spring-boot-starter-data-rest and spring-boot-starter-data-jpa

I am trying to understand various dependencies of spring boot.
I have come across three of them:
spring-boot-starter-data-rest
spring-boot-starter-data-jpa
spring-boot-starter-data-jdbc
I would like to know the difference between the three. Tried searching online documents, which say the three are almost related to spring data. how to resolve the confusion?
To add in here I have also found an another dependency
spring-boot-starter-web-services. I think it supports both SOAP and REST. Its just my assumption, I waiting for an explanation
spring-boot-starter-data-jpa is used to access your database with JPA (Java Persistence API)
spring-boot-starter-data-jdbc is used to access your data with jdbc (Java Database Connectivity)
the difference between JPA and JDBC is the level of abstraction. JDBC is more low level, JPA is more 'magic'
and spring-boot-starter-data-rest is used to provide Rest Endpoints on top of your Spring Data repositories.
To do this, you just annotate your spring data repository with a RepositoryRestResource annotation and direct Spring MVC creates the Restful endpoints.
#RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(#Param("name") String name);
}
This is very handy if you do very CRUD like (Create, Read, Update, Delete) applications.
JPA means "Java Persistence API". It's for querying or saving data in relational databases using object-relational mapping.
REST means "REpresentational State Transfer". It's a style for creating web services that leverages HTTP verbs, a simpler alternative to SOAP.
JDBC starter uses the Spring JdbcTemplate to query or save data in relational databases without relying on object-relational mapping. You write SQL and ask the JdbcTemplate class to execute it in the database using JDBC.
I would say JPA and JDBC starters should be mutually exclusive: either one or the other.
You use REST only if you're writing web services. These may or may not query or persist data in a relational database. I would expect to see both the REST starter and a persistence starter in a pom if a REST service needed persistent data.
Three very different starters.
You ask why Spring Boot has a REST data starter parent that combines the two. In a word: convenience.

How can I have a shared schema in multi tenancy architecture with spring boot

After several readings about multi tenancy and how to implement it. I finally got my application working using Spring boot 2, Jpa and hibernate.
What I have for now is a schema per tenant architecture and every request use an X-tenantID header key to define the tenant from which the data should be queried.
Once I tried to implement security I found myself limited by this header key. What I want to achieve is to have a shared tenant where I should store all my users with the tenant identifier for each one of them, along with some other informations that should be used from all tenants. If it's possible how can I achieve such behavior? Is there a github project reference ? If a solution like eclipselink is proposed can you please reference an article explaining it's integration with spring boot?
The second question is how to create at runtime a new schema in database once a use is registered? A sample code or a documentation reference should be very welcomed.
Thanks

Hibernate and CRUDRepository Spring Boot

I am new to Hibernate and Spring boot. I understand that hibernate is ORM tool so that you can map objects to database and just use save() to save obj into the database automatically. In Spring Boot, I would do something like
public interface CourseRepository extends CrudRepository<Course, String>{
}
and somehow I will get all the CRUD operations like save and findAll etc.
From another tutorial of hibernate, it says that session is required to save the object to the database. However, I don't see any form of sessionfactory or session being implemented in Spring boot and I am still able to do the save and findAll operations etc.
How is this happening and what relations does this have with hibernate?
Where exactly should I start to find out more about hibernate, stuff like #OneToMany and basically understand the details of the whole application to database operations in spring boot? Thanks
Spring Boot takes care of all the session management. Refer to this question for better understanding:
Spring Boot & Spring Data: how are Hibernate Sessions managed?
there are two things crudRepository and sessionFactory. try to find out difference between them and also try to read what is JPA and Hibernate.

Do I need to configure hibernate before configuring spring-data?

I am a little bit lost here, my main goal is to create a MVC pattern with Spring MVC and include Spring-security with user/passwords from the database.
So far, I have Spring security and MVC running well, but I dont know how to include the database (I must use spring data in some point).
I've read the tutorials and info of the site, and it says its a layer that works with other ORM (such as hibernate). So my question is, Should I configure hibernate before Spring data?
Is there any guide on how to do it (where they use annotations only?).
The answer to the question "do I need hibernate configured to use Spring Data JPA" is "yes" if you want Hibernate to be your JPA implentation.
So here's what the stack looks like for a typical Spring MVC + Spring Data JPA application:
#Controller class with #RequestMapping
calls
#Service class
calls
#Repository class (this annotation is optional, extend CrudRepository interface)
The repository you write uses an #Entity (JPA) class to access the database through Hibernate as described in this tutorial. Spring does a great job hiding most of the setup details of Hibernate from you (you don't need a persistence.xml if you do it right).

Using Spring, JPA with Hibernate to access multiple databases/datasources configured in Jboss

I have a requirement where i need to configure a Spring based application to work with two databases. We have two databases, one that we use to keep the live data and the other database is used as a datawarehouse and contains archived data (which has the exact structure as the Live db).
To keep it simple, assume that there is a request to search for a product. What the application should do is to search for the product details in the Live database and if not found it will check the archive database.
If i need to configure such a setup, do i still need to configure to datasources and would the search code have to use the first datasource to check the live database and if not found it will run another query using the archive database?
The above is probably doable but i am wondering whether there is a better way of doing this. For example, is it possible for the application to work on a single datasource even though behind the scenes it actually works with two databases?
The application is based on Spring, JPA/Hibernate, SOAP and Mysql database and Jboss 7 as the application server.
Any examples showing how this is configured using Spring and Jboss would be very useful.
Thanks
Spring has exactly what you want - the AbstractRoutingDataSource. See this blog post on how to use it. In your case, you need to switch the datasource during one request, so you'll need to have 2 transactions, switching the datasource between them by changing the datasource indicator on the ThreadLocal:
For these DAOs, demarcate the wrapping Service-layer either with distinct packages, class names, or method names
Indicate to Spring that the Service-layer method calls should run in their own transactional contexts by annotating with #Transactional(propogation=Propogation.REQUIRES_NEW)
Create an Aspect (using AspectJ annotation #Aspect) to fire around the service-layer method calls (using #Around) to set the ThreadLocal value before the method call, and to unset it afterwards
In the #Controller, simply call the Service-layer methods. The Aspect will take care of setting the values to indicate which datasource to use, and the AbstractRoutingDataSource will use that datasource in the context of each transaction.

Categories

Resources