how to close hibernate tools db connections - java

When developing a new project I often want to re-create the schema to apply any new entities or relationships created. I like using hibernate tools in eclipse, but it's a pain when wanting drop and re-create the schema - since it seems to maintain open connections to the db (postgres in this case).
Does anybody know if there is an easy way of getting the eclipse hibernate tools plugin to close off all connections?

It might be helpful to tell you, that i was using Hibernate Tools for creating the schema, too. I never had the problem you are describing. Nowadays i use liquibase for schema migration. It is a different approach, but i like it.

I'v never hit this problem before, but we use Maven run from the command line to create/update the schema and also DBUnit to upload data. Then back to elcipse...

Related

Without using hibernate.hbm2ddl.auto, how do I export all the initial schema into Flyway?

I am at the almost ready stage of my JEE development. With a lot of recommendation NOT to use Hibernate's hbm2ddl.auto in production, I decided to remove it.
So now, I found out about Flyway, which seems great for future db changes and migrations, but I am stuck at first step: I have many entities, some entities inherit from base entities. This makes the CREATE statement very complex.
What is the best practice to create the first migration file?
Thanks!
If you've taken an "entities first" approach during development you'll need to generate the initial schema in the same way for the first live deployment: This will produce the first creation script used by Flyway and there may also need to be a second associated script for populating reference data.
In a nutshell, the reasons for no longer being able to use hbm2ddl.auto after the first deployment are that create will destroy existing data and update isn't reliable enough to cover all types of schema changes (as it sounds like you may already know from this SO question).
Flyway is a very useful tool but it does require a level of discipline that may not have existed during development. When going forward from the initial release, database update scripts need to be produced for Flyway that are equivalent to the changes made to the entities since the last release. There are tools (e.g. various commercial products from Redgate) that may help here: These attempt to "diff" two schemas and generate schema and/or data update scripts for getting from database A to database B. But in my experience, none of them are perfect and they don't quite reach the holy grail of enabling a completely automated approach.
Arguably, the best way is an "as you go" manual approach to ensure that non-destructive update scripts are committed to source control whenever an entity change is made that affects the schema or reference data - but as already mentioned, this will require some discipline and/or documented processes for all team members to follow.
For the first migration file, you just need the current ddl of your database. There are many tools which can get this for you (such as the "copy ddl" option in the IntelliJ IDEA Database tool or a GUI client from your database vendor).
I am not sure about Flyway but there is an alternate way, you can use ant tasks for hibernate to generate or update schema.
Hope it helps.
If you build your project with Maven, you could use Hibernate maven plugin.

java (spring) create database diffs automatically

I'm seeking for same functionality in Java that present in PHP's Doctrine ORM.
I can describe an entity and the doctrine:migrations:diff console command will make me a fine migration which will generate SQL for updating the database schema. then I can update current database schema with new fields/tables etc running doctrine:migrations:migrate
But what's the same way to do it in Java? I've tried to do it with Flyway, but had no luck because it can't just get the entity and generate diff: https://github.com/flyway/flyway/issues/648#issuecomment-64208848
So, I just tried to do it with liquibase, but don't understand how to do it.
I've tried to ask this question in #java room on irc.freenode.net, but the closest answer was "let hibernate create the schema and have liquibase extract it" (thank you, k5_)
but… how?
many sources say that I must use somewhat "hibernate.hbm2ddl.auto"… but how? what is it?
I'm surprised that there's no examples "for dummies" over internet for this important functionality
I think that it is a terrible way to write all migrations by hands and I just don't believe that there are no solutions exist
please, help me
thank you in advance!
UPD
In my particular case I'm using Hibernate, I have basic entities defined (with OneToMany, ManyToOne mappings)
I want to start on clean database and updating its schema with migrations.
So my goal is to generate this migrations automatically from entities descriptions compared to current database schema state.
UPD 2
The second closest answer is (thanks once again, k5_): "configure hibernate to create a schema when it doesnt exist (just add the configuration option you posted to the persistence.xml). Start the server it will create a schema in a database. Use the liquibase to extract the schema. Or use it to create a diff over two databases."

How to create tables with hibernate recomandations

I'm relatively new to hibernate technology, and want to create an application with that. I have read that is not recomanded to use "hibernate's creation tabels" option (hibernate.hbm2ddl.auto) for a production environment. Does that mean that i need to create first tables and after that my POJOs classes or this recomandations is only for update value of hbm2ddl.auto?
Thank advance!
hibernate.hbm2ddl.auto is recommended for everything else except production. Especially for development. So feel free to use it.
Only when deploying on production use some database scripts to update your schema correctly.

IntelliJ IDEA Hibernate

I'm learning hibernate and I am running into some issues. I'm reading "Harnessing Hibernate" by O'Reilly. They explain everything using ANT, but since I want to avoid writing a huge build.xml file, I'm trying to get it to work with IntelliJ.
I managed to make a mapping according to a DB table in a MySQL database, and wrote the bean for it. It worked, but I can't find any information on how to generate beans and SQL code, or how to reverse engineer with IntelliJ. I found loads of tutorials about Eclipse, using JBOSS Hibernate tools plugin, and the site claims this support for generating code is already in the standard installation of IntelliJ.
Am I forgetting some configuration such as adding libraries? I'm trying to find this out but I'm desperate now. Please don't suggest me to use Eclipse, I need IntelliJ for my current role.
AFAIK, IntelliJ IDEA includes the complete JPA/Hibernate support in its Ultimate Edition:
Generating Persistence Mappings from Database Schema
IntelliJ IDEA allows you to quickly
generate persistence mappings from any
database schema: Generating
Persistance Mappings
(source: jetbrains.com)
Now, the question is, what edition of Intellij IDEA are you using?
If you add the hbm2ddl to your Hibernate config and ask it to create the database schema you'll get it by running a single test or some other code that exercises Hibernate. Once you have it, turn off create.
Let Hibernate do the work.

How to upgrade database schema built with an ORM tool?

I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. How do you do it in your projects?
The first solution that comes to my mind is to create my own mechanism for upgrading databases, with SQL scripts doing all the work. But in this case I'll have to remember about creating new scripts every time the object mappings are updated. And I'll still have to deal with low-level SQL queries, instead of just defining mappings and allowing the ORM tools to do all the job...
So the question is how to do it properly. Maybe some tools allow for simplifying this task (for example, I heard that Rails have such mechanism built-in), if so please help me decide which ORM tool to choose for my next Java project.
LiquiBase is an interesting open source library for handling database refactorings (upgrades). I have not used it, but will definitely give it a try on my next project where I need to upgrade a db schema.
I don't see why ORM generated schemas are any different to other DB schemas - the problem is the same. Assuming your ORM will spit out a generation script, you can use an external tool to do the diff
I've not tried it but google came back with SQLCompare as one option - I'm sure there are others.
We hand code SQL update scripts and we tear down the schema and rebuild it applying the update scripts as part of our continuous build process. If any hibernate mappings do not match the schema, the build will fail.
You can check this feature comparison of some database schema upgrade tools.
A comparison of the number of questions in SOW of some of those tools:
mybatis (1049 questions tagged)
Liquibase (663 questions tagged)
Flyway (400 questions tagged)
DBDeploy (24 questions tagged).
DbMaintain can also help here.
I think your best bet is to use an ORM-tool that includes database migration like SubSonic:
http://subsonicproject.com/2-1-pakala/subsonic-using-migrations/
We ended up making update scripts each time we changed the database. So there's a script from version 10 to 11, from 11 to 12, etc.. Then we can run any consecutive set of scripts to skip from some existing version to the new version. We stored the existing version in the database so we could detect this upon startup.
Yes this involved database-specific code! One of the main problems with Hibernate!
When working with Hibernate, I use an installer class that runs from the command-line and has options for creating database schema, inserting base data, and dynamically updating the database schema using SchemaUpdate. I find it to be extremely useful. It also gives me a place to put one-off scripts that will be run when a new version is launched to, for example, populate a new field in an existing DB table.

Categories

Resources