Home

Awesome

Summary

MapperDao is an ORM library for the scala language and the following databases:

It allows ...(more)

News

...(more)

Quick Links

Example

import java.util.Properties
import org.apache.commons.dbcp.BasicDataSourceFactory

// create a datasource using apache dbcp
val properties = new Properties
properties.load(getClass.getResourceAsStream("/jdbc.test.properties"))
val dataSource = BasicDataSourceFactory.createDataSource(properties)

// create the mapperdao instance, connect to an oracle database and register our 2 entities
import com.googlecode.mapperdao.utils.Setup
val (jdbc,mapperDao,queryDao,txManager) = Setup.oracle(dataSource,List(PersonEntity,CompanyEntity))

// domain model classes (immutable)
class Person(val name: String, val company: Company)
class Company(val name: String)

// mappings (using default table and column naming convention)
object PersonEntity extends Entity[Int,SurrogateIntId, Person] {
	val id = key("id") autogenerated (_.id)
	val name = column("name") to (_.name)
	val company = manytoone(CompanyEntity) to (_.company)

	def constructor(implicit m) = new Person(name, company) with Stored {
		val id: Int = PersonEntity.id
	}
}

object CompanyEntity extends Entity[Int,SurrogateIntId, Company] {
	val id = key("id") autogenerated (_.id)
	val name = column("name") to (_.name)

	def constructor(implicit m) = new Company(name) with Stored {
		val id: Int = CompanyEntity.id
	}
}

val tx = Transaction.get(txManager, Propagation.Nested, Isolation.ReadCommited, -1) 

// insert a person
import mapperDao._
val person = new Person("Kostas", new Company("Coders limited"))

val inserted = tx { () => insert(PersonEntity, person) } // inserts person, company, in 1 transaction

// print the autogenerated id and the person name
println(s"${inserted.id} ${inserted.name}"))

// now update the company for this person
val company2 = insert(CompanyEntity, Company("Scala Inc"))
val modified = new Person(inserted.name, company2)
val updated = update(PersonEntity, inserted, modified) // no transaction here, but we could do the operation transactionally

// and select it from the database
val selected = select(PersonEntity, updated.id).get

// finally, delete the row
mapperDao.delete(PersonEntity, selected)

// run some queries
val pe=PersonEntity //alias
val people=query(select from pe) // get all
// people is a list of Person with IntId

// fetch only page 2 of all people
val people=query(QueryConfig.pagination(2, 10),select from pe)
// people is a list of Person with IntId

Roadmap

MapperDao would like to thank

YourKit Java Profiler and YourKit .NET Profiler.