質問

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..

役に立ちましたか?

解決

You cannot use placeholders or replacement variables in persistence.xml like you are trying to do. The properties you pass to JpaPersistModule are an atlernative to defining <property> elements in persistence.xml.

For example instead of having this line in persitence.xml:

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />

You can define the same property inside of your *.properties file:

javax.persistence.jdbc.driver=com.mysql.jdbc.Driver

So you should remove this invalid line:

<property name="javax.persistence.jdbc.user" value="${hibernate.username}" />

and add an entry like this in your properties file:

javax.persistence.jdbc.user=my_username
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top