簡體   English   中英

在Hibernate 4.2中從XML遷移到注釋配置

[英]Migration from XML to Annotation configuration in Hibernate 4.2

我一直使用hibernate.cfg.xml來配置Hibernate。 由於各種原因,我需要將所有配置都移至Java,並且映射實體存在問題。

以前在XML中,我的映射如下所示:

<mapping class="com.mycompany.enitites.Test" />

一切都是注釋驅動的,因此我不需要指定其他任何內容。

現在,我使用Hibernate的Configuration.class配置了所有內容,如何添加映射? 我嘗試了configuration.addClass(Test.class)但這引發了一個錯誤,即文件test.hbm.xml不存在。 而且我不想使用它,我希望Hibernate從注解中讀取映射。

編輯:我嘗試了configuration.addAnnotatedClass(Test.class)但在涉及Test.class實體的第一個遇到的查詢上,此引發relation "test" does not exist

我在以下幾行中犯了語法錯誤:

configuration.setProperty("hibernate.hbm2ddl.auto", "update");

這阻止了Hibernate在啟動時創建表並因此找不到它們...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM