I am using Java + Playframework + JPA using PlayFramework 2.3-M1 for developing application.
I want to use MySQL database when I run the aplication, but when I run test using
activator test
I want to use in memory database H2.
How can this be done ?
Below is my
conf/META-INF/persistence.xml
<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="prod" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>prodDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>TestDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
</persistence>
conf/application.conf
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
db.test.driver=org.h2.Driver
db.test.url="jdbc:h2:mem:play"
db.test.user=sa
db.test.password=""
db.test.jndiName=TestDS
jpa.test=testPersistenceUnit
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://X.X.X.X/lb"
db.default.user=lb
db.default.password="lb"
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled
# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"
Is this the best approach to get it done?
Thanks
So I used some mix/match approach for faster testing
Below are my files:
conf/test.conf
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:lb"
db.default.user=lb
db.default.password=""
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS
jpa.default=testPersistenceUnit
# Evolutions
# ~~~~~
# You can disable evolutions if needed
#evolutionplugin=disabled
conf/application.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://X.X.X.X/lb"
db.default.user=lb
db.default.password="lb"
#
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
# Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled
Then added following option to build.sbt file
build.sbt
javaOptions in Test += "-Dconfig.file=conf/test.conf"
Finally the persistence.xml file
<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 for Production-->
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
<!--Persistence Unit for Testing-->
<persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
</persistence>
Finally I test it using
activator test
Thanks to users at play-framework google group for leads into achieving this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With