2012-04-03

Scala + Guice + Gradle: JPA 2 & EclipseLink (Part 4)

Previously I showed you how to create a Singleton in Guice powered Scala project. In this article I will show you how you can (JPA 2) EclipseLink support.

This sample app is a console app, so we have to handle entity management ourselves.

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="audioliciousPu">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

<class>me.m1key.audiolicious.domain.entities.Library</class>
<class>me.m1key.audiolicious.domain.entities.Artist</class>
<class>me.m1key.audiolicious.domain.entities.Album</class>
<class>me.m1key.audiolicious.domain.entities.Song</class>
<class>me.m1key.audiolicious.domain.entities.Stat</class>
<class>me.m1key.audiolicious.domain.entities.Rating</class>

<properties>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="eclipselink.jdbc.url"
value="jdbc:mysql://localhost:3306/audiolicious_test" />
<property name="eclipselink.jdbc.user" value="root" />
<property name="eclipselink.jdbc.password" value="" />
<property name="eclipselink.target-database" value="MYSQL" />
<property name="eclipselink.logging.level" value="OFF" />
<property name="eclipselink.orm.throw.exceptions" value="true" />
</properties>
</persistence-unit>
</persistence>

EntityManager

package me.m1key.audioliciousmigration.persistence
import javax.persistence.EntityManager
import javax.persistence.EntityManagerFactory
import javax.persistence.Persistence

class JpaPersistenceProvider extends PersistenceProvider {

 private var factory: EntityManagerFactory = _
 private var entityManager: EntityManager = _

 @Override
 def initialise: Unit = {
   factory = Persistence.createEntityManagerFactory("audioliciousPu")
   entityManager = factory.createEntityManager();

 }

 @Override
 def getEntityManager: EntityManager = {
   return entityManager
 }

 @Override
 def close: Unit = {
   entityManager.close();
   factory.close();
 }

}

Usage

val persistenceProvider = injector.getInstance(classOf[PersistenceProvider])
persistenceProvider.initialise
val entityManager = persistenceProvider.getEntityManager
entityManager.getTransaction().begin()

//...

entityManager.getTransaction().commit()
persistenceProvider.close

build.gradle

repositories {
    // ...
    mavenRepo name: "EclipseLink", url: "http://download.eclipse.org/rt/eclipselink/maven.repo/"
}

dependencies {
    compile group: 'me.m1key.audiolicious', name: 'audiolicious-domain-objects', version: '0.1.0-SNAPSHOT'
    compile group: 'org.eclipse.persistence', name: 'eclipselink', version: '2.3.0'
    compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.0.0'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.18'
    // …
}
We needed to explicitly add EclipseLink specific repository.

Conclusion

I did not present any entities here because in my app they come from another Java powered project. In the next article I will show you how to integration test this code.

Download source code

Source code for this article can be obtained via GitHub. Please see the README file for building and running.

No comments:

Post a Comment