jdbc data independence without using hibernate api? - jdbc

I am trying to solve problem by using properties file but in properties file we can handle only database driver problem.if we want to mysql to oracle database need to change all my query.the problem is now how to make query independent without hibernate api?? please suggest

Related

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.

Can anyone explain detailed use of persistent.xml file in hibernate JPA project?

I have read articles about hibernate -JPA on the internet but unable to understand how it used and how it is working in the background for hibernate. Also, need information about what should be the name of a persistent unit in persistent.xml and what is the use of it?
Mainly we use persistnce.xml / hibernate.cfg.xml to tell hibernate the following details
What Database you're using? so, that hibernate executes all the queries w.r.t that Database
Connecting to Database like Providing Username and password
The database name
Hibernate mapping files name and many more
Go through this link

Change the Hibernate database schema at run time in spring boot application

I have A requirement where the current schema to be used is stored in db table;(schema_a or schema_b).
The application is loaded with default schema
spring.jpa.properties.hibernate.default_schema=schema_a
Now when the data in table is updated to B I want to consider all jpa query to use schema_b without any down time at server.
Your question is not clear enough to provide some code to your question, but you can catch inspiration from the following articles:
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Spring Boot - Change connection dynamically

configure database schema in spring boot application using jdbc template

I have a spring boot application using spring jdbc template for DAO layer connecting to Oracle DB. The DB username is different than the schema on which the queries will be run. Hence when the queries are run it needs to run using a different schema and I do not want to prefix the hardcoded value for the schema(For ex select * from user1.table.....)
I researched a bit and couldn't find a simple and straight way to do that.
For ex if I were using JPA I could have simply configured the property spring.jpa.properties.hibernate.default_schema=<schema name> but couldn't find an equivalent way of configuring the same when using spring jdbc
I ran into a similar problem and didn't find an ideal way to do it. I ended up setting the schema in SQL when the app loaded. At least I was able to reuse spring.jpa.properties.hibernate.default_schema, which I already had set for the JPA default schema.
final String schemaName = jpaProperties.getProperties().get("hibernate.default_schema");
jdbcTemplate.execute("SET SCHEMA '" + schemaName + "'");
This clearly isn't ideal, but it is better than defining your schema in multiple places.
(Note: I autowired both the JpaProperties and JdbcTemplate.)
You need to use the Oracle JDBC Driver.
A good example can be found in this mykong article:
# Oracle settings
spring.datasource.url=jdbc:oracle:thin:#localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver

Customize JDBC query generation from JPA for select/insert/update/delete?

I want to route all jpa-generated jdbc requests to Oracle packages that actualy perform select/insert/update/delete queries. Is there any possible way to do this?
Update: there is no problem to generate Oracle packages for this usage, the problem is in customization of generated JDBC requests.

Resources