標籤:
在使用註解進行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使用註解時的問題