org.hibernate.exception.SQLGrammarException:无法插入[com.sample.Person]

时间:2012-02-10 11:51:17

标签: java hibernate orm hibernate-mapping

我正在尝试设置一个小型的Hibernate样本,我发现here但是当我运行代码时,我得到了以下错误

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.sample.Person]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at .....


Caused by: org.postgresql.util.PSQLException: ERROR: relation "person" 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:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 23 more

但是我已经在数据库中有一个名为person的表,这里是我修改过的hibernate.cfg.xml     

    

    <!-- hibernate dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>


    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost/testDB</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.show.sql" ></property> 
    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

    <!-- Automatic schema creation (begin) === -->
    <property name="hibernate.hbm2ddl.auto">create</property>


    <!-- Simple memory-only cache -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- ############################################ -->
    <!-- # mapping files with external dependencies # -->
    <!-- ############################################ -->

    <mapping resource="com/sample/Person.hbm.xml" />

</session-factory>

如果有人能指出我做错了什么会很棒,因为这是我第一次尝试Hibernate。 谢谢!

编辑:Person.hbm.xml     

<class name="com.sample.Person" table="person">

    <id name="id" column="ID">
        <generator class="native" />
    </id>

    <property name="name">
        <column name="NAME" length="16" not-null="true" />
    </property>

    <property name="surname">
        <column name="SURNAME" length="16" not-null="true" />
    </property>

    <property name="address">
        <column name="ADDRESS" length="16" not-null="true" />
    </property>

</class>

EDIT-II:日志文件的内容(Postgresql.log)

 2012-02-13 09:23:25 IST LOG:  database system was shut down at 2012-02-10 18:14:57 IST
 2012-02-13 09:23:25 IST FATAL:  the database system is starting up
 2012-02-13 09:23:33 IST LOG:  database system is ready to accept connections
 2012-02-13 09:23:38 IST LOG:  autovacuum launcher started
 2012-02-13 09:46:01 IST ERROR:  syntax error at or near "auto_increment" at character 41
    2012-02-13 09:46:01 IST STATEMENT:  create table person (ID bigint not null         auto_increment, NAME varchar(16) not null, SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID)) type=InnoDB
 2012-02-13 09:46:01 IST ERROR:  relation "person" does not exist at character 13
2012-02-13 09:46:01 IST STATEMENT:  insert into person (NAME, SURNAME, ADDRESS) values   ($1, $2, $3) RETURNING *
2012-02-13 09:46:01 IST LOG:  could not receive data from client: No connection could be made because the target machine actively refused it.


2012-02-13 09:46:01 IST LOG:  unexpected EOF on client connection
2012-02-13 09:48:15 IST ERROR:  syntax error at or near "auto_increment" at character 41
2012-02-13 09:48:15 IST STATEMENT:  create table person (ID bigint not null auto_increment, NAME varchar(16) not null, SURNAME varchar(16) not null, ADDRESS varchar(16) not null, primary key (ID)) type=InnoDB
2012-02-13 09:48:15 IST ERROR:  relation "person" does not exist at character 13
2012-02-13 09:48:15 IST STATEMENT:  insert into person (NAME, SURNAME, ADDRESS) values ($1, $2, $3) RETURNING *
 2012-02-13 09:48:15 IST LOG:  could not receive data from client: No connection could be made because the target machine actively refused it.


2012-02-13 09:48:15 IST LOG:  unexpected EOF on client connection

UPDATE :我刚注意到一些奇怪的东西,我在数据库中创建关系,然后运行这段代码,只是为了看到表被删除,因为当我运行它时它就消失了码;知道为什么会这样吗?

9 个答案:

答案 0 :(得分:8)

我通过修改hibernate.cfg.xml中的以下属性来解决错误

  <property name="hibernate.hbm2ddl.auto">validate</property>

早些时候,每次运行程序时表都会被删除,现在它没有,因为hibernate只验证模式并且不会影响对它的更改。

答案 1 :(得分:4)

根据异常,Hibernate想要写入“person”表,但是在你的hbm.xml中你定义了一个表“Person”,你确定数据库模式中存在正确的表吗? / p>

答案 2 :(得分:2)

注意:

  1. 当您创建与类名相同的表时,无需在Person.hbm.xml(........)中指定表名。也适用于领域。

  2. 在各自的数据库中创建“person”表时,请确保您在Person.hbm.xml中指定的任何FILEDS名称必须与表COLUMNS名称匹配ELSE,您将获得上述错误。

答案 3 :(得分:1)

我的问题是数据库名称不正确 我通过在字段中引用正确的数据库名称来解决问题,如下所示

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myDatabase</property>

答案 4 :(得分:0)

您似乎正在连接到错误的数据库。确定“jdbc:postgresql://localhost/testDB"会将您连接到实际数据源吗?

通常它们的格式为"jdbc://hostname/databasename".查看Postgresql日志文件。

答案 5 :(得分:0)

  

我通过修改hibernate.cfg.xml中的以下属性解决了错误

     
    

<property name="hibernate.hbm2ddl.auto">validate</property>

    早些时候,每次运行程序时表都被删除了,现在它没有,因为hibernate只验证模式并且不会影响对它的更改。

  

据我所知,您也可以从验证更改为更新,例如:

<property name="hibernate.hbm2ddl.auto">update</property>

答案 6 :(得分:0)

您可以尝试在配置文件中将正确的数据库名称放在连接URL中。因为我在运行POJO类文件时遇到了同样的错误,并且已经解决了这个问题。

答案 7 :(得分:0)

我通过修改数据库字符集来解决这个错误。旧的数据库字符集是cp1252并且我转移到utf-8

答案 8 :(得分:0)

org.hibernate.exception.SQLGrammarException我们的意思是什么?

实现JDBCException,指示发送到数据库服务器的SQL无效(语法错误,无效的对象引用等)。

在我的话中,Grammar配置文件中存在hibernate.cfg.xml错误,

当你在里面写错了架构定义属性名时会发生这种情况,如下例所示:

<property name="hibernate.connection.hbm2ddl.auto">create</property>

应该是这样的:

<property name="hibernate.hbm2ddl.auto">create</property>