gpt4 book ai didi

database - Hibernate 不在数据库中创建表

转载 作者:行者123 更新时间:2023-11-29 14:03:30 24 4
gpt4 key购买 nike

我正在使用 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 应该做什么),错误仍然存​​在。我认为这是创建 session 或连接数据库时的问题。

PostgreSQL 是否需要配置以接受 Hibernate 连接?。问题是否来自 JNDI,如消息所说“未将工厂绑定(bind)到 JNDI,未配置 JNDI 名称”?

有什么建议吗?

最佳答案

属性是hibernate.hbm2ddl.auto,而不是hibernate.hbm2dll.auto

DDL = Data Definition Language

DLL = Dynamically Loaded Library

关于database - Hibernate 不在数据库中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16638930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com