Hibernate does not create Table in the database

前提是你 提交于 2020-01-30 11:23:09

问题


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:

<?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:

   <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:

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.

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"?

Any suggestions?


回答1:


The property is hibernate.hbm2ddl.auto, instead of hibernate.hbm2dll.auto.

DDL = Data Definition Language

DLL = Dynamically Loaded Library




回答2:


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#325596 Sorry for this disgression, but I think it could be useful.



来源:https://stackoverflow.com/questions/16638930/hibernate-does-not-create-table-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!