What is the best way to maintain queries in Spring boot application? - spring

In My Application, Using the below technologies
Spring boot 2.7.x
Cassandra
spring batch 5. x
java 11
As part of this, I need to extract data from the Cassandra database and need to write out the file
so here I need to use queries to fetch data so
just want to know what is the best way to maintain all queries at one place so any query changes come in the future, I shouldn't build the app rather just need to modify the query.

Using a repository class is necessary. If you are using JPA i recommend using a repository for each Entity class. With JDBC it is possible to create a single repository which contains all the queries. To access the query methodes i would use a service class. In this way your code is structured well and maintainable for future changes.

Related

How to call database on multiple environments using Springboot JPA

I am new to Springboot and trying to build a small rest-service. We have a DB deployed on different environments (e.g. DEV, TEST). The rest-service will make a call to the appropriate database based on the received query param (e.g. ?env=TEST). The schemas of the deployed database are the same, the difference is only in connection string. I have some questions related to this task.
I read a few articles how to work with multiple databases using Spring JPA (for example this one: https://www.baeldung.com/spring-data-jpa-multiple-databases). It did work, but in the given example they get different entites from different databases using different queries, in my case the entity and the query is the same, but I still have to duplicate repositories, transactionManagers, entityManagers etc because of different datasources. And this is just two environments and I have more of them.
I have another thought that I might need to recreate the repository each time I process a request (to make the repository non-singleton). I am not sure if it is a good practice.
Maybe it worth to use JDBCTemplate instead of Spring JPA in this case?
Could you please suggest something how to approach such a task?

what is the best way to create session from java spring boot using oracle Database?

I created user in oracle database and I am trying to create session but I find many ways in spring boot so what is the easy way if I want to create classe connections using the Username and Password ?
You can jdbc template, spring data JDBC or spring data JPA, well depending on your use case.
If your data model is quite complex, you should avoid using the JDBC template as you will need to write prepared statements which can be cumbersome. JPA will allow you to use object-oriented programming principles and also will help you map the entities to your database columns.
For example, if you are going to use spring data JPA, you need to set the application properties as follows:
spring.datasource.type=oracle.oracleucp.jdbc.UCPDataSource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual
spring.datasource.oracleucp.connection-pool-name=UcpPoolBooks
spring.datasource.oracleucp.initial-pool-size=5
spring.datasource.oracleucp.min-pool-size=5
spring.datasource.oracleucp.max-pool-size=10
This would behind the scene create an Oracle Datasource. In this example, we are using Oracle Universal Connection Pooling. You can also use HikariCP which is quite popular.
check this out
If you want to use UCP with above properties then you must have SpringBoot version higher than 2.4.0.
Check out the Spring Boot code sample on GitHub.

Spring boot , Elasticsearch

Searched over the net but unable to find the satisfying approach.
I am new to spring boot and aware of starter dependancies,
I want to develop a springboot app using elastic search as a storage system.
Wherever i searched i found that somewhere my service class will have to implement some interface from springframework for ES crud operations.
Is there any other way without implementing or extending the components.
I myself want to create transport client and want to query ES by my code or methods not by overidden ones.
Please if you ahve ever seen any projects you can redirect me to that link .
Thanks.
Assuming I understand you correctly, you can use the Elasticsearch REST client: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html
You supply the JSON entities for the queries and parse the responses yourself. Its pretty basic in what it does, so you're not dependent on a lot of third party stuff to perform operations.

Easiest Way to Access Neo4J from Java

I want to access a Neo4j DB with Java and wanted to know what the preferred way to do this is. I just want to write a quite simple data structure to the DB.
http://neo4j.com/developer/java/ gives following options:
JDBC
Hibernate OGM
Spring Data
Rest API via Unmanaged Extensions
I looked into accessing Neo4J with JDBC and Hibernate OGM. It seems that its not worth it to use for me. JDBC gives me some trouble. So should i go with the REST way or try to fix my JDBC problems?
The JDBC driver is really a wrapper around the REST interface (as of neo4j 2.3). There is a example application how to use it. Should suffice for very simple use.
Then there is neo4j-ogm (different from Hibernate OGM) - this is an object graph mapping library, similar to hibernate in ORM world. This has minimal external dependencies and is very easy to use - ideal for cases where you want to map couple of objects into graph.
Then there is the Spring Data Neo4j project, which since version 4 uses neo4j-ogm for mapping, but adds other Spring data features, like repositories, derived finder queries, transactions ...

Spring Roo #RooJpaActiveRecord parameterized the JPA table catalog

I need a why to change the JPA catalog element in my java class? We have many database environments which we need to be able to deploy our application too. Example: In your dev environment we have a database for new development, and production support. All database live on the same server so we have the following database names: am_web_dd and am_web_ps. So we need to be able to change the catalog at build time or start up time. We've thought of using Maven to do a search and replace during build but I was wondering if there is a way of doing this with a parameter?
Here is one of our #RooJpaActiceRecord statements:
#RooJpaActiveRecord(catalog = "am_web_t4", schema = "dbo", table = "user_t")
I would like to be able to make catalog a parameter. Is this possible? if not what would be the best approach?
Thank you for your time!
I know it is possible from a JPA standpoint, but I don't think you will be able to using straight Roo. This might help.
There may also be a way to use a Java Configuration object in Spring to build your JPA Entity Manager. I think that's were you want to set it.
The approach you suggest of making catalog dynamic would be suitable if you wanted to let the user choose/change the schema on demand, however, it looks like this is not your requirements, so I would steer away from this path as it more difficult than you need.
You can use spring profiles to define different database connections and use an environment variable to define which one is active. Spring profiles can be set in XML or java configuration classes.

Resources