標籤:blog http io ar os sp java 檔案 資料
轉自: http://blog.csdn.net/biangren/article/details/8010018
最近開始學Hibernate,看的是李剛的那本《輕量級java ee公司專屬應用程式實戰》。頭一個hibernate程式,我原原本本的按照書上例子寫下來,同時只是改動了些mysql的串連參數,並且在mysql中建立了一個hibernate資料庫,僅此而已。然而預想不到的事情發生了……程式寫好之後,運行,報錯
Hibernate: insert into news_table (title, content) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [org.crazyit.app.domain.News]
…………此處省略
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘hibernate.news_table‘ doesn‘t exist
…………此處省略
當時我就蛋疼了……情況是這樣的,如果在mysql中把news_table表建好,然後運行成功,這說明串連肯定是沒問題的。可就是不能自動建表,百度啊,遇到同樣問題的人不少,按照他們所說的一個個去解決,可是還是沒有效果。csdn上也發帖問了,有人說是: <propertyname="hibernate.hbm2ddl.auto">update(create)</property>這個設定有問題,應該用update。好吧,我敢說我本來用的就是update,還是會出錯。。有人說是什麼mysql引擎不配對的緣故,好吧,我敢說<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>這樣設定,在mysql的設定檔ini中什麼引擎之類的也是innodb的(這個不太瞭解,隨便說點),當時我想問題肯定也不在這。還有人說,可能是你持久類的欄位設定成為關鍵字,這個更不靠譜了。。。。。
後來我想也許是版本相容問題,書上說的是用Hibernate3我用的是hibernate4。好吧我就重新下載hibernate3還是沒用。。。。。。就這樣,兩天下來,什麼各種方式都嘗試了,還是報一樣錯
最後實在沒法了,看了下hibernate的視頻教程,一步一步跟著做,每一個細節都不放過,嘗試著hibernate.cfg.xml和**.hbm.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">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">liaobin1992</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="org/crazyit/app/domain/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
更改之後的配置:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
把這行換成:
<property name="hibernate.dialect">org.hibernate.dialect.MySQDialect</property>
這樣就行了 ,然後整個世界都安靜了。。
雖然沒明白為什麼這麼做,但是終於解決了問題,還是有點點的欣慰。。。
哪位大俠路過能幫忙解釋下 ,這是為什嗎?
[轉] Hibernate不能自動建表解決辦法(hibernate.hbm2ddl.auto) (tables doesn't exist)