我正在使用PostgreSQL
,我正在尝试运行一个简单的Hibernate
应用程序,特别是page中描述的应用程序。我的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>
我的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>
我使用链接中的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();
}
然而,不可能让应用程序工作,即做hibernate在数据库中创建表并插入值。无论我怎样尝试,我都会收到同样的错误:
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
“关系部门不存在”在线:
session.persist(department);
我在没有maven
的情况下尝试了它,完全相同的错误。为了抢占一些读者,从不在数据库中创建表(关系)“部门”。即使我在数据库中手动创建它(hibernate期望做什么),错误仍然是相同的。我认为这是创建会话或与数据库连接时的问题。
PostgreSQL
是否需要配置才能接受Hibernate
连接?问题可能来自JNDI
,因为消息显示“未将工厂绑定到JNDI,没有配置JNDI名称”?
有什么建议吗?
答案 0 :(得分:12)
该属性为hibernate.hbm2ddl.auto
,而不是hibernate.hbm2dll.auto
。
DDL =数据定义语言
DLL =动态加载库
答案 1 :(得分:1)
这个问题已得到正确回答。顺便说一句,我想补充一些东西。你把
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">user</property>
更改postgres密码默认值(它带有安全问题)不是一个好的选择,并且使用postgres用户作为DB中的所有者是不安全的。有关详细信息,请参阅以下链接https://serverfault.com/questions/110154/whats-the-default-superuser-username-password-for-postgres-after-a-new-install/325596#325596 对此违规行为感到抱歉,但我认为这可能有用。