Spring stateMachine persistance with Jpa - spring

I got a problem with persister configuration. The problem is similar to Spring State machine - Table Scripts , but I dont want to generate tables by myself or with liquibase. So I'd like to use StateMachineJpaRepositoriesAutoConfiguration but I can't find info how I should enable it.
I already tried to use #ImportAutoConfiguration and do some stuff in property file, for example:
spring:
statemachine:
data:
jpa:
repositories:
enabled: true
But, unfortunately It didn't work for me, and I still get the error:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException:
Schema-validation: missing table [action]
Maybe the way with StateMachineJpaRepositoriesAutoConfiguration is not suitable here, so I'm needed any advice

Tried to use embedded H2 and all the tables was created. So the problem was in my spring.jpa.hibernate settings.
So it should be spring.jpa.generate-ddl=true and spring.jpa.hibernate.ddl-auto=create

Related

H2: globally_quoted_identifiers seems not be taken into account

I use spring-boot 2.7.6 and H2 2.1.214.
To fix an issue due to the name of some columns that are also keywords in liquibase files (loaded to initialize the H2 database for tests) and entities, I tried to use the globally_quoted_identifiers property as defined here: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#_quoting_options
So I did it like this:
spring:
jpa:
properties:
hibernate:
globally_quoted_identifiers: true
But I still have the same errors due to keywords (org.h2.jdbc.JdbcSQLSyntaxErrorException) so I do not know if I use it correctly?
As a workaround I have to use NON_KEYWORDS=VALUE,KEY but I would like to manage all keywords globally.

How to specify utf8 encoding for db and tables, using embedded database MariaDB in Spring application?

Developing web application I'd like to use an embedded database to store data. However instead of proposed in-memory databases I`d like to prefer to use custom one. My choice is MariaDB. I have been ruled with this article how to get MariaDB as embedded database in my project, everything is fine, database appears, except one thing: I cannot change an encoding for my tables values.
I tried to set additional application properties in my application.yml file, tried to complete connection string with parameters of specifying encoding, but all those stuff didn't work unfortunately :(
May any of you have ever faced with such problem and can help me? Thank you in advance!
My current application.yml is following:
mariaDB4j:
dataDir: ./localMariaDB
port: 3307
databaseName: embeddedDB
spring:
datasource:
url: jdbc:mariaDB://localhost:3307/
username: root
password:
driver-class-name: org.mariadb.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: create
And finally I get exception bellow:
Caused by: java.sql.SQLException: Incorrect string value: '\xD0\x92 \xD1\x80\xD0...' for column 'current_status' at row 1
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readErrorPacket(AbstractQueryProtocol.java:1688) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readPacket(AbstractQueryProtocol.java:1550) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:1513) ~[mariadb-java-client-2.6.2.jar:na]
at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.executeQuery(AbstractQueryProtocol.java:318) ~[mariadb-java-client-2.6.2.jar:na]
... 121 common frames omitted
By the way, when I run the application, the connection string which I get looks like jdbc:mysql://localhost:3307/embeddedDB.
I am confused why there is :mysql: instead of :mariadb: as specified in my connection properties. Does it have an influence on my database behavior?
So, I have found the resolution to my issue.
It appeared as usual very simple :)
To change standard character encoding, which MariaDB proposes, I had to specify additional configuration properties before setting up the MariaDB's DataSource.
As this source provides to configure MariaDBSpringService entity to further use in DataSourceconfiguration, it is necessary to extend it (MariaDBSpringService) with some character encoding description utils. So, to specify custom encoding (i.e. to change standard one) I've just added the following lines of code to MariaDBSpringService bean:
service.getConfiguration().addArg("--character-set-server=utf8mb4");
service.getConfiguration().addArg("--collation-server=utf8mb4_general_ci");
In general, the full bean declaration is below:
#Bean
public MariaDB4jSpringService mariaDB4jSpringService(){
MariaDB4jSpringService service = new MariaDB4jSpringService();
service.getConfiguration().addArg("--character-set-server=utf8mb4");
service.getConfiguration().addArg("--collation-server=utf8mb4_general_ci");
return service;
}
Hope, this will help somebody, who further can possibly face with such issue))
Thanks to everybody, who tried to help me!

Schema not found using Hibernate and Oracle

Right now I am running into an issue where I am getting "MySchema" not found exception:
org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "MySchema" not found; SQL statement:
select batchstatu0_.batch_key as batch_ke1_0_ from myschema.batch_status batchstatu0_ [90079-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:573)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
When I run the generated sql from Oracle SQL Developer using the same credentials, the select statement correctly finds the schema and runs the query.
One of the things I initially tried with success but not the solution I would like to go with was to specify a whole new DataSource for the specified schema. That worked but will be a serious headache when we start adding event more schemas. I would much rather use one Datasource and specify the schema in the Table annotations.
#Table(name="BATCH_STATUS", schema ="MySchema")
public class BatchStatus {
...
}
And the properties:
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
hibernate.show_sql=true
spring.datasource.jdbc-url=jdbc:oracle:thin:xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
hibernate.default_schema=MySchema
I would assume this has to be some sort of config issue but I haven't been able to figure it out.
you need to specify
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
As per the exception, it is using H2 driver

Spring Boot. #DataJpaTest H2 embedded database create schema

I have couple of entities in my data layer stored in particular schema. For example:
#Entity
#Table(name = "FOO", schema = "DUMMY")
public class Foo {}
I'm trying to setup H2 embedded database for integration testing of my data layer.
I'm using #DataJpaTest annotation for my tests to get H2 embedded database configured automatically. However, the creation of tables fails because schema DUMMY is not created at DB initialization.
Any ideas on how to create schema before creation of tables in test cases?
I've tried to use #Sql(statements="CREATE SCHEMA IF NOT EXISTS DUMMY") but didn't succeed.
Also, I've tried to set spring.datasource.url = jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS DUMMY in my test.properties file together with TestPropertySource("classpath:test.properties"), but that didn't work too.
I had the same issue, I managed to resolve by creating schema.sql (in resources folder) with the content
CREATE SCHEMA IF NOT EXISTS <yourschema>
Documentation can be found here but imho the lack of real examples make it very complex.
Warning: this script is also executed within the normal (not test) environment.
Not mandatory, but good practice, add h2 dependency only in test scope
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
I think you are looking for this annotation:
#AutoConfigureTestDatabase(replace=Replace.NONE)
example:
#DataJpaTest
#AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
class UserRepoTest {...}
After couple hours of struggling I've found a workaround.
You can define spring.jpa.properties.hibernate.default_schema = DUMMY in your application.properties.
And then set spring.jpa.properties.hibernate.default_schema = in your test.properties and use together with #TestPropertySource("classpath:test.properties")
So, in this way the schema DUMMY won't be created and the entities will be created in default schema.
In my case schema.sql under test/resources din't worked.
The following configuration in test/resources/application.yml file worked.
spring:
datasource:
username: sa
password: sa
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:usrmgmt;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS DUMMY;
liquibase:
change-log: classpath:db/changelog/db.changelog-master.xml
In the above configuration, provided the below additional configuration
INIT=CREATE SCHEMA IF NOT EXISTS DUMMY extension to the existing DB url. In the absence of this, faced the exception Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "DUMMY" not found;.
spring.liquibase.change-log property. In the absence of this, faced the exception Caused by: liquibase.exception.ChangeLogParseException: classpath:/db/changelog/db.changelog-master.yaml does not exist.

How to see the schema sql (DDL) in spring boot?

How can I see the DDL SQL generated by Hibernate for building the schema from the JPA mappings? I am using the embedded HSQL db.
I tried the following and none of them worked in Spring-Boot 1.3.5.RELEASE.
Adding the following to application.properties file
debug=true
spring.jpa.properties.hibernate.show_sql=true
Set org.hibernate.SQL level to debug in logback.xml
Steps listed at http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
Those only show me the sql issued by Hibernate for queries. I am looking for the DDL schema sql issued by Hibernate due the following property:
spring.jpa.hibernate.ddl-auto=create-drop
Try with this property and value:
javax.persistence.schema-generation.scripts.action=create
Do not forget to also set this property:
javax.persistence.schema-generation.scripts.create-target=my-schema.sql
From the JPA 2.1 Specifiation, page 370:
javax.persistence.schema-generation.scripts.action
The javax.persistence.schema-generation.scripts.action property specifies
which scripts are to be generated by the persistence provider. The
values for this property are none, create, drop-and-create, drop. A
script will only be generated if the script target is specified. If
this property is not specified, it is assumed that script generation
is not needed or will
In Spring Boot you can define those two properties in your application.properties file:
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=build/my-schema.sql
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
Here is a blog post about JPA schema generation with further information about these and other properties:
http://www.thoughts-on-java.org/standardized-schema-generation-data-loading-jpa-2-1/

Resources