Set Intellij IDEA JPA Console naming strategy - spring-boot

I have an all-default (excepts Lombok) Spring Boot 2.5.6 + PostgreSQL project with a very basic JPA mapping open in Intellij IDEA 2021.2.3. JPA configuration is implemented with annotations (no XML). The app itself works fine, records get saved and retrieved from the DB. In "Persistence" window I can see my JPA structure correctly, datasource is assigned. Here is the entity code:
#Entity
#Data
...
public class Cover {
...
private String ageRestriction;
}
But when I try to execute a query with e.g. select c from Cover c I get an error:
jpa-ql> select c from Cover c
[2021-11-01 16:14:03] [42703] ERROR: column cover0_.agerestriction does not exist
[2021-11-01 16:14:03] Hint: Perhaps you meant to reference the column "cover0_.age_restriction".
I tried to add the naming strategy configuration to my settings but this had no effect:
spring:
jpa:
hibernate:
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
I don't see "Assign naming strategy" in the persistence unit context menu as well (see screenshot). Is there a way to force JPA Console to use the same naming strategy my app does?
PS: I've managed to make the console work after specifying #Column for all multi-word field names and adding #Table annotation with schema and name properties. But I would prefer to be able to sync console and app JPA/Hibernate configurations somehow.

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.

Why I can't find my tables in H2 schema / How can I validate which H2 schema my Spring boot app is working with?

I'm running a spring boot app
didn't have any setting for h2 other than maven
when i'm connecting to the h2 console i can see the tables that were supposed to be created for two entities
i connected with the JDBC URL: jdbc:h2:mem:testdb (which is supposed to be the default)
Is there a way to make sure what schemas is H2 currently running/ or some log file for H2 ?
in my application.properties i have this:
spring.h2.console.enabled=true
spring.h2.console.path=/h2
I read somewhere that H2 initializing itself upon login, but a demo i was watching these were the exact steps taken , so not sure that is the case.
these are the settings in the H# console:
You can explicitly instruct spring boot to create and connect to a particular schema in H2 with config as below.
spring.datasource.url=jdbc:h2:~/test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=sa
This creates a datasource of name test database in h2 in file mode. There would be a file called test.db in your home folder which would be the data file for the database.
DB_CLOSE_ON_EXIT property decides to recreate the database on every restart.
There is an easier way to tell Spring JPA the default schema for your H2 data source by just adding the "SET SCHEMA {default schema}" in the datasource url, e.g.:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS testdb\\;SET SCHEMA testdb
I actually saw the right schema all along
The reason I thought I wasn't seeing the right schema was - the JPA Entities I expected to see, were not there.
I then found that this was because I didn't name the package for the JPA entities correctly
I named it "domain" (see pic):
I should have named it com.example.domain as can be seen:
This is because Spring Boot looks is doing a #ComponentScan "under" the package with the main class , so I had to prefix the "domains" with the name of the package that the main class resides in, which is com.example.
For me I had to check the log when I run Sprinboot
jdbc:h2:mem:9967b201-6b59-4925-acb3-d2e50dc5d9a5. --> this can be any other auto generated UUD
Adding this to your JDPC URL in the browser will let you see the tables that you created.

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/

Testing Hibernate Mappings

I'm using Hibernate to map objects to a legacy schema which contains some ginormous tables via annotations (as XML files are so 2003). Since these classes are so large, yes I occasionally make an occasional typo, which Hibernate doesn't bother to tell me about until I try to run it.
Here's what I've tried:
One: Setting hbm2ddl.auto to "validate":
This causes the String values of the class to validate against varchar(255). Since many of the column types in the database are CHAR(n), this blows up. I would have to add the columnDefinition="CHAR(n)" to several hundred mappings.
Two: Using Unitils.
Importing these via Maven causes imports of dependency libraries which blow up other sections of code. Example: I'm using Hibernate 4.1, but Unitils imported Hibernate 3.2.5 and blew up a UserType.
So, is there another way to do this? I looked at the Unitils code to see if I could simply yank the sections I needed (I do that with apache-commons fairly often when I just need a single method), but that's not a simple task.
Hibernate is configured via a Spring application context.
Any ideas out there?
I would write tests against an in-memory database (HSQLDB, H2) using the Spring testing framework. You'll quickly see any mapping errors when you attempt to run queries against the tables.
The test class would look something like this:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=MyTestConfig.class)
#TransactionConfiguration(transactionManager="txMgr", defaultRollback=true)
public class MyTest {
#Autowired
private SessionFactory sessionFactory;
// class body...
}
I would configure Hibernate to auto-deploy the tables as part of the tests.

Resources