JPA persistence.xml

Is there any way to make data in persistence.xml dynamics? I was thinking about adding a database name property to my properties file, then tables are created if they do not exist.

Is it possible?

I am using EclipseLink (JPA2.0) and MySQL.

+4
source share
2 answers

If you use JPA in a standalone environment, you can pass additional properties to Persistence.createEntityManagerFactory() .

In application server environments, you can use a data source derived from JNDI.

+3
source

If you use spring, you can use the spring property-placeholder-configurer mechanism to do this. Just add the EclipseLink provider adapter:

 public class ExtendedJpaVendorAdapter extends XJpaVendorAdapter { private Map<String, Object> vendorProperties; @Override public Map<String, Object> getJpaPropertyMap() { Map<String, Object> properties = super.getJpaPropertyMap(); properties.putAll(vendorProperties); return properties; } public Map<String, Object> getVendorProperties() { return vendorProperties; } public void setVendorProperties(Map<String, Object> vendorProperties) { this.vendorProperties = vendorProperties; } } 

And then you can customize them in the spring xml file.

0
source

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


All Articles