Hei guys, im pretty new to SpringBoot and i have 1 problem.
When i start the app the spring read the schema.sql and make the tabels but the data.sql is not read, so i dont have no data on my db.
this is my app.prop
spring.datasource.url = jdbc:mysql://localhost:3306/garagesaledb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultComponentSafeNamingStrategy
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.sql.init.mode=always
spring.datasource.schema = classpath:db/schema.sql
spring.datasource.data = classpath:db/data.sql
spring.jpa.hibernate.ddl-auto=update
spring.liquibase.drop-first=true
spring.jpa.defer-datasource-initialization=true
schema.sql
create table asset
(
id bigint not null auto_increment,
category varchar(255),
price decimal(10,2),
quantity integer,
purchaseOrder_id bigint,
primary key(id),
);
data.sql
INSERT INTO garagesaledb.asset(category, price, quantity,purchaseOrder_id) VALUES
('MOUSE', 10.0, 1, null);
It's just a simple app and query but it doesnt read it.
Either you put the file somewhere outside the resources (external file) or provided the location wrongly, - please check the logs
To confirm that, change below line
spring.datasource.data = classpath:db/data.sql
to :
spring.datasource.data = file:<full-path>db/data.sql
OR
in some spring-boot versions,
spring.sql.init.data-locations=classpath:db/data.sql
Add this property to app.prop spring.datasource.initialization-mode=always
Refer Link : https://docs.spring.io/spring-boot/docs/2.1.0.M1/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
Spring Boot automatically creates the schema of an embedded DataSource. This behaviour can be customized by using the spring.datasource.initialization-mode property. For instance, if you want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
I hope this helps.
Related
There is spring boot application.
Here is configuration:
spring
flyway:
locations: classpath:db/migration
baseline-on-migrate: true
schemas: ${app.db.schema}
placeholders:
schema: ${app.db.schema}
init-sqls: CREATE SCHEMA IF NOT EXISTS ${app.db.schema}
And it doesn't work.
I need to create db schema before flyway will run migrations.
Flyway tries to read database migration scripts from classpath:db/migration folder by default.
All the migration scripts must follow a particular naming convention - V<VERSION_NUMBER>__<NAME>.sql.
Create a new file named V1__Create_Tables.sql inside src/main/resources/db/migration directory and add the sql script, for example:
-- ----------------------------
-- Schema for helloservice
-- ----------------------------
CREATE SCHEMA IF NOT EXISTS helloworld;
-- ----------------------------
-- Table structure for user
-- ----------------------------
CREATE TABLE helloworld.users (
id BIGSERIAL PRIMARY KEY NOT NULL UNIQUE,
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
first_name VARCHAR(255),
middle_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
enabled bool NOT NULL DEFAULT true,
account_locked bool NOT NULL,
account_expired bool NOT NULL,
credential_expired bool NOT NULL,
created_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE helloworld.users IS 'User table';
When you run the application, flyway will automatically check the current database version and apply any pending migrations. By default, no additional properties are required. You can also create a schema in this script. Or flyway will do it for you if you specify a non-existent scheme.
If you are using hibernate, check this property:
spring.jpa.hibernate.ddl-auto=validate
For more information, see the instructions.
There is documentation link: https://flywaydb.org/documentation/commandline/migrate#schemas
Actually FlyWay is responsible for creating schemas if they don't exist.
Here is an example in changelog-history table:
I'm trying to write tests using in-memory DB.
I wrote an sql to clean and store data to DB. But I have an exception:
Caused by: org.h2.jdbc.JdbcSQLDataException: Hexadecimal string contains non-hex character: "e7485042-b46b-11e9-986a-b74e614de0b0"; SQL statement:
insert into users (user_id, name, created_on, modified_on) values ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', null, null) -- ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', NULL, NULL) [90004-199]
My sql:
insert into users (user_id, name, created_on, modified_on) values ('e7485042-b46b-11e9-986a-b74e614de0b0', 'Ann', null, null);
insert into product(product_id, name, created_on, modified_on) VALUES ('f3a775de-b46b-11e9-95e4-af440b6044e6', 'product1', '2019-08-01 17:51:51.000000', '2019-08-01 17:51:51.000000');
insert into products_users(user_id, product_id) VALUES ('e7485042-b46b-11e9-986a-b74e614de0b0', 'f3a775de-b46b-11e9-95e4-af440b6044e6');
My application.properties:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Using spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL fixed it for me.
Or adding an annotation #Type to the UUID field should fix the issue:
#Id
#Type(type="uuid-char")
private UUID user_id;
The actual cause of this problem is the mapping between your object and the generated create table statement by hibernate (ddl-auto:create) used to create your h2 database schema.
If you enable the output of the those ddl statements using:
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=TRACE
you will most likely see that your UUID class has been mapped to a binary column in your database.
Hibernate:
create table <your_table> (
id bigint generated by default as identity,
...,
<your_object> binary(255),
...
primary key (id)
)
This means that your uuid-string is mapped onto a binary column and thus contains illegal characters. You need a varchar(<uuid-length>) column to store a uuid. There are several solution strategies, one of them is defining a type, see this StackOverflow answer. You can read on binary columns on the official MySQL reference site.
I resolved this problem by adding spring.jpa.hibernate.ddl-auto=none to my application.properties file
I am trying to implement DB migration with Flyway 4.2.0 + Oracle 11g
I have this empty schema:
And when I try to migrate, Flyway says:
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty
schema(s) "PASHA" without metadata table! Use baseline() or set
baselineOnMigrate to true to initialize the metadata table.
This is the config:
#Bean(initMethod = "migrate")
Flyway flyway() {
Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(false);
flyway.setSchemas("PASHA");
flyway.setLocations("classpath:db/migration/oracle");
flyway.setDataSource("jdbc:oracle:thin:#host:1521:test", "login", "password");
return flyway;
}
Why do I get this message? My base is empty.
Flyway itself uses a query to check if the schema is empty.
In the case of oracle, the query is:
SELECT * FROM ALL_OBJECTS WHERE OWNER = ?
Execute that query (with your owner in the place of ?) and see if it returns something (it does).
For instance, LOBs that haven't been purged show there. If that's the case, try:
purge recyclebin;
and the query should be empty now.
You need to either let Flyway create the schema itself (meaning there should not be a 'PASHA' schema created before hand), or baseline the existing schema (meaning setting your configuration with flyway.setBaselineOnMigrate(true) ).
Basically, Flyway tries to create a schema ('PASHA' in your example) which already exists.
Adding all of these helped. But the one without spring actually did the trick! Silly as it is, but just worked!
spring.flyway.baselineOnMigrate=true
spring.flyway.baseline-on-migrate = true
flyway.baseline-on-migrate= true
flyway.baselineOnMigrate=true
Here's my application.properties file:
spring.datasource.url= jdbc:postgresql://localhost:5432/Crypto
spring.datasource.username=postgres
spring.datasource.password=wololo
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=always
And here's my data.sql file:
INSERT INTO crypto(departure, details, lowest_price_date, lowest_price_ever, coin_name, url)
VALUES (null, null, null, 10, 'foooobs', 'http://foo.foo');
The problem is every time I restart the app, a new record is added even if it exists from the previous session. I want to initialize the database with data only once and not change it ever.
Any ideas?
one possible solution is to use fixed ids :
INSERT INTO
crypto (id, departure, details, lowest_price_date, lowest_price_ever, coin_name, url)
^^
VALUES (1, null, null, null, 10, 'foooobs', 'http://foo.foo');
^
So what will happen, first you are making spring.jpa.hibernate.ddl-auto=update with this solution, if the record not exit then create a new one, if it is exit, then just update the attributes, and not create a new record.
Or maybe you can take a look at flyway, it is a good tool in such situations.
I use spring-data-jpa and mysql database. My tables character set is utf-8. Also I added ?useUnicode=yes&characterEncoding=utf8 to mysql url in application.properties file. Problem when I pass characters like "ąčęėį" to controller to save it in mysql. In mysql I got ??? marks. But when I use mysql console example update projects_data set data="ąęąčę" where id = 1; every works well.
application.properties:
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=utf8
spring.datasource.username = gehive
spring.datasource.password = pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
tables:
+---------------+--------------------+
| TABLE_NAME | character_set_name |
+---------------+--------------------+
| customer | utf8 |
| projects | utf8 |
| projects_data | utf8 |
+---------------+--------------------+
Try
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=UTF-8
It seems issue is due to missing "-".
Reference:-
https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next
I had the same issues, and I solved it by adding this line to my application.properties file:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Note: The following didn't work:
spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
For anyone using the HikariCP connection pool who prefers not to append the parameters directly to the JDBC URL:
The spring.datasource.connectionProperties property was removed some time ago. Since then, you need to use connection pool specific properties as shown in #SebTheGreat's answer:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Hikari doesn't have a connection-properties property, but this works:
spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
Remember to escape any special characters, as below:
spring.datasource.url=jdbc\:mysql\://localhost\:3306/${SERVER_DB_NAME}\?useUnicode=true\&characterEncoding=utf\-8\&characterSetResults=utf\-8
In my case that's solve my issue
https://mathiasbynens.be/notes/mysql-utf8mb4
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
It works for me if using mysql java connector version 5.1.44 (before I used connector 8.0.17 it doesn't work)
Another possible solution depending your configuration is using #SqlConfig annotation
#Sql(config = #SqlConfig(encoding = "utf-8"))