Hibernate mapping resource located in a separate bank

I have a separate jar file that contains entity mapping and hibernation directly. My Hibernate confg (cgf.xml) is placed in another jar file. And as a result, I will catch the exception "resource: com / iceleads / data / Test.hbm.xml not found".

Example:

entities.jar com.package.entity.TestEntity.java com.package.entity.TestEnity.hbm.xml mainUsage.jar com.package.main.MainClass.java - there are I get session factory SessionFactory factory = HibernateUtil.getSessionFactory(); com.package.main.hibernate.cfg.xml in HibernateUtil sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); in hibernate.cfg.xml <mapping resource="com/package/entity/TestEntity/Test.hbm.xml"/> 

entity.jar in class path mainUsage.jar

Please suggest me how can I customize hibernate.cfg.xml to use a separate entity jar.

Thanks a lot!

Artyom

+4
source share
2 answers

Use the addJar() method when creating a new configuration.

 sessionFactory = new Configuration().configure("hibernate.cfg.xml") .addJar(new File("/path/to/jar")).buildSessionFactory(); 
+1
source

Include the mapping file path in the mapping resource. For example, use <mapping resource="com/example/test/test.hbm.xml"/> , and test.hbm.xml is in the com.example.test package inside the jar file.

This will serve the purpose.

0
source

Source: https://habr.com/ru/post/1344770/


All Articles