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.

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.

    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete