2012-02-10 130 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> 
相關問題