Hibernate使用註解時的問題

來源:互聯網
上載者:User

標籤:

在使用註解進行Hibernate開發時、報了一個如下的錯誤

Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity: com.eyang.hibernate.model.Qygdat org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)at QygdTest.main(QygdTest.java:23)

經查證是在Model類中沒有給主鍵添加註解、也就是說在使用Hibernate進行開發時必須指定表的主鍵才能正常運行、

在對應屬性的get方法上添加@Id註解即可

 

另外還發現了一個現象、即使用xml設定檔和註解進行資料的添加時

若表中有10個欄位、Model中也有十個屬性與之對應、

在xml檔案中、我們可以唯寫其中的3個或者幾個欄位則產生的sql中只有對應的這幾個欄位的值、而沒有配置的則不會在sql中產生、

在使用註解時、Model中的十個屬性也會全部在sql中產生、

Hibernate使用註解時的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.