2

Can anyone help me how can I refresh/update existing, and running hibernate/Spring application to a newer version?

To changethe source override the war in tomcat. In Hibernate persistence.xml hibernate.hbm2ddl.auto = check - and this not refresh the db structure, only check.

My problem is in database. In the second version of the app I got much more different entity/table strucutre.

How can I upgrade the existing structure and data to the other? Write SQL scripts, and exec it in database directly?

Is it any method for table upgradeing in java/hibernate?

Thanks

Barnabas
  • 39
  • 5
  • possible duplicate of [Hibernate question hbm2ddl.auto possible values and what they do](http://stackoverflow.com/questions/438146/hibernate-question-hbm2ddl-auto-possible-values-and-what-they-do) – NimChimpsky Apr 25 '12 at 10:09

3 Answers3

1

https://stackoverflow.com/a/1689769/106261

validate: validate the schema, makes no changes to the database.

update: update the schema.

create: creates the schema, destroying previous data.

create-drop: drop the schema at the end of the session.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
1

When you work with Hibernate, you can leverage hbm2ddl set in update or create, but this doesn't suite in real cases because Hibernate can't update your database as you want. For instance it can add column, but not remove or rename, so it's pretty limited, and in real life you'll probably need much more sophisticated cases when you issue a new version of your application.

In order to do it correctly, you'll need to use special tools for database migrations like flyway or liquibase. You'll need to setup your spring beans to initialize some class from these tools and specify your SQL scripts location, they will pick those SQL files and update database with those that are not there yet (so they won't re-write the same scripts twice).

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
  • 1
    Thanks a lot, flyway looks very good for me. I can use it in maven, or in spring, it looks really fine. Thanks. – Barnabas Apr 25 '12 at 15:28
0

Alternatively, if you don't want to use another tool like flyway or liquibase (which are great tools, btw), you can manually generate the scripts Hibernate would have applied with hibernate.hbm2ddl.auto=update with the following code:

LocalSessionFactoryBuilder sessionFactory = new LocalSessionFactoryBuilder(dataSource);
sessionFactory.scanPackages("your.package.containing.entities");
Dialect dialect = new MySQL5Dialect(); // select your dialect
DatabaseMetadata metadata = new DatabaseMetadata(dataSource.getConnection(), dialect, sessionFactory);
List<SchemaUpdateScript> scripts = sessionFactory.generateSchemaUpdateScriptList(dialect, metadata);

Formatter formatter = FormatStyle.DDL.getFormatter();
for (SchemaUpdateScript script : scripts) {
   System.err.println(formatter.format(script.getScript()) + ";");
}

Good luck!

darrachequesne
  • 742
  • 7
  • 18