postgresql Hibernate不在数据库中创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16638930/
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
Hibernate does not create Table in the database
提问by arjacsoh
I am using PostgreSQL
and I am trying to run a simple Hibernate
application, in particular the application decribed in the page. My hibernate.cfg.xml file is:
我正在使用PostgreSQL
并且正在尝试运行一个简单的Hibernate
应用程序,特别是页面中描述的应用程序。我的 hibernate.cfg.xml 文件是:
<?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>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql:testDB2</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">user</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2dll.auto">create</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> -->
<mapping class="com.al.hibmaventest.Department"/>
<mapping class="com.al.hibmaventest.Employee"/>
</session-factory>
</hibernate-configuration>
My pom.xml
(when using maven) is:
我的pom.xml
(使用 Maven 时)是:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.al.hibmaventest</groupId>
<artifactId>HibMavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I use the main()
as it is in the link:
我使用main()
链接中的原样:
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("Sales");
session.persist(department);
Employee emp1 = new Employee("Nina", "Mayers", "111");
Employee emp2 = new Employee("Tony", "Almeida", "222");
emp1.setDepartment(department);
emp2.setDepartment(department);
session.persist(emp1);
session.persist(emp2);
session.getTransaction().commit();
session.close();
}
However it is impossible to get the application work, namely to do hibernate create the Tables in the Database and insert the value. I receive always, whatever I tried, the same error:
但是,不可能让应用程序工作,即在数据库中进行休眠创建表并插入值。无论我尝试过什么,我总是收到同样的错误:
INFO: Not binding factory to JNDI, no JNDI name configured
Hibernate: insert into Department (DEPT_NAME) values (?)
Mai 19, 2013 6:56:49 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 42P01
Mai 19, 2013 6:56:49 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ERROR: relation "department" does not exist
Position: 13
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.al.hibmaventest.Department]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2163)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2643)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at com.al.hibmaventest.Main.main(Main.java:25)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "department" does not exist
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 14 more
"Relation department does not exist" on line :
在线“关系部门不存在”:
session.persist(department);
I have tried it without maven
as well, exactly the same error. In order to preempt some readers, the Table (relation) "department" is never created in the database. Even if I create it manually in the Database(what is hibernate expected to do), the error remains the same. I think it is a problem in the creation of the sessions or in the connection withe database.
我也试过了maven
,完全一样的错误。为了抢占某些读者,数据库中从来没有创建表(关系)“部门”。即使我在数据库中手动创建它(hibernate 期望做什么),错误仍然相同。我认为这是创建会话或与数据库连接的问题。
Does PostgreSQL
need configuration to accept Hibernate
connection?. Can the problem come from the JNDI
as the message says "Not binding factory to JNDI, no JNDI name configured"?
是否PostgreSQL
需要配置才能接受Hibernate
连接?。问题是否来自JNDI
消息说“未将工厂绑定到 JNDI,未配置 JNDI 名称”?
Any suggestions?
有什么建议?
回答by JB Nizet
The property is hibernate.hbm2ddl.auto
, instead of hibernate.hbm2dll.auto
.
属性是hibernate.hbm2ddl.auto
,而不是hibernate.hbm2dll.auto
。
DDL = Data Definition Language
DLL = Dynamically Loaded Library
DDL = 数据定义语言
DLL = 动态加载的库
回答by Mitodina
The question has been correctly answered. By the way, I'd like to add something. You put
问题已得到正确回答。顺便说一下,我想补充一点。你把
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">user</property>
It's not a good option to change postgres password default (it carries security problems), and it isn't secure using postgres user as owner in a DB. For more information, see the following link https://serverfault.com/questions/110154/whats-the-default-superuser-username-password-for-postgres-after-a-new-install/325596#325596Sorry for this disgression, but I think it could be useful.
更改 postgres 密码默认值不是一个好的选择(它会带来安全问题),并且在数据库中使用 postgres 用户作为所有者并不安全。有关更多信息,请参阅以下链接https://serverfault.com/questions/110154/whats-the-default-superuser-username-password-for-postgres-after-a-new-install/325596#325596抱歉题外话,但我认为它可能有用。