我正在使用,PostgreSQL并且正在尝试运行一个简单的Hibernate应用程序,尤其是运行在page中描述的应用程序。我的hibernate.cfg.xml文件是:
PostgreSQL
Hibernate
<?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时)是:
pom.xml
<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()在链接中使用它:
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),该错误仍然相同。我认为这在创建会话或与数据库连接时存在问题。
maven
是否PostgreSQL需要进行配置才能接受Hibernate连接?JNDI当消息显示“未将工厂绑定到JNDI,未配置JNDI名称”时,问题是否可能出自?
JNDI
有什么建议么?
该属性是hibernate.hbm2ddl.auto,而不是hibernate.hbm2dll.auto。
hibernate.hbm2ddl.auto
hibernate.hbm2dll.auto
DDL =数据定义语言 DLL =动态加载的库
DDL =数据定义语言
DLL =动态加载的库