0

I am developing a JPA application for my project. I am using eclipselink. I found this tutorial but it doesn't fit with the complexity of my application link here

I want to create an external file for the username, password and url of the database so that I will not change the persistence.xml file every time i change the username and password, or the hostname of the db.

This is the code I am working with

Map map = new HashMap();
map.put(PersistenceUnitProperties.JDBC_USER, "root");
map.put(PersistenceUnitProperties.JDBC_PASSWORD, "toor");
map.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://localhost:3306/sad?zeroDateTimeBehavior=convertToNull");
map.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");

the code above throws an exception

This is my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="SADPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>sad.entity.Credential</class>
<class>sad.entity.Customer</class>
<class>sad.entity.Employee</class>
<class>sad.entity.HourlyRate</class>
<class>sad.entity.Item</class>
<class>sad.entity.Product</class>
<class>sad.entity.ProductTransaction</class>
<class>sad.entity.Province</class>
<class>sad.entity.Tax</class>
<class>sad.entity.TransactionParticular</class>
<class>sad.entity.WorkingHours</class>
<class>sad.entity.Zero</class>
</persistence-unit>

After setting the persistence unit I created the entity manager with

Persistence.createEntityManagerFactory("SADPU", map);

I removed the properties tags to test if I can achieve what I want but it throws an exception

Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null]. Verify that you have set the expected driver class and URL. Check your login, persistence.xml or sessions.xml resource. The jdbc.driver property should be set to a class that is compatible with your database platform

Am I doing it wrong?

ltlynx
  • 51
  • 1
  • 10
  • 1
    The API only tells that the properties in you map will overwrite the properties from the configuration, but not that these properties can be left undefined in the configuration. Strange as it sounds, try to define some dummy properties in the configuration and try again; maybe it is just some configuration check kicking it ahead of time (it looks like more of a bug than a feature, if it is the case). – SJuan76 Oct 07 '12 at 18:37
  • Thanks for the reminder about the map. I found the error on my code. It wasn't the API's fault. I think I need to take rest. – ltlynx Oct 07 '12 at 19:01

1 Answers1

0

I managed to create a portable Controller class as follows :

public class SwagJpaController implements Serializable {

    public static final String PERSISTENCE_UNIT = "MyDBPU";

    public SwagJpaController(String database) {
        Map<String, String> properties = new HashMap<String, String>();
        properties.put("javax.persistence.jdbc.url", 
                "jdbc:mysql://localhost:3306/" + database + "?zeroDateTimeBehavior=convertToNull");
        this.emf = Persistence.createEntityManagerFactory(
                SwagJpaController.PERSISTENCE_UNIT, properties);
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
}

I would recommend placing the property keys as hard coded strings in you Map.put calls and seeing if that works.

Dawson
  • 4,391
  • 2
  • 24
  • 33