use persistence.xml for authentication and URL in Play app using JPA

114 views
Skip to first unread message

Sean Van Buggenum

unread,
Jan 7, 2016, 8:56:54 AM1/7/16
to play-framework
Hi all,

I'm using JPA and Hibernate to persist my data. 
Implementing it as per the documentation I found on the play website works fine. 

However, I thought it would be nicer if I was able to consolidate most of the details back into the persistence.xml
(sounds more standard..... JPA like).

so, rather than have the url, username, and password filled out in the application.conf file, 
I added them to the persistence.xml file.,, leaving in the application.conf file only:


db
.default.jndiName=DefaultDS

jpa
.default=defaultPersistenceUnit



and my persistence.xml file looks like this:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
 
version="2.1">
 
<persistence-unit name="defaultPersistenceUnit"
 
transaction-type="RESOURCE_LOCAL">
 
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 
<non-jta-data-source>DefaultDS</non-jta-data-source>
 
<properties>
 
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
 
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
 
<!-- can be create/validate/update/create-drop -->
 
<property name="hibernate.hbm2ddl.auto" value="update" />
 
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/dplay1"/>
 
<property name="javax.persistence.jdbc.user" value="sa" />
 
<property name="javax.persistence.jdbc.password" value="sa" />
 
</properties>
 
</persistence-unit>
</persistence>



I had hoped that would be enough. 

However, I seemed to be wrong. 

Not only does it complain about authentication being wrong (if all I comment out is username/password) it doesn't like If I comment out the URL. Even though the url and authentication details are all in the persistence.xml. 

Is there anyway to get Play happy about the above?

Regards, 

Sean 




Matthias Kurz

unread,
Jan 7, 2016, 12:24:24 PM1/7/16
to play-framework
Just a guess:
Try 
<property name="javax.persistence.jdbc.username" value="sa" />
instead of
<property name="javax.persistence.jdbc.user" value="sa" />

Greetings,
Matthias

Sean Van Buggenum

unread,
Jan 8, 2016, 3:17:56 AM1/8/16
to play-framework
Nope. That ain't it. 
Every example I can find lists it as jdbc.user and jdbc.password, so I guess that is correct. 
Since i'm using JPA I need this persistence.xml. There's no way to do the whole setup within the application.conf, so I was hoping there was a way to move all the connection configuration to the persistence.xml, and have application.conf simply point to that. 
That is what I thought would be happening when I specify:

db.default.jndiName=DefaultDS

jpa
.default=defaultPersistenceUnit
Reply all
Reply to author
Forward
0 new messages