如果我错了,请纠正我!
通常,如果hbm2ddl.auto设置为" UPDATE",hibernate会检查表中是否存在表,如果不存在,则会创建该表并推送数据,如果表存在,则只是将数据添加到该表。
对于hbm2ddl.auto = update,我观察到的是,在每次插入之前,hibernate都试图创建表。虽然表存在,但它会在日志中抛出异常,然后hibernate会知道表的存在
这是正常的行为吗?或者我错过了什么?
请在控制台中找到下面的log4j帮助生成的日志。
Hibernate: create table UsersCC (userID int4 not null, userDOB date, userEmail varchar(255), userPswd varchar(255), primary key (userID))
23:02:27,799 WARN ExceptionHandlerLoggedImpl:27 - GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:524)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:470)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:273)
at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:203)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:110)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at com.hibernate.Setup.SetupTest.main(SetupTest.java:19)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "userscc" already exists
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2310)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2023)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:217)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:421)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:318)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:310)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
... 13 more
Hibernate: insert into UsersCC (userDOB, userEmail, userPswd, userID) values (?, ?, ?, ?)
23:02:27,998 TRACE BasicBinder:65 - binding parameter [1] as [DATE] - [Sun Jan 01 23:02:25 IST 2017]
23:02:27,999 TRACE BasicBinder:65 - binding parameter [2] as [VARCHAR] - [motogg@support.com]
23:02:28,000 TRACE BasicBinder:65 - binding parameter [3] as [VARCHAR] - [lenovo18]
23:02:28,004 TRACE BasicBinder:65 - binding parameter [4] as [INTEGER] - [101]
23:02:28,013 INFO pooling:230 - HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/campusCafeDb]
如果正常,那么它不是性能问题吗?
答案 0 :(得分:2)
hbm2ddl.auto验证: 如果值为validate,则hibernate仅验证表和列是否存在。如果该表不存在,那么hibernate会抛出异常。 Validate是hbm2ddl.auto的默认值。
hbm2ddl.auto更新: 如果值是update,则hibernate会检查表和列。如果表不存在则会创建一个新表,如果列不存在,则会为其创建新列。
此链接对所有值都有漂亮的解释。
http://www.onlinetutorialspoint.com/hibernate/hbm2ddl-auto-example-hibernate-xml-config.html
none
我认为是非官方的价值。我发现它在stackoverflow中的某个地方,但我忘了在哪里。