2012-02-10 139 views
6

我想设置一个小型的休眠工作示例,我发现​​但是,当我运行代码时,我得到了以下错误org.hibernate.exception.SQLGrammarException:无法插入[com.sample.Person]

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 

但我已经有了由名字的人在数据库中的表,这里是我的修改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:我只注意到一些奇怪的事情,我在数据库中创建了关系,然后运行这段代码,只是为了看到表被删除,因为它只是在我运行此代码时消失;任何想法为什么会发生?

+0

你可以发布你的Person.hbm.xml吗? – 2012-02-10 11:52:29

+0

@SajanChandran,补充! – KodeSeeker 2012-02-10 11:54:31

回答

8

我通过在hibernate.cfg.xml

修改以下特性解决了这个错误
<property name="hibernate.hbm2ddl.auto">validate</property> 

早些时候,该表是获取每个我跑程序的时候删除,现在它不作为休眠仅验证架构,并且不影响改变它

4

根据例外情况,Hibernate希望写入表“person”,但在您的hbm.xml中,您定义了一个表“Person”,您确定数据库模式中存在正确的表吗?

+0

+1:可能存在这个问题,postgres在表名中可能有点奇怪,并且区分大小写。 – 2012-02-10 12:02:50

+0

@quaylar,我发布了我在Person.hbm.xml中所做的更正,但是错误仍然存​​在 – KodeSeeker 2012-02-10 12:03:08

+0

@KodeSeeker您可以打开数据库并检查P(p)erson-Table的名称吗? – quaylar 2012-02-10 12:28:40

0

看来你是连接到错误的数据库。 [Rü确保“jdbc:postgresql://localhost/testDB"将您连接到数据源的实际?

他们一般是形式"jdbc://hostname/databasename".查找到PostgreSQL的日志文件。

+0

呃..这只是明显错误。 – KodeSeeker 2012-02-10 12:13:52

+0

有帮助吗? – 2012-02-10 12:23:37

+0

它没有。而不能。它抛出一个错误说 - “无法打开连接.... 引起:: java.sql。SQLException:找不到适用于jdbc:// localhost/testDB的合适驱动程序 – KodeSeeker 2012-02-10 12:25:22

2

注:

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

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

1

我的问题是数据库名称不正确。
我通过如下

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

在现场指正确的数据库名称解决的问题,我通过在hibernate.cfg.xml

修改以下特性解决了这个错误<property name="hibernate.hbm2ddl.auto">validate</property>

此前,每次运行程序时表格都会被删除,并且没有它没有,因为休眠只验证模式,并不影响对它的更改。

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

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

tnx用于编辑我的帖子:) – Secondo 2014-09-26 06:49:11

0

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

0

我通过修改数据库charset.Old数据库字符集sovled这个错误是cp1252,我CONVER到utf-8

0

什么我们所说的org.hibernate.exception.SQLGrammarException

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

,并在我的文字里有一种你hibernate.cfg.xml配置文件内的Grammar错误,

当你写错架构认定中的属性名称里面,像下面的例子就发生了:

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

这应该是这样的:

<property name="hibernate.hbm2ddl.auto">create</property> 
相关问题