[轉] Hibernate不能自動建表解決辦法(hibernate.hbm2ddl.auto) (tables doesn't exist)

來源:互聯網
上載者:User

標籤: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.