简体   繁体   中英

Persistence.xml file usage without container [packaging]

I am trying to use jpa/hibernate framework with a simple java console application without a container. According to jpa documentation the persistence.xml file should be placed under the folder META-INF on the src folder.

The issue came when trying to package the application as a simple jar file then the persitence.xml file will be within the jar file generated (since it should be placed on the src folder).

In this situation the persistence.xml file is not easy accessible to modify the application configuration like the DB URL, time out,the hibernate logs...

I tried to put the META-INF/persistence.xml outside the src folder and added it to the CLASSPATH but an error saying Could not find any META-INF/persistence.xml file in the classpath is always thrown.

Is there any way to keep the persistence.xml editable and accessible once the application is packaged and deployed on production like any other classic configuration file (eg .properties files).

Thank you

I think you don't need to make persistence.xml editable since you can provide additional properties when calling Persistence.createEntityManagerFactory() (or using similar Hibernate-specific mechanisms).

So, you can use persistence.xml for static configuration only, and configure dynamic properties using your own configuration mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM