Hibernate實踐異常記錄self

小序

或許網上搜羅下就能得到相關異常得解決方案了,個人相信這之後再遇到類似的異常,定會走同樣的路。既然如此,何不深刻的理解下並記錄下來,即使只是一些簡單的問題也好。[不積跬步無以成千裏]

異常記錄

org.hibernate.AnnotationException: No identifier specified for entity: Student

  • 對象關係映射的未加上主鍵 @Id
  • hibernate.cfg.xml 配置文件中未添加對 Student 對象的 < mapping > 標籤聲明

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned beforeQuery calling save():Student

  • “assigned”指示對象當中的主鍵未分配相應的屬性值,這裏我給 int 類型的主鍵加上自增註解 @GeneratedValue

org.hibernate.exception.SQLGrammarException: error performing isolated work

  • hibernate.cfg.xml 配置文件中添加< property name=”hibernate.hbm2ddl.auto” >validate< /property >

No default (no-argument) constructor for class: Student (class must be instantiated by Interceptor)

  • Student 對象須實現不帶參數的默認構造函數,不然無法完成 hibernate 數據映射

check the manual that corresponds to your MySQL server version for the rightonds to your MySQL server version for the right syntax to use near ‘type=MyISAM’ at line 7

  • MySQL 版本兼容問題,方言< property name=”dialect”>設置爲如下:
    • org.hibernate.dialect.MySQLInnoDBDialect(mysql5.0以前使用)
    • org.hibernate.dialect.MySQL5InnoDBDialect(mysq5.1-5.5)
    • org.hibernate.dialect.MySQLDialect(這個試了好像沒用)

org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]

org.hibernate.PropertyNotFoundException: Could not locate getter method for property [Address#mPostcode]

- 如異常內容提示可知,是屬性映射 getter 對應屬性出錯。後發現是屬性命名與 getter/setter 存在命名問題,如經設置“mPostcode”這個屬性在生成 getter/setter 後爲 getPostcode(),其正確寫法應該是 getmPostcode()。很奇怪這種情況對普通類型的屬性並沒有影響,這裏是組合屬性映射出現的問題。


java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

  • 需要將對應的 mysql-connector-java-*-bin.jar 包放置 Tomcat 的 lib 目錄下
  • 另外還要將該文件名配置於 jre 的 CLASS_PATH 路徑下
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章