Replace hsqldb with MySQL - mysql

I have a Spring REST project configured with hsqldb.
I would like to change it to MySQL.
I have MySQL server installed and running, but I don't know how to modify this pom:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
PS.: Project im talking about comes as a source code for 'Spring REST' book:
http://www.apress.com/9781484208243
source code download link:
http://www.apress.com/downloadable/download/sample/sample_id/1704/

As far I see you are using Spring Boot on this, so you can easy change the databases changing the drivers dependencies from:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
To
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
The driver version will be version on parent pom.
Then specify the parameters on properties
spring.datasource.url=jdbc:mysql://localhost:port/yourdb
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # we can ommit this if we want, Spring Boot will deduce based on the classpath
For more configuration on databases you can see the properties available on appendix here

Related

H2 and mysqldriver error: jdbcUrl not accepted

I've created a spring boot application running in my dev-environment with H2 dependency. This was working fine. After also adding mysql driver in the dependencies, for testing in cloud purposes, I cannot use the H2 configuration anymore, because of the following error:
Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, jdbc:h2:mem:memDb;DB_CLOSE_DELAY=-1
However, my application.properties that's being used, doesn't specificy to use the mysql driver. How to solve?
Application.properties:
# H2 DB settings
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:memDb;DB_CLOSE_DELAY=-1
spring.datasource.driveClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Dependencies:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>

Reverse engineering with hibernate on a maven project using MySQL

I am trying to apply reverse engineering with hibernate on a maven project using MySQL as a data base, I allredy installed the jBOSS from the market place and configuration properties are successfully created by I have problem with the java.lang.runtimeexception java.lang.reflect.invocationtargetexc
this problem is due with the missing of dependencies of your maven project to resorve that try to add those dependencies in your project
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate3</artifactId>
<version>3.2.3.GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>

Error:org.apache.commons.logging.LogFactoryjava.lang.NoClassDefFoundError for application build with micronaut-bom1.2.10, graal19.2.1 & aws-sdk2.10.56

Created an application using micronaut bom 1.2.10 version, along with software.amazon.awssdk:lambda:2.10.56 & software.amazon.awssdk:s3:2.10.56 dependencies which had functionality to retrieve data from s3 storage and used graal 19.2.1 to create a native image.
The native image is successfully created but when i try to access the endpoint it fails for below exception:
failed: org.apache.commons.logging.LogFactoryjava.lang.NoClassDefFoundError: org.apache.commons.logging.LogFactory
and series of exceptions with specifically while creating S3 client.
The exception also had failure at below point:
failed: Could not initialize class software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactoryjava.lang.NoClassDefFoundError: Could not initialize class software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory
Code for S3Client :
S3Client s3Client = S3Client.builder().region(getRegion()).build();
build-native-image.sh
${GRAALVM_HOME}/bin/native-image --no-server -cp example-function-*-all.jar
-H:IncludeResources="git.properties"
-H:IncludeResources="logback.xml"
-H:IncludeResources="application.properties" \
So this issue was resolved by adding below configuration in build.gradle:
allprojects {
configurations {
all {
exclude(group = "commons-logging")
}
}
}
and in the dependencies added:
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.30'
I hope this helps.
I faced the same issue, when running tests, with the exact same NoClassDefFoundError but when using AWS sts and secretsmanager libraries. I could actually get it to work by just including the jcl-over-slf4j dependency, while the error happened when using the slf4j-api dependency. I did not need to exclude commons-logging. Snippet of dependency in maven pom.xml that worked:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.89</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>secretsmanager</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
</dependencies>

on AWS - could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet aws

I have this (common) problem with my DB access. But in my case, I dont find the right answere...
Created a very basic MySql DB and Spring Boot App accessing it.
On my localhost it runs smooth.
Then I dumped the local DB. Inserted it into the AWS-MySql DB.
And deployed the Spring-Boot app with Docker to my EC2.
Everything is well.
But on using the AWS MySQL all REST Endpoints return:
status 500
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
For me it looks like all the same as it was on localhost.
What did I miss?
I found the cause. I know a solution.
BUT I cant accept to not to know how it happend:
My POJOs are defined by Classname only (no Table="" Metadata decortion)
On my local machine it runs well.
The SQL query is: "... 'Dbname'.'tablename' ... "
Dockerized on AWS on the RDS Mysql.
The SQL query is: "... 'Dbname'.'TableName' ... "
And then the of course the query fails.
My configs:
properties:
#Hibernate (Spring-JPA)
hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-services.version>1.5.0.RELEASE</spring-cloud-services.version>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-service-registry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-dependencies</artifactId>
<version>${spring-cloud-services.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
So obviously my NamingStrategy settings will be ignored or overriden in local or aws.
But at this point I could need some help.
PS: And why I cant find any useful documentatins about the Hibernate naming strategy settings?!? e.g.: strat-setting-A => TableName -> TABLE_NAME etc...
Why do I have to read the code to understand this? (T.T)
EDIT:
Hooray! I have the cause.
My local db config is diffrent. Well its by default: lower_case_table_names=1
So it just ignores CamelCase. But on aws it seems to be set to 0 Which means it takes care of camel case names.
So soultion is easy: Setting my naming strategy to lowercase only!
Just WHERE do I find the docs which config is right?? :-)
Well anyway... better no docs then wrong docs!
SOLUTION:
2 Options:
1 - Configuring the POJOs manually with #Table() decorator.
2 - Removing the hibernate naming setting from the properties (or change it to what ever needed)
PS: Iam still interessted into all setting possibilites if someone know a good doc.

Can I install the mysql jdbc connector using mvn install:install-file?

I'd like to install the JDBC connector using maven.
I have the following: mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connnector-java -Dversion=5.1.6 -Dpackaging=jar -Dfile= -DgenerationPom=true
I think all I'm need is what I put on the other side of the =Dfile= ?
I haven't used maven in a while either, so I'm not sure what the file switch is used for?
Thanks for all the insight!
The "install-file" or "deploy-file" goals are used for installing or deploying artifacts to your local or internal repository that are not available from Maven Central or other external repositories that you may have configured.
If you've got access to Maven Central, simply adding the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
...should do the trick.
To answer your question though, the -Dfile= argument is for specifying the artifact that would actually be installed in the local repository.
lotz answer is right and that should be sufficient
But, If you want to use the latest version of the connector, you can check https://mvnrepository.com/artifact/mysql/mysql-connector-java
Add the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>