Created
May 24, 2012 02:38
-
-
Save ybiquitous/2779117 to your computer and use it in GitHub Desktop.
JPA2.0+Spring3.1+Scala2.9 Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies ++= Seq( | |
"org.springframework" % "spring-orm" % "3.1.1.RELEASE", | |
"cglib" % "cglib-nodep" % "2.2.2" % "runtime", | |
"javax.inject" % "javax.inject" % "1", | |
"org.hibernate" % "hibernate-entitymanager" % "4.1.3.Final", | |
"log4j" % "log4j" % "1.2.16" % "runtime", | |
"com.h2database" % "h2" % "1.3.166" % "runtime" | |
) | |
scalaVersion := "2.9.2" | |
scalacOptions ++= Seq("-deprecation", "-unchecked") | |
fork in run := true | |
javaOptions in run ++= Seq("-Dlog4j.configuration=log4j.properties") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jpa_spring_scala | |
import javax.persistence._ | |
import scala.collection.JavaConversions._ | |
trait Persistent[T, K] { | |
@PersistenceContext @transient | |
var entityManager: EntityManager = _ | |
def entity: T | |
def entityClass = entity getClass | |
def get(id: K): T = entityManager find (entityClass, id) | |
def save() { entityManager persist entity } | |
def remove() { entityManager remove entity } | |
def getAll: List[T] = entityManager | |
.createQuery("from " + entityClass.getName, entityClass) | |
.getResultList.toList | |
} | |
@Entity | |
@javax.inject.Named | |
class Person extends Persistent[Person, Int] { | |
@Id @GeneratedValue | |
var id: Int = _ | |
var name: String = _ | |
var age: Int = _ | |
def entity = this | |
override def toString = "%s{id=%s, name=%s, age=%s}" | |
.format(getClass.getSimpleName, id, name, age) | |
} | |
import org.springframework._ | |
import context.support._ | |
import context.annotation._ | |
@Configuration | |
@ComponentScan( | |
basePackages = Array("jpa_spring_scala"), | |
scopeResolver = classOf[Jsr330ScopeMetadataResolver] | |
) | |
class Config { | |
@Bean | |
def dataSource = new jdbc.datasource.DriverManagerDataSource("jdbc:h2:target/testdb") | |
@Bean | |
def entityManagerFactory = { | |
val res = new orm.jpa.LocalContainerEntityManagerFactoryBean | |
res setDataSource dataSource | |
res setPackagesToScan "jpa_spring_scala" | |
res setPersistenceProviderClass classOf[org.hibernate.ejb.HibernatePersistence] | |
res setJpaPropertyMap Map( | |
"hibernate.dialect" -> "org.hibernate.dialect.H2Dialect", | |
"hibernate.hbm2ddl.auto" -> "create-drop", | |
"hibernate.show_sql" -> "false" | |
) | |
res setJpaDialect new orm.jpa.vendor.HibernateJpaDialect | |
res | |
} | |
@Bean | |
def transactionManager = new orm.jpa.JpaTransactionManager(entityManagerFactory getObject) | |
@Bean | |
@Scope("prototype") | |
def transactionTemplate = new transaction.support.TransactionTemplate(transactionManager) | |
} | |
object Main extends App { | |
val context = new AnnotationConfigApplicationContext(classOf[Config]) | |
val person = context.getBean(classOf[Person]) | |
val tx = context.getBean(classOf[transaction.support.TransactionTemplate]) | |
tx.execute(new transaction.support.TransactionCallback[Any] { | |
def doInTransaction(status: transaction.TransactionStatus) = { | |
person.name = "Hoge" | |
person.age = 10 | |
person.save | |
person.age += 20 | |
person.save | |
person.name += " Aaaa" | |
person.save | |
println(person get person.id) | |
val person2 = context.getBean(classOf[Person]) | |
person2.name = "Foo" | |
person2.age = 23 | |
person2.save | |
} | |
}) | |
println(person getAll) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log4j.rootLogger=WARN, console | |
log4j.appender.console=org.apache.log4j.ConsoleAppender | |
log4j.appender.console.layout=org.apache.log4j.PatternLayout | |
log4j.appender.console.layout.ConversionPattern=%d [%t] %5p - %m | %c{2}%n | |
#log4j.logger.org.springframework.orm=DEBUG | |
#log4j.logger.org.springframework.transaction=DEBUG | |
log4j.logger.org.hibernate.SQL=DEBUG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment