Hibernate, load schema from file - java

In Hibernate, I can use hbm2dll tool to import data files after hibernate generates the schema from my model. This is fine, but I want to load the file with my database schema and make hibernate use that instead of generating one.
Let's say I have some database configured in my hibernate config
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
Now before I run my test I want to load the db with some schema defined in my schema.sql file. And I don't want to use Spring, I know there is support for it there.
I know I could use hibernate.hbm2ddl to generate schema when SessionFactory is created but I'd rather want to load my manually defined schema. Something like <jdbc:embedded-database> from spring-jdbc.
Is there any support for that in Hibernate?

link1 and link2 might help you out.
Also you can set hibernate.hbm2ddl to none, and add your import.sql file in the classpath which will be executed at the server startup.

Related

How can I ask hibernate to make a Database for me? (maven & java)

I have University homework that asks me to create a simple database with hibernate, but I'm having a lot of troubles... I would like if someone would show me a solution.
I set the pom.xml with MySQL and hibernate dependencies; after that I made the hibernate.cfg.xml file in which I give it the connection URL. I am trying to do the annotated stuff so I don't have to do the other xml file... but it says that it can't find the database once I run the program, even if I specified in the cfg file that I have to create the database
<property name = "hibernate.hbm2ddl.auto">create</property>
Hibernate won't create the database for you by this property, only the tables.you can create it if it is not present already by adding a parameter to the URL.
jdbc:mysql://db:3306/mydb?createDatabaseIfNotExist=true

spring boot sql database DML and DDL scripts

How i could define some schema and data to be inserted into db for
sql database in spring boot
Also could i do this for embedded databases
For example i am using two databases and i want to populate some data or define some schema and apply to different databases before application starts.
A file named import.sql in the root of the classpath is 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 is probably not something you want to be on the classpath in production. It is a Hibernate feature (and has nothing to do with Spring).
You can take a look in spring docs

Create a Hibernate schema not automatically, but with SQL script

In my project I use h2 in memory database, and I want it to be created not by Hibernate, but with by a SQL script. Here is my hibernate.properties
I made
hibernate.hbm2ddl.auto=none
none to disable autocreation of database, and added
hibernate.hbm2ddl.import_files=schema.sql,insert-users.sql
schema.sql contains SQL code to create schema, and then to insert-users.sql and it contains the initial data.
The project builds successfully, but when I try to hit database, I get
a Table <tablename> not found exception.
Since Hibernate won't do this for you unless you use create or create-drop hbm2ddl, there are other ways to achieve what you want.
Specialized tools
There are tools that are created specifically for this: Flyway, LiquiBase. These are often configured to be run when the app is deployed and allow you to version DB scheme. They are applicable not only for testing (and mainly - not for testing), but for production as well. They can ensure that the scheme on all your envs is the same. If you use these tools, then it's better to set hbm2ddl to validate.
Spring's support
Less widespread way is to use Spring's support for embedded DBs:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
Data for testing
If the intention is to create data for testing (not scheme), then it's better to create entities and use your DAO/Repository layer to persist those in tests. This way you don't duplicate mechanisms of persisting data.
Two comments from the Hibernate documentation are relevant here:
This is useful for testing or demoing: by adding INSERT statements for example you can populate your database with a minimal set of data when it is deployed.
and
These statements are only executed if the schema is created ie if hibernate.hbm2ddl.auto is set to create or create-drop.
I'm not too sure that the import functionality will do what you want it to do.

Using persistence.xml to create a Session to query the database?

I am familiar with using Hibernate and the hibernate.cfg.xml file to create a Session.
However, I have a persistence.xml file within my project, not the hibernate.cfg.xml file.
How do I use it to create a Session that I can then use to query my database?
If you want to use persistance.xml you should use EntityManager. How to use EntityManager you can find in Hibernate, JPA – Part 1 tutorial.
The key config file for JPA is persistence.xml. This lives in the
META-INF directory. It details what the persistence driver to use
and what JNDI data source to connect to. Additional properties can
also be specified, in this case we’ll include some Hibernate
properties.
Next pat is about Hibernate, JPA & Spring MVC – Part 2.

H2 database schema creation check

I have H2 database generated on the-fly using Hibernate & Spring for my test-cases.
I have few weird errors, and I would like to know exactly how the schema looks like in H2. how the tables defined, and how the columns.
How can I generate this report during run-time ?
If the schema is being deployed by Hibernate using hibernate.hbm2ddl.auto=true you could turn on trace logging for org.hibernate.tool.hbm2ddl. This would show you the DDL Hibernate is generating when it creates your schema.
You could run the SQL statement SCRIPT NODATA.
To see the SQL statements that are run against the database, append ;TRACE_LEVEL_FILE=2 to the database URL. This will write all SQL statements to the .trace.db file.
If you want to know what are the schemas getting generated on the fly. Simply add
<prop key="hibernate.show_sql">true</prop>
With this you'll be able to debug.

Categories