I am trying to develop an application using Spring boot and MySQL. As the documentation said, First I created the project using Spring initializr using Intelij Idea, configured the application.properties file, and wrote schema-mysql.sql file and data-mysql.sql file. After I ran the project, I found there are no tables or data in the MySQL database. What is wrong with my configuration? Please help.
application.properties file,
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
dependencies in pom.xml file,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
schema-mysql.sql file,
CREATE TABLE IF NOT EXISTS `SOL16_USERS` (
`USERNAME` VARCHAR(200) NOT NULL,
'PASSWORD' VARCHAR(200) NOT NULL,
PRIMARY KEY (`USERNAME`)
);
CREATE TABLE IF NOT EXISTS 'SOL16_PRIVILEGES' (
'PRIVILEGE_ID' INT(2) NOT NULL,
'PRIVILEGE' VARCHAR(15) NOT NULL,
PRIMARY KEY ('PRIVILEGE_ID')
);
CREATE TABLE IF NOT EXISTS 'SOL16_USER_PRIVILEGES' (
'USERNAME' VARCHAR(200) NOT NULL,
'PRIVILEGE_ID' VARCHAR(2) NOT NULL,
PRIMARY KEY ('USERNAME')
);
And the file/directory structure is,
src
|----main
| |----java
| |----resources
| | |----static
| | |----templates
| | |----application.properties
| | |----data-mysql.sql
| | |----schema-mysql.sql
As the Spring boot documentation mentioned, what have fixed my issue was adding spring.datasource.platform to the application.properties. That is what I have been missing to initialize the schema using schema-{platform}.sql and data-{platform}.sql.
{platform} = value of spring.datasource.platform
So my final application.properties file is,
spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username = root
spring.datasource.password = password
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.show-sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.platform=mysql
spring.datasource.schema=schema-mysql.sql
spring.datasource.data=data-mysql.sql
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
I faced the same issue and resolved it applying the below code in spring boot 2.0
spring.datasource.initialization-mode=always
You need to add below code in application.properties
and open your xammp/wamp or server then create yourdatabase for example studentdb
same name give in application.properties file
spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Please use the key field given below---->
spring.jpa.hibernate.ddl-auto=create
or
spring.jpa.hibernate.ddl-auto=update
Related
I am using postgresSQL instance on google cloud platform in spring boot app with spring data JPA.
But i am not able to connect to postgresSQL instance at the time of deployment.
I am not really sure on what dependency is required for this and what is the application properties configuration.
here is the code.
application.properties
spring.datasource.url=jdbc:postgresql://google/postgres?cloudSqlInstance=<instance-name>&socketFactory=com.google.cloud.sql.postgres.SocketFactory
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
pom.xml
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
app.yml for deployment on flex environment:
runtime: java
env: flex
service: payment2
handlers:
- url: /.*
script: this field is required, but ignored
beta_settings:
cloud_sql_instances: <instance-name>
Thanks in advance.Please help!!!
I am using the hibernate with spring boot and table created in given database but not in another database like 10.10.1.350 is management node and 10.10.1.348 and 10.10.1.349 are child db. when we create table using query directly in 348 then table auto created in 349. But we are using the hibernate then table created in 348 but not auto create in 349.
Properties like:-
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://10.10.1.348:3306/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=abc#1234
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Pojo Class-
#Entity
#Table(name = "test")
#Getter
#Setter
public class Test {
#Id
#Column(name = "tst_id")
private Long tstId;
#Column(name = "tst_nm")
private String tstNm;
}
pom.xml for mysql dependency-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Hibernate docs says:
Although Hibernate provides the update option for the hibernate.hbm2ddl.auto configuration property, this feature is not suitable for a production environment.
Just use Flyway or Liquibase for DB migrations. Spring boot integrates with both of them.
I have a Spring Boot application
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
and hibernate is set to access the database in the application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/banquets-booking?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# this not helps as puts in quotes table field name only
#spring.jpa.properties.hibernate.globally_quoted_identifiers=true
#update validate create create-delete
spring.jpa.hibernate.ddl-auto=update
Now I have added a new field to the entity, but the update fails:
2018-09-03 13:05:34.893 TRACE 20867 --- [ restartedMain] org.hibernate.type.TypeFactory : Scoping types to session factory org.hibernate.internal.SessionFactoryImpl#56922754
Hibernate:
alter table banquets-booking.sub_event
add column outlet_id bigint
2018-09-03 13:05:35.841 WARN 20867 --- [ restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
This looks like a bug, because Hibernate was able to generate the schema initially, but fails on the update.
How can I resolve this issue, without renaming the database?
This issue is not because of the minus sign.
You should try adding a dialects like one of the below lines.The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
OR
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.MySQLMyISAMDialect
OR
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
to see which one works for you.
I'm trying to connect my spring boot application with MySQL server.
I have followed the documentation properly but still, I'm getting some issue in connection
Following exception is throwing every time
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
In order for me to get around this error I have to log into MySql with the username & password stored in my application.properties file using MySql Workbench. After doing so the error no longer occurs. This only happens when trying to Run my spring boot project after restarting MySql server.
UPDATE: Adding "&allowPublicKeyRetrieval=true" to my spring.datasource.url in the application.properties file solved the problem. I figured this out after I noticed another error in the logs: "MySQL Public Key Retrieval is not allowed"
spring.datasource.url=jdbc:mysql://localhost:3306/db_example?autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
First of all restart MySQL server and retry again...
If it is not...
Did you add application.properties details and JPA, MySQL dependency?
Please Could you show pom.xml file and application.properties file.
appication.properties
spring.mvc.view.prefix=/WEB-INF/JSP/ #jsp file path
spring.mvc.view.suffix=.jsp
#Hibernate
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.generate-ddl=true
#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Thank you...!
In our Spring Boot project I am trying to accumulate messages in h2 in-memory database and load them into MySQL database once message number reaches 50.
Here is dependencies from pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- H2 DB -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
log4j.properties is as below:
log4j.rootLogger = DEBUG, sql
# Define the file appender
log4j.appender.sql=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.sql.URL=jdbc:h2:mem:liteDatabase
# Set Database Driver
log4j.appender.sql.driver=org.h2.Driver
# Set database user name and password
log4j.appender.sql.user=sa
log4j.appender.sql.password=sa
# Set the SQL statement to be executed.
log4j.appender.sql.sql=INSERT INTO LOGS (LOG_DATE, LOGGER, LOG_LEVEL, MESSAGE) VALUES (now() ,'%C','%p','%m')
# Define the xml layout for file appender
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
From my createTables-h2.sql file:
CREATE TABLE LOGS
(
id INT auto_increment PRIMARY KEY,
LOG_DATE DATETIME NOT NULL,
LOGGER VARCHAR2(250) NOT NULL,
LOG_LEVEL VARCHAR2(10) NOT NULL,
MESSAGE VARCHAR2(1000) NOT NULL
);
datasource bean for h2:
#Bean(name = "liteDataSource")
public BasicDataSource liteDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("h2.driver-class-name"));
dataSource.setUrl(env.getProperty("h2.url"));
dataSource.setUsername(env.getProperty("h2.username"));
dataSource.setPassword(env.getProperty("h2.password"));
Resource initData = new ClassPathResource("scripts/createTables-h2.sql");
DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initData);
DatabasePopulatorUtils.execute(databasePopulator, dataSource);
return dataSource;
}
The problem is log4j is trying to log into the database before it is
created
Error message:
log4j:ERROR Failed to excute sql
org.h2.jdbc.JdbcSQLException: Table "LOGS" not found; SQL statement:
INSERT INTO LOGS VALUES ('', now() ,'org.springframework.core.env.MutablePropertySources','DEBUG','Adding [servletConfigInitParams] PropertySource with lowest search precedence') [42102-193]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5389)
at org.h2.command.Parser.readTableOrView(Parser.java:5366)
at org.h2.command.Parser.parseInsert(Parser.java:1053)
at org.h2.command.Parser.parsePrepared(Parser.java:413)
at org.h2.command.Parser.parse(Parser.java:317)
at org.h2.command.Parser.parse(Parser.java:289)
at org.h2.command.Parser.prepareCommand(Parser.java:254)
at org.h2.engine.Session.prepareLocal(Session.java:561)
at org.h2.engine.Session.prepareCommand(Session.java:502)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1203)
at org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:126)
at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:115)
at org.apache.log4j.jdbc.JDBCAppender.execute(JDBCAppender.java:218)
at org.apache.log4j.jdbc.JDBCAppender.flushBuffer(JDBCAppender.java:289)
at org.apache.log4j.jdbc.JDBCAppender.append(JDBCAppender.java:186)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.apache.log4j.Category.callAppenders(Category.java:206)
at org.apache.log4j.Category.forcedLog(Category.java:391)
at org.apache.log4j.Category.log(Category.java:856)
at org.slf4j.impl.Log4jLoggerAdapter.log(Log4jLoggerAdapter.java:581)
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:131)
at org.springframework.core.env.MutablePropertySources.addLast(MutablePropertySources.java:109)
at org.springframework.web.context.support.StandardServletEnvironment.customizePropertySources(StandardServletEnvironment.java:84)
at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:122)
at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
at org.springframework.web.context.support.StandardServletEnvironment.<init>(StandardServletEnvironment.java:44)
at org.springframework.boot.SpringApplication.getOrCreateEnvironment(SpringApplication.java:436)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:335)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
at com.example.DemoApplication.main(DemoApplication.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Please help me out if you can offer any solution!