4

Is there a way to make the data on the persistence.xml dynamic? I was thinking of adding a database name property on my properties file, then the tables are created, if not existing.

Is this possible?

I'm using EclipseLink(JPA2.0) and MySQL.

Cyril Horad
  • 1,555
  • 3
  • 23
  • 35

2 Answers2

3

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

In application server environments you can use datasource obtained from JNDI.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Can you supply more about the Persistence.createEntityManagerFactory(persitence_name, map_name)? – Cyril Horad Apr 11 '11 at 12:21
  • @Cyril: The point is that properties from the map override properties from the `persistence.xml`. So, you can pass your dynamic properties via that map. – axtavt Apr 11 '11 at 12:23
  • And the map should contain the the key of the property name of the persistence.xml exactly? – Cyril Horad Apr 11 '11 at 12:30
  • @axtavt: If I won't include the classes on the map, would it affect the the classes defined on the persistence.xml? – Cyril Horad Apr 11 '11 at 12:37
  • @Cyril: That map is only for properties, it doesn't affect classes. – axtavt Apr 11 '11 at 12:46
0

If you are using spring, you can use the spring property-placeholder-configurer mechanism to do so. Just extend your EclipseLink vendor 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 configure these in the spring xml file.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140