M Sach
M Sach

Reputation: 34424

hibernate.hbm2ddl.auto value to do both the operations i.e create/update on database schema?

i want to know for now, Is it possible in hibernate to create database schema if schema does not exist otherwise update the schema if it is already exist and there are some modification in hbm file with some value for property hibernate.hbm2ddl.auto .

As per my understanding create value will always drop the previous schema and create the new schema ? update value will not create new schema, it will just update the schem if exist a based on hbm.xml. Right?

Mine scheme is local schema not a production schema.

Upvotes: 2

Views: 1715

Answers (2)

zneo
zneo

Reputation: 598

You don't need to use SchemaExport, you can just use SchemaUpdate. It works great and you can review the sql before you choose to commit if you like.

You do need to create the database yourself, or use some jdbc for that. And then:

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

public static void updateDb(Configuration cfg, boolean printSqlToConsole, boolean commit)
{
    SchemaUpdate u = new SchemaUpdate(cfg);
    u.setFormat(printSqlToConsole);
    u.execute(printSqlToConsole, commit);
}

Note: that it cannot handle renamed/changed fields, you need to take care of that. But it can handle new fields. Fields that are not mapped are ignored.

Upvotes: 0

Dev
Dev

Reputation: 12196

The update value for hibernate.hbm2ddl.auto will indeed create new schema if it does not exist.

Upvotes: 1

Related Questions