I've been briefly looking at JPA recently, and I was wondering what the deal is with database schema migrations and staying lined up with the classes you've created.
Is there support in JPA for this stuff? Utilities? Best Practises?
Cheers!
I've been briefly looking at JPA recently, and I was wondering what the deal is with database schema migrations and staying lined up with the classes you've created.
Is there support in JPA for this stuff? Utilities? Best Practises?
Cheers!
The short answer is no.
If you change your beans, then you will have to migrate the existing schema by hand. So for Rails style database migrations you will have to look elsewhere.
You can however generate the initial ddl from your Java beans easily. The example below illustrates schema creation with EclipseLink version 2.0:
The key element here is
This tells EclipseLink to drop existing tables and generate new once from your JPA mapping. This procedure is highly vendor specific so for other JPA vendors (Hibernate, OpenJPA...) you will have to consult their specific documentation.
I won't rely on JPA providers to update the database schema. Check Liquibase for one of the good approaches.
If you set the
generateDdl
to Hibernate (if it is the underlying implementation), then it generates the database schema according to your current dialect. So after changing the dialect it will automatically generate the database.Other JPA providers may have different properties for this.