该评论实际上是不正确的。从很久以前开始,与“创建”相比,“创建-
删除”的真实行为是前者在关闭SessionFactory时会删除架构。“创建”本身会执行注释中所说的内容,即删除架构并在启动时重新创建。为了进行验证,将“
org.hibernate”日志记录设置为跟踪将显示使用“ create”或“ create-drop”删除该架构,然后在启动时创建该架构:
INFO - Running hbm2ddl schema exportDEBUG - import file not found: /import.sqlINFO - exporting generated schema to databaseTRACE - total checked-out connections: 0TRACE - using pooled JDBC connection, pool size: 0DEBUG - alter table Bar drop constraint FK103F39E150191DEBUG - Unsuccessful: alter table Bar drop constraint FK103F39E150191DEBUG - Table "BAR" not found; SQL statement:alter table Bar drop constraint FK103F39E150191 [42102-149]DEBUG - drop table Bar if existsDEBUG - drop table Foo if existsDEBUG - create table Bar (id integer generated by default as identity, foo_id integer, primary key (id))DEBUG - create table Foo (id integer generated by default as identity, primary key (id))DEBUG - alter table Bar add constraint FK103F39E150191 foreign key (foo_id) references FooINFO - schema export complete
但是,在关闭(SessionFactory.close())时,“创建”会给出
INFO - closingINFO - cleaning up connection pool: jdbc:h2:file:D:/dev/projects/testbed/test-db-for-hibernate-create-drop
而通过“ create-drop”,您会看到
INFO - closingINFO - Running hbm2ddl schema exportDEBUG - import file not found: /import.sqlINFO - exporting generated schema to databaseTRACE - total checked-out connections: 0TRACE - using pooled JDBC connection, pool size: 0DEBUG - alter table Bar drop constraint FK103F39E150191DEBUG - drop table Bar if existsDEBUG - drop table Foo if existsINFO - schema export completeTRACE - returning connection to pool, pool size: 1INFO - cleaning up connection pool: jdbc:h2:file:D:/dev/projects/testbed/test-db-for-hibernate-create-drop
您可以自己尝试:
git clone git@github.com:zzantozz/testbed tmpcd tmpmvn compile exec:java -Dexec.mainClass=rds.hibernate.Main -pl hibernate-create-drop



