I m using Guice-JPA module with hibernate to perform the DAO operations.
When hard coding the connection information in persistence.xml, everything works fine. but when I try to use properties file for connection parameters, persistence.xml just treats them as empty string and I get an exception.
here is my guice code and Persistense.xml.
JpaPersistModule jpaModule = new JpaPersistModule("myModule");
Properties properties = new Properties();
try {
properties.load(Bootstrap.class.getClassLoader().getResourceAsStream("appConfig.properties"));
} catch (IOException e) {
e.printStackTrace();
}
JpaPersistModule module = jpaModule.properties(properties);
injector = Guice.createInjector(new ApplicationConfig(), module);
PersistService persistService = injector.getInstance(PersistService.class);
persistService.start();
After this code, I confirmed from the debugger that the properties object carry all the properties, so I m sure it read the properties file correctly.
here is the properties file and persistence.xml
hibernate.username=root
<persistence 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"
version="2.0">
<persistence-unit name="24x7monitoring" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/dbname"/>
<property name="javax.persistence.jdbc.user" value="${hibernate.username}" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
I get the following exception:
Access denied for user ''@'localhost' to database
Please advise on why properties files are not being read by the JPA module, knowing that it works fine without the placeholders..