[英]Is it possible to use both annotations and hbm.xml files in the same project in Hibernate?
是否必須在使用注釋和hibernate配置文件之間做出嚴格的選擇,或者是否可以在同一個項目中同時使用這兩種方式?
我們有一個使用Hibernate 3.2.5完成的大型項目和使用.hbm.xml
文件配置的POJO。 該項目使用Hibernate SessionManager
Factory而不是基於JTA的EntityManager
工廠。
這種方式配置了超過一百個POJO,原始開發團隊不願意切換到注釋。
同時我們需要使用更多的POJO來擴展項目,我們願意使用Hibernate注釋來配置這些POJO,並希望繼續使用Hibernate - 如果可能的話 - 而不是切換到JPA。
是否可以同時為某些POJO使用.hbm.xml
配置文件和其他POJO的注釋組合?
Manu給出了答案但是如果要切換到注釋並且想要將這些hbm.xml轉換為注釋。 請繼續。 您可以使用將使用現有表中的注釋生成pojo類的工具。 即使使用這些工具,你也不必改變其他代碼就像以前一樣生成pojos。 即不要改變屬性名稱。 我曾經在同一個項目上工作,我使用了Ant腳本。
我認為這篇文章很棒但是通過實際代碼示例它會更好。 我已經嘗試過ManuPK ref,這就是它的工作方式。
我的實際參考文獻在這里: http : //docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html#setup-configuration
//typical way to use cfg.xml
Configuration cfg = new Configuration();
SessionFactory f = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
Session s = f.openSession();
s.persist(new EntityObj());
//to use both annotation & cfg.xml mappings, use AnnotationConfiguration instead of Configuration
AnnotationConfiguration cfg = new AnnotationConfiguration();
SessionFactory f = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
Session s = f.openSession();
s.persist(new EntityObj());
AnnotationConfiguration將照常讀取cfg.xml以解析<mapping resource =“your_entity.hbm.xml”/>和<mapping class =“my.entity.objclass”/>
免責聲明 :我嘗試使用舊的Hibernate ver 3.2.1,它運行正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.