Java 在休眠启动时删除所有表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18342853/
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
Drop all tables at hibernate startup
提问by Chris
How can I configure hibernate in a way that it DROPS all tables before it runs, for developement purposes? I don't want to constantly DROP all tables by hand before running my code.
出于开发目的,如何以一种在运行之前删除所有表的方式配置休眠?我不想在运行我的代码之前不断手动删除所有表。
I'm already using
我已经在用
<property name="hbm2ddl.auto">create</property>
But this does not seem to work, since I ran into several errors that where caused by wrong schemas from a previous run.
但这似乎不起作用,因为我遇到了几个错误,这些错误是由上次运行的错误模式引起的。
EditMaybe my questions was a bit missleading. I want to have a clean database. Its not only because of actual runtime errors but also about having a clean database for every run.
编辑也许我的问题有点误导。我想要一个干净的数据库。这不仅是因为实际的运行时错误,还因为每次运行都有一个干净的数据库。
<property name="hbm2ddl.auto">create-drop</property>
Ignores tables that are unknown to hibernate (which can happen quite often if you are playing around with tablenames).
忽略休眠未知的表(如果您正在使用表名,这可能会经常发生)。
回答by Antoniossss
Use the value create-drop
instead of create
使用该值create-drop
而不是create
回答by dprogramz
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
回答by Salevajo
Another possibility to get an always clean database at startup in a development environment is an in memory db like H2 or similar. See http://www.h2database.com/html/features.html#in_memory_databasesfor details.
在开发环境中启动时获得始终干净的数据库的另一种可能性是像 H2 或类似的内存数据库。有关详细信息,请参阅http://www.h2database.com/html/features.html#in_memory_databases。
回答by Gene
You Java configuration file references the Database here:
您的 Java 配置文件在此处引用了数据库:
So the “postgres” part in “jdbc:postgresql://localhost:5432/postgres” of your configuration file refers to the red box I drew here in PostgreSQL/pgAdmin:
所以你的配置文件的“jdbc:postgresql://localhost:5432/postgres”中的“postgres”部分指的是我在PostgreSQL/pgAdmin这里画的红框:
I suggest you create a new Database and move the schemas and tables you don't want Hibernate to touch into that database.
我建议您创建一个新数据库并将您不希望 Hibernate 触及的模式和表移动到该数据库中。
As it is, every time hibernate connects to pgAdmin, it will access the database you set in your configuration file and the settings you have in your configuration file will apply to that database.
实际上,每次 hibernate 连接到 pgAdmin 时,它都会访问您在配置文件中设置的数据库,并且您在配置文件中的设置将应用于该数据库。
To create a new database in pgAdmin, right click on Databases and go to "New Database". Here is a picture:
要在 pgAdmin 中创建一个新数据库,请右键单击 Databases 并转到“New Database”。这是一张图片: