h2 database and sql server in spring boot - java

I was asked this question by a colleague, and I am unable to answer it:
I have an in memory database (h2), and I have a SQL database server which our app needs to talk too. But due to the two different SQL dialects we are now wondering how do we get around the issue by doing the least amount of work?
This to be done in spring boot. I am aware of how to connect to the h2 database and SQL database server, but I am unsure on the rest.

If everything is setup by Spring, you should not be worried about the 2 different SQL dialects. Just be sure that spring.jpa.database is set to default to let Spring autodetect the right dialect for each datasource.
Here's a link that explain how to use/initialize two datasource in a Spring Boot projet if you need some more information.

Related

Difference between connecting to a database using DriverManager and SpringBoot(Hibernate)

There are 2 ways to connect to a database when developing Java apps.
Using DriverManager
Connection conn = DriverManager.getConnection(url, name, password);
// execute the query.
Using application property file in SpringBoot
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:5432/db_name
spring.datasource.username=user
spring.datasource.password=password
Now you can use #Entity annotation on your class to put data into database.
My question is how are these 2 ways different. If not how, is SpringBoot method working same as DriverManager in the background.
I assume that by Driver Manager you wanted to made reference to JDBC and by Springboot(Hibernate) you wanted to say JPA.
To simply answer your question, both JDBC and JPA will connect to the driver. Just that if you use JPA this step is made by default without you explicitly coding it.
You can look at JPA as an upper layer of JDBC which handles all the boilerplate code like connecting to the driver.
You can read more about JPA and JDBC here: JPA or JDBC, how are they different?
When you set configuration properties you are just saying to spring: "Hey, i have this properties, can you autoconfigure what i need?". At this point spring at the start of application will use you configuration properties to setup everything you need to connect to your database (using DriverManager or not is not important).
Spring do exactly what you should to do to configure your database connection.
Remember that in 99% of cases you can't write better code than spring do. So, use spring properties

Why spring-data-jpa is not creating tables in MS SQL server?

When I use in-memory(h2) database, spring-data-jpa is creating tables, but, when I change datasource to point to MS SQL (2017), I cannot see tables getting created.
Can anyone help me to solve this scenario?
Note: I'm using spring boot in my project
Make sure you configured: spring.jpa.hibernate.ddl-auto=update
It's hibernate how creates tables and not Spring Data JPA.

Spring Auto configuration resulting in old MySQL dialect

I created a small POC app with spring boot, using hibernate (5.2.9) and maria db (10.1.19).
I had some sql dialect issues where my create/drop table SQL was using type=MyIasam but resolved that locally by setting the spring.jpa.properties.hibernate.dialect, however, when I deploy to the cloud (PCF) all of the cloud profile stuff kicks in, and I end up with hibernate deciding its dialect is going to be org.hibernate.dialect.MySQLDialect
this results in invalid SQL getting generated for creating new tables.
Note that I'm not really sure what else could be happening. This is a spring boot app (1.5.3) and the cloud profile is kicking in to do auto configuration. There's a bunch of properties injected. And I can't seem to get my dialect property to be respected.
This is a solid crushingly easy problem that is the escaping me.
Any ideas what I need to set, or provide as dependencies?
I tried removing all of the mysql dependencies, but then the connection string inject is jdbc:mysql... which i think may be part of the problem...

Choose JDBC database depends on logged user

I have a simple spring boot application. I am using JDBC connection configured by JDBC Template (JDBC url is got from properties file).
Can you tell me how to reach following thing:
JDBC Connection should be established in depends on logged user, I have a problem with projecting in spring such flow of control that object jdbc template will be created after loggining user.
It is about different users use different database.
Can you help me, please?
You can do the routing at the datasource level.
See https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
And
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.html

Trying to get data from Mysql db (Spring Boot + Vaadin)

I'm trying to get and write data to Mysql Db using Spring Boot & Vaadin. I'm new in Spring and it's too boringly to make simple web project with all of Spring's configurations. So I decided to use Spring Boot. And now my headache is - getting and writing data to Mysql Db. I wrote in application.properties configuration for DB, also "spring.jpa.hibernate.ddl-auto=create" create new table in db (if it's not exists), but no data loaded and no data added to db table. As I see from this doc https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html there is no need to write from beginning some classes to connecting db and getting data...
Please help. https://github.com/AntonKostyukewicz/VaadinCrud - here is my sources of project.
Have you looked at the Vaadin JPA Container add-on? If you are willing to talk to the DB in JPA, that's an excellent option. https://vaadin.com/docs/-/part/framework/jpacontainer/jpacontainer-overview.html

Categories