3

I'm using Hibernate tenancy and every time user logs I'm changing database to his username (SQLite). Sadly sometimes the database does not exists and I need to create it.

The problem is that I don't know how to create all tables in database at runtime.

Normally Hibernete creates for me db with this:

<property name="hibernate.hbm2ddl.auto">update</property>
MAGx2
  • 3,149
  • 7
  • 33
  • 63
  • Create a new `SessionFactory` from scratch just like you create the initial one? – millimoose May 28 '13 at 21:47
  • session = sessionFactory.withOptions().tenantIdentifier("tenancy_id").openSession(); <- but where I should add this? – MAGx2 May 28 '13 at 21:51
  • I don't think the database update runs when you open a session. It runs when you create a `SessionFactory`. When switching databases, you'll have to get rid of the old factory, and create it anew. I have no idea *where* to do this because I have no idea where you're doing it *now*. – millimoose May 28 '13 at 21:53
  • I hope it helps you. It works for me. http://stackoverflow.com/questions/21069687/hibernate-auto-create-database/26706954#26706954 – Jugal Panchal Nov 03 '14 at 02:34

3 Answers3

4

Did you try the value create instead.

A value of create will create your tables at sessionFactory creation, and leave them intact.

A value of create-drop will create your tables, and then drop them when you close the sessionFactory.

That would be

<property name="hibernate.hbm2ddl.auto">create</property>

More infornation here and here
Or there could be a dialect problem

Community
  • 1
  • 1
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
3

You can create database at run time by adding createDatabaseIfNotExist=true parameter in DB connection URL.

For more information check the SO Link !

Community
  • 1
  • 1
Harsh
  • 43
  • 6
2

You can use the SchemaExport for this to export the entities you want to create in your newly created database right after you created the database. the basic steps are below. How you get the properties for your config does not really matter.

    Configuration config = new Configuration();
    config.addAnnotatedClass(Class1.class);
    config.addAnnotatedClass(Class2.class);
    config.addAnnotatedClass(Class3.class);
    <set all hibernate properties/datasource here>
    SchemaExport schema = new SchemaExport(config);
    schema.create(true, true);

Javadocs are here: http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html

For options of setting upt the configuration look here. http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/cfg/Configuration.html

Edit: I guess a remark that has to be added is that it is considered bad practice to let hibernate handle DB/SCHEMA/TABLE creation in a production environment. Depending on the needs and the viability it might be better practice to save prepared SQL statements for this, or even do it manualy by a DB admin. But since we are all lazy I guess thats not often going to happen. ;D

Carsten
  • 1,511
  • 2
  • 13
  • 24
  • Can I get my classes from hibernate.cfg.xml? Like config.loadAnnotatedClassFromXML()? – MAGx2 May 29 '13 at 08:29
  • Yes. Just pick the method that suist you best from here: http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/cfg/Configuration.html. You probably want to look into addClass() or addFile(). – Carsten May 29 '13 at 08:54