java 如何在运行时在 Hibernate 中创建数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16801941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:02:19  来源:igfitidea点击:

How to create database in Hibernate at runtime?

javahibernate

提问by MAGx2

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.

我正在使用 Hibernate 租赁,每次用户登录时,我都会将数据库更改为他的用户名 (SQLite)。可悲的是,有时数据库不存在,我需要创建它。

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:

通常,Hibernete 会为我创建数据库:

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

采纳答案by Carsten

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.

您可以为此使用 SchemaExport 来在创建数据库后立即导出要在新创建的数据库中创建的实体。基本步骤如下。如何获取配置的属性并不重要。

    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

Javadocs 在这里: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

有关设置配置的选项,请查看此处。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

编辑:我想必须添加的一句话是,让休眠处理生产环境中的 DB/SCHEMA/TABLE 创建被认为是不好的做法。根据需要和可行性,为此保存准备好的 SQL 语句可能是更好的做法,或者甚至由数据库管理员手动执行。但由于我们都很懒惰,我想这不会经常发生。;D

回答by e2-e4

Did you try the value createinstead.

您是否尝试过该值create

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.

create 值将在 sessionFactory 创建时创建您的表,并保持它们不变。

create-drop 的值将创建您的表,然后在您关闭 sessionFactory 时删除它们。

That would be

那将是

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

More infornation hereand here
Or there could be a dialect problem

更多信息在这里这里
或者可能有方言问题

回答by Harsh

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

您可以通过createDatabaseIfNotExist=true在数据库连接 URL 中添加参数在运行时创建数据库。

For more information check the SO Link!

有关更多信息,请查看SO 链接