Skip to content

Commit 3827c83

Browse files
author
anonymous
committed
Initial commit
0 parents  commit 3827c83

19 files changed

+340
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Extracted from https://github.com/ulrich/macaron-factory/blob/master/.gitignore
2+
# Ignore all dotfiles...
3+
.*
4+
# except for .gitignore
5+
!.gitignore
6+
7+
# Ignore Play! working directory #
8+
/db
9+
/eclipse
10+
/lib
11+
/log
12+
/logs/
13+
/modules
14+
/precompiled
15+
/project/project/
16+
/project/target/
17+
/target/
18+
/tmp
19+
test-result
20+
server.pid
21+
*.iml
22+
*.eml
23+
activator*.sbt

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2013 Typesafe, Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
play-scala-spring-data-jpa
2+
====================
3+
4+
A sample application demonstrating Play Framework using Spring Data JPA with Hibernate as persistence provider.
5+
6+
Used technologies:
7+
8+
* Play Framework 2.3.9
9+
* Scala
10+
* Spring Core 4
11+
* Spring Data JPA 4
12+
* Hibernate 4
13+
14+
Based on the play-java version: [https://github.com/huntc/play-spring-data-jpa](https://github.com/huntc/play-spring-data-jpa)

activator.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name=play-scala-spring-data-jpa
2+
title=Play Framework using Scala, Spring Data JPA
3+
description=A starter application with Play Framework, Scala and Spring Data JPA with Hibernate
4+
tags=playframework,scala,spring,jpa,hibernate

app/Global.scala

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import javax.persistence.{Persistence, EntityManagerFactory}
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext
4+
import org.springframework.orm.hibernate3.HibernateExceptionTranslator
5+
import org.springframework.orm.jpa.JpaTransactionManager
6+
import play.api.{Application, GlobalSettings}
7+
import org.springframework.context.annotation.Configuration
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
9+
import org.springframework.context.annotation.Bean
10+
11+
/**
12+
* Application wide behaviour. We establish a Spring application context for the dependency injection system and
13+
* configure Spring Data.
14+
*/
15+
object Global extends GlobalSettings {
16+
17+
/**
18+
* Declare the application context to be used. AnnotationConfigApplicationContext cannot be refreshed so we can
19+
* only do this once. We tell the context what packages to scan and then to refresh itself.
20+
*/
21+
val ctx = new AnnotationConfigApplicationContext
22+
// ctx.scan("controllers", "services")
23+
// ctx.refresh()
24+
25+
/**
26+
* Sync the context lifecycle with Play's.
27+
*/
28+
override def onStart(app: Application) {
29+
super.onStart(app)
30+
31+
// AnnotationConfigApplicationContext can only be refreshed once, but we do it here even though this method
32+
// can be called multiple times. The reason for doing during startup is so that the Play configuration is
33+
// entirely available to this application context.
34+
ctx.register(classOf[SpringDataJpaConfiguration])
35+
ctx.scan("controllers", "models")
36+
ctx.refresh
37+
38+
39+
ctx.start()
40+
}
41+
42+
/**
43+
* Sync the context lifecycle with Play's.
44+
* @param app
45+
*/
46+
override def onStop(app: Application) {
47+
// This will call any destruction lifecycle methods and then release the beans e.g. @PreDestroy
48+
ctx.close
49+
50+
super.onStop(app)
51+
}
52+
53+
/**
54+
* Controllers must be resolved through the application context. There is a special method of GlobalSettings
55+
* that we can override to resolve a given controller. This resolution is required by the Play router.
56+
* @param controllerClass
57+
* @tparam A
58+
* @return
59+
*/
60+
override def getControllerInstance[A](controllerClass: Class[A]): A = ctx.getBean(controllerClass)
61+
}
62+
@Configuration
63+
@EnableJpaRepositories(Array("models"))
64+
class SpringDataJpaConfiguration {
65+
@Bean def entityManagerFactory: EntityManagerFactory = {
66+
return Persistence.createEntityManagerFactory("default")
67+
}
68+
69+
@Bean def hibernateExceptionTranslator: HibernateExceptionTranslator = {
70+
return new HibernateExceptionTranslator
71+
}
72+
73+
@Bean def transactionManager: JpaTransactionManager = {
74+
return new JpaTransactionManager
75+
}
76+
}

app/controllers/Application.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package controllers
2+
3+
import javax.inject.Inject
4+
import javax.inject.Named
5+
6+
import models.{Person, PersonRepository}
7+
import play.api.mvc.{Action, Controller}
8+
9+
/**
10+
* The main set of web services.
11+
*/
12+
@Named
13+
class Application @Inject() (personRepository: PersonRepository) extends Controller {
14+
def index = Action {
15+
val person: Person = new Person
16+
person.firstname = "Bruce"
17+
person.surname = "Smith"
18+
val savedPerson: Person = personRepository.save(person)
19+
val retrievedPerson: Person = personRepository.findOne(savedPerson.id)
20+
Ok(views.html.index.render("Found id: " + retrievedPerson.id + " of person/people"))
21+
}
22+
}

app/models/Person.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package models
2+
3+
import javax.persistence.Entity
4+
import javax.persistence.GeneratedValue
5+
import javax.persistence.Id
6+
7+
/**
8+
* This declares a model object for persistence usage. Model objects are generally anaemic structures that represent
9+
* the database entity. Behaviour associated with instances of a model class are also captured, but behaviours
10+
* associated with collections of these model objects belong to the PersonRepository e.g. findOne, findAll etc.
11+
* Play Java will synthesise getter and setter methods for us and therefore keep JPA happy (JPA expects them).
12+
*/
13+
@Entity
14+
class Person {
15+
@Id
16+
@GeneratedValue
17+
var id: Long = _
18+
var firstname: String = _
19+
var surname: String = _
20+
}

app/models/PersonRepository.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package models
2+
3+
import org.springframework.data.repository.CrudRepository
4+
import javax.inject.Named
5+
import javax.inject.Singleton
6+
7+
/**
8+
* Provides CRUD functionality for accessing people. Spring Data auto-magically takes care of many standard
9+
* operations here.
10+
*/
11+
@Named
12+
@Singleton
13+
trait PersonRepository extends CrudRepository[Person, java.lang.Long] {}

app/views/index.scala.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@(message: String)
2+
3+
@main("Welcome to Play 2.3") {
4+
5+
@play20.welcome(message)
6+
7+
}

app/views/main.scala.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@(title: String)(content: Html)
2+
3+
<!DOCTYPE html>
4+
5+
<html>
6+
<head>
7+
<title>@title</title>
8+
<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
9+
<link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
10+
<script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
11+
</head>
12+
<body>
13+
@content
14+
</body>
15+
</html>

build.sbt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name := "play-scala-spring-data-jpa"
2+
3+
version := "1.0-SNAPSHOT"
4+
5+
libraryDependencies ++= Seq(
6+
javaJpa,
7+
// "org.springframework" % "spring-context" % "3.2.2.RELEASE",
8+
"javax.inject" % "javax.inject" % "1",
9+
"org.springframework.data" % "spring-data-jpa" % "1.8.0.RELEASE",
10+
"org.springframework" % "spring-core" % "4.1.4.RELEASE",
11+
"org.hibernate" % "hibernate-core" % "4.3.9.Final",
12+
"org.hibernate" % "hibernate-entitymanager" % "4.3.9.Final",
13+
// "org.mockito" % "mockito-core" % "1.9.5" % "test",
14+
// "mysql" % "mysql-connector-java" % "5.1.35"
15+
"com.h2database" % "h2" % "1.4.187"
16+
)
17+
18+
lazy val root = (project in file(".")).enablePlugins(PlayScala)
19+
20+
// Possibly causes the error java.util.concurrent.TimeoutException: Futures timed out after [300000 milliseconds]. Disable if so.
21+
fork in run := true

conf/META-INF/persistence.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
4+
version="2.0">
5+
6+
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
7+
<provider>org.hibernate.ejb.HibernatePersistence</provider>
8+
<non-jta-data-source>DefaultDS</non-jta-data-source>
9+
<properties>
10+
<!--<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>-->
11+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
12+
<property name="hibernate.hbm2ddl.auto" value="update"/>
13+
</properties>
14+
</persistence-unit>
15+
16+
</persistence>

conf/application.conf

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# This is the main configuration file for the application.
2+
# ~~~~~
3+
4+
# Secret key
5+
# ~~~~~
6+
# The secret key is used to secure cryptographics functions.
7+
# If you deploy your application to several instances be sure to use the same key!
8+
application.secret="GL/hbPrxueeoWtuqp^Z0P_lSs8wKDmuRiYfApHQQ]qDtRbxCebbJ[Jt/Hm?L8HoB"
9+
10+
# The application languages
11+
# ~~~~~
12+
application.langs="en"
13+
14+
# Global object class
15+
# ~~~~~
16+
# Define the Global object class for this application.
17+
# Default to Global in the root package.
18+
# application.global=Global
19+
20+
# Router
21+
# ~~~~~
22+
# Define the Router object to use for this application.
23+
# This router will be looked up first when the application is starting up,
24+
# so make sure this is the entry point.
25+
# Furthermore, it's assumed your route file is named properly.
26+
# So for an application router like `conf/my.application.Router`,
27+
# you may need to define a router file `my.application.routes`.
28+
# Default to Routes in the root package (and `conf/routes`)
29+
# application.router=my.application.Routes
30+
31+
# Database configuration
32+
#
33+
db.default.driver=org.h2.Driver
34+
#db.default.driver=com.mysql.jdbc.Driver
35+
db.default.url="jdbc:h2:mem:play"
36+
#db.default.url="jdbc:mysql://localhost:3306/my-database-name"
37+
#db.default.user="my-username...."
38+
#db.default.password="my-password...."
39+
db.default.jndiName=DefaultDS
40+
41+
# Evolutions
42+
# ~~~~~
43+
# You can disable evolutions if needed
44+
evolutionplugin=disabled
45+
46+
# Logger
47+
# ~~~~~
48+
# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
49+
50+
# Root logger:
51+
logger.root=INFO
52+
53+
# Logger used by the framework:
54+
logger.play=INFO
55+
56+
# Logger provided to your application:
57+
logger.application=DEBUG
58+
59+
# Configure our user-code thread pool to something similar to servlets
60+
play {
61+
akka {
62+
actor {
63+
default-dispatcher = {
64+
fork-join-executor {
65+
parallelism-factor = 1.0
66+
parallelism-max = 200
67+
}
68+
}
69+
}
70+
}
71+
}

conf/evolutions/default/1.sql

Whitespace-only changes.

conf/routes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Routes
2+
# This file defines all application routes (Higher priority routes first)
3+
# ~~~~
4+
5+
# Home page - we use the @controllers syntax here so that the router knows we are referring to a class. In turn it
6+
# will call on the GlobalSettings.getController method to resolve the actual controller instance.
7+
GET / @controllers.Application.index
8+
9+
# Map static resources from the /public folder to the /assets URL path
10+
GET /assets/*file controllers.Assets.at(path="/public", file)

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.5

project/play-fork-run.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This plugin adds forked run capabilities to Play projects which is needed for Activator.
2+
3+
addSbtPlugin("com.typesafe.play" % "sbt-fork-run-plugin" % "2.3.9")

project/plugins.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Comment to get more information during initialization
2+
logLevel := Level.Warn
3+
4+
// The Typesafe repository
5+
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
6+
7+
// Use the Play sbt plugin for Play projects
8+
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.9")

project/sbt-ui.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This plugin represents functionality that is to be added to sbt in the future
2+
3+
addSbtPlugin("org.scala-sbt" % "sbt-core-next" % "0.1.1")

0 commit comments

Comments
 (0)