Enhance Hibernate Database Schema? - java

I'm using Hibernate (JPA2) hibernate.hbm2ddl.auto=update for test and hibernate.hbm2ddl.auto=validate for production.
What I want to do is, extending the generated schema with an additional table (that is not mapped to an entity) so that this table is generated for the tests and verify for production.
Is this possible, and how?

Yes, it's possible using "auxiliary database objects". I wrote a blog post on the topic because the documentation wasn't the greatest.
Edit: One other undocumented feature of Hibernate that I didn't mention in that blog: if you include a file named "import.sql" in the root of your classpath when you run a Hibernate schema export, it will also execute the statements in that file.

Write an SQL script to create the table. When you release up the environmental chain, run the SQL first to create the table in Prod. Then validate will be fine.

Related

JPA/Hibernate automatically create/validate database functions

Is there any way to automatically create database functions using jpa/hibernate when i use spring.jpa.hibernate.ddl-auto=create ?
I've a number of custom functions (defined in a separated script file in the project) I use on my custom #Query in my repositories, but those functions needed to be create manually
You might think "what is the problem just run the script after created the database" but this must be done for every environment of the system: dev, test, prod... also it makes impossible to automatically test using in memory database (since when i hit run junit creates the database, hibernate creates the tables in the database, but nobody creates the functions so the repository wont work properly)
So how can I automatically create custom database functions using jpa/hibernate
You can add an import.sql file in your classpath.
Hibernate will create the schema and then run it on the selected db.
You can find more details in the documentation.
Note though that the main purpose for schema generation in Hibernate is prototyping or testing.

Spring Data / Hibernate Table DDL creation

with the property spring.jpa.hibernate.ddl-auto it's possible to auto generated table from entities.
What I need is that only some specific entities should be generated to tables in database an the rest of the entities do nothing.
It this possible ?
I guess this is your question here? https://discourse.hibernate.org/t/i-would-really-like-to-know-more-about-the-update/5455
Like I wrote there already, you shouldn't use hbm2ddl for anything other than development purposes. Use a proper schema management tool like Liquibase or Flyway.
As #pcsutar already commented, impl said SchemaFilterProvider

In Java, Spring maintaining database without any scripts

I am working on a project in Java (using Spring Boot, Thymeleaf, Hibernate, JPA, MySql). Every time I create a new Model Class, I have to create a table in the database or if I make any change in the Model class I have to alter the table by myself. Is there any way to avoid this database related stuff. For example I will make Model classes and declare their relationships my Database tables will be generated automatically. In future if I make any changes to my classes they will be applied to the database automatically without loosing any data.
Previously I worked on PHP, Laravel. There all I needed to do is 1) run command php artisan make:migration create_posts_table, 2) declare columns like $table->string('title');, $table->foreign('user_id')->references('id')->on('users'); and then 3) run command php artisan migrate. That's it. No SQL scripts needed. I was wondering if Java, Spring has something like this.
Sure you can do it.
Use spring.jpa.hibernate.ddl-auto=update in your application.properties.
You can also use more advanced tools like https://www.liquibase.org/
Ideal way
spring.jpa.hibernate.ddl-auto=none
In my opinion, the ideal way is to create one SQL file which will create the schema at the startup for us.
To let Spring Boot to create it for you
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
Other Possible values: create, create-drop, validate
More Detailed Explanation
You can do migration using Flyway, it's similar to Laravel migration.
Add the dependency and put your migration SQL files to classpath:db/migration. Flyway will automatically check the sql files version and apply any pending migrations.
https://flywaydb.org/documentation/plugins/springboot

Create schema before hbm2ddl

I want to test my program with an inmemory hsqldb. To create the table I use hibernate.hbm2ddl.auto=create
But I get an exception because the schemas, defined in the entity classes by annotations, are not created before the tables are created. Now I am searching for an opportunity to create the schemas before hibernate.hbm2ddl.auto runs. To remove the schemas is not an opportunity for me, because I need them for my program.
My problem is pretty much the same like this. The different is I do not use spring, so the solution does not work for me.
Assuming that you're using H2 database, you may provide init command to run with jdbc connection url. For example:
your.jdbc.url=jdbc:h2:mem:;DB_CLOSE_DELAY=-1;INIT=create schema IF NOT EXISTS your_schema
Unfortunately, the issue on hibernate jira is still unresolved https://hibernate.atlassian.net/browse/HHH-5665
Since Hibernate 5 there is a cleaner and more db-independent way, which also works with hsqldb. Add this configuration property:
hibernate.hbm2dll.create_namespaces=true

H2 in-memory database initialization with data

I'm using H2 with Hibernate to generate in-memory DB on the fly for unit-testing.
I managed to create the DB successfully, and everything is working ok.
But I have an issue I don't know how to approach.
I need to load reference data to the DB for testing prior to the execution of the tests.
I have this data sored as a SQL insert's file which I need to run only once in real time envirnemnt, however, because the DB is generated every time from scratch I need to figure out how to insert the data on runtime.
The data is quite simple, it's countries lists, states list, etc.
Whats the best way to do it ?
btw, everything is working underneath Spring framework.
For your tests you could execute an init script on creation of the connection.
http://www.h2database.com/html/features.html#execute_sql_on_connection
From the question tags I see you're using Hibernate. You can add a file named "import.sql" to your classpath (i.e. in src/main/resources if you're using a Maven project layout).
From Spring documentation
In addition, a file named import.sql in the root of the classpath will
be executed on startup if Hibernate creates the schema from scratch
(that is if the ddl-auto property is set to create or create-drop).
This can be useful for demos and for testing if you are careful, but
probably not something you want to be on the classpath in production.
It is a Hibernate feature (nothing to do with Spring).

Categories

Resources