Java Hibernmate+;Postgres在第一次运行时创建表
我有一个小应用程序,如果不存在,我希望hibernate创建表。我已经完成了所有需要的注释(我想)和mi hibernate.cfg.xml,但是当我运行测试以查看dao方法是否正确时,hibernate会向我显示一个异常,说明关系不存在。当我检查数据库时,没有表 我想知道这是使hibernate自动创建表的一种方法,还是必须向代码中添加一些新代码或属性 hibernate.cfg.xmlJava Hibernmate+;Postgres在第一次运行时创建表,java,database,postgresql,hibernate,postgresql-9.3,Java,Database,Postgresql,Hibernate,Postgresql 9.3,我有一个小应用程序,如果不存在,我希望hibernate创建表。我已经完成了所有需要的注释(我想)和mi hibernate.cfg.xml,但是当我运行测试以查看dao方法是否正确时,hibernate会向我显示一个异常,说明关系不存在。当我检查数据库时,没有表 我想知道这是使hibernate自动创建表的一种方法,还是必须向代码中添加一些新代码或属性 hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- SQL Dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Connection Settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/tour_bi</property>
<property name="connection.username">xxx</property>
<property name="connection.password">xxx</property>
<property name="current_session_context_class">thread</property>
<!-- SQL to stdout -->
<property name="show_sql">true</property>
<!-- Update database on startup -->
<property name="hbm2dll.auto">create</property>
<!-- Annotated entity classes -->
<mapping class="domain.Car_Type"/>
<mapping class="domain.Car"/>
<mapping class="domain.Client"/>
<mapping class="domain.Country"/>
<mapping class="domain.Guide"/>
<mapping class="domain.Imagen"/>
<mapping class="domain.Languaje"/>
<mapping class="domain.Province"/>
<mapping class="domain.Stop"/>
<mapping class="domain.System"/>
<mapping class="domain.Tour_Type"/>
<mapping class="domain.System"/>
<mapping class="domain.Tour"/>
</session-factory>
</hibernate-configuration>
这是登录控制台
log4j:WARN Please initialize the log4j system properly.
06:06:29.838 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
06:06:31.101 [main] INFO org.hibernate.validator.util.Version - Hibernate Validator 4.0.2.GA
06:06:31.105 [main] DEBUG org.hibernate.validator.engine.ResourceBundleMessageInterpolator - ValidationMessages not found. Delegating to org.hibernate.validator.ValidationMessages
06:06:31.110 [main] DEBUG org.hibernate.validator.engine.resolver.DefaultTraversableResolver - Found javax.persistence.PersistenceUtil on classpath.
06:06:31.112 [main] INFO org.hibernate.validator.engine.resolver.DefaultTraversableResolver - Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
06:06:31.116 [main] DEBUG org.hibernate.validator.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only
06:06:31.196 [main] DEBUG org.hibernate.validator.engine.ResourceBundleMessageInterpolator - ValidationMessages not found. Delegating to org.hibernate.validator.ValidationMessages
06:06:31.196 [main] DEBUG org.hibernate.validator.engine.resolver.DefaultTraversableResolver - Found javax.persistence.PersistenceUtil on classpath.
06:06:31.197 [main] INFO org.hibernate.validator.engine.resolver.DefaultTraversableResolver - Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
06:06:31.203 [main] DEBUG org.hibernate.validator.xml.ValidationXmlParser - No META- INF/validation.xml found. Using annotation based configuration only
Creando Autos
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into car_type (active, type, _id) values (?, ?, ?)
没有什么说hibernate正在创建任何表,它完全可以创建对象
在我的测试日志中:
org.hibernate.exception.SQLGrammarException: ERROR: no existe la relación «car_type»
Position: 13
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at com.sun.proxy.$Proxy18.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2849)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3290)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:80)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:186)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:315)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at test.TestDB.createCarTYpe(TestDB.java:98)
at test.TestDB.crudTest(TestDB.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.postgresql.util.PSQLException: ERROR: no existe la relación «car_type»
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2505)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2241)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:447)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:368)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:158)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 43 more
我正在使用tomcat服务器9。我需要在我的tomcat上更新web.xml吗
更新此设置:
<property name="hbm2dll.auto">create</property>
创建
致:
更新
更新此设置:
<property name="hbm2dll.auto">create</property>
创建
致:
更新
谢谢你的回答。我已经尝试过更新,但是没有任何结果谢谢你的回答。我已经尝试过更新,但没有任何结果
<property name="hbm2dll.auto">create</property>
<property name="hbm2dll.auto">update</property>