Created
April 20, 2012 13:20
-
-
Save JorritPosthuma/2428506 to your computer and use it in GitHub Desktop.
Scala & Objectify4: http://thoughtsofthree.com/2012/04/scala-objectify4/
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
import com.googlecode.objectify.annotation | |
import scala.annotation.target.field | |
object Annotations { | |
type AlsoLoad = annotation.AlsoLoad @field | |
type Embed = annotation.Embed @field | |
type Id = annotation.Id @field | |
type Ignore = annotation.Ignore @field | |
type IgnoreLoad = annotation.IgnoreLoad @field | |
type IgnoreSave = annotation.IgnoreSave @field | |
type Index = annotation.Index @field | |
type Load = annotation.Load @field | |
type Mapify = annotation.Mapify @field | |
type Parent = annotation.Parent @field | |
type Serialize = annotation.Serialize @field | |
type Translate = annotation.Translate @field | |
type Unindex = annotation.Unindex @field | |
} |
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
// Create the company | |
val company = Company("Apple", Address("Infinite Loop", "1", "Cupertino", "United States")) | |
// Save it synchronous (by adding .now) | |
val company_key = Ofy.save.entity(company).now | |
// Create the User (with reference to the company) | |
val user = new User("steve.jobs@apple.com", "iPassword", "Steve Jobs", Ref.create(company_key)) | |
// Save it synchronous | |
Ofy.save.entity(user).now | |
// Retrieve based on the email-adress | |
val get = Ofy.load.`type`(classOf[User]).id(user.email).get | |
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
val get = Ofy.load.kind(classOf[User]).id(user.email).get |
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
LoaderWrapper | |
ObjectifyWrapper | |
QueryResultIteratorWrapper | |
QueryWrapper | |
SimpleQueryWrapper | |
TransactionWrapper | |
ResultWrapper | |
SimpleFutureWrapper |
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
object Ofy extends Ofy { | |
ObjectifyService.register(classOf[User]) | |
ObjectifyService.register(classOf[Company]) | |
protected val _conn = new DynamicVariable[Objectify](null) | |
def ofy: Objectify = { | |
if (_conn.value == null) { | |
_conn.value = ObjectifyService.factory.begin() | |
_conn.value.setWrapper(this) | |
} | |
_conn.value | |
} | |
} |
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
trait Ofy extends Objectify { | |
def ofy: Objectify | |
def load: ScalaLoader = new ScalaLoader(ofy.load) | |
def save: Saver = ofy.save | |
def delete: Deleter = ofy.delete | |
def getTxn: Transaction = ofy.getTxn | |
def getFactory: ObjectifyFactory = ofy.getFactory | |
def consistency(policy: Consistency): Ofy = new OfyWrapper(ofy.consistency(policy)) | |
def deadline(value: java.lang.Double): Ofy = new OfyWrapper(ofy.deadline(value)) | |
def cache(value: Boolean): Ofy = new OfyWrapper(ofy.cache(value)) | |
def transaction(): Ofy = new OfyWrapper(ofy.transaction()) | |
def transactionless(): Ofy = new OfyWrapper(ofy.transactionless()) | |
def transact[O <: Objectify, R](work: TxnWork[O, R]): R = ofy.transact(work) | |
def transact[O <: Objectify, R](limitTries: Int, work: TxnWork[O, R]): R = ofy.transact(limitTries, work) | |
def clear = ofy.clear | |
def setWrapper(ofy: Objectify) = ofy.setWrapper(ofy) | |
def toEntity(pojo: Object): Entity = ofy.toEntity(pojo) | |
def toPojo[T](entity: Entity) : T = ofy.toPojo(entity) | |
} |
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
class OfyWrapper(val base: Objectify) extends Ofy { | |
base.setWrapper(this) | |
override def ofy: Objectify = base | |
} |
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
class ScalaLoader(val loader: Loader) extends LoaderWrapper[ScalaLoader](loader) { | |
def kind[E](kind: Class[E]): LoadType[E] = loader.`type`(kind) | |
} |
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
@Entity | |
case class User( | |
@Id var email: String, | |
@Index var password: String, | |
var name: String, | |
var company: Ref[Company] | |
) { | |
// Only for Objectify creation | |
private def this() { this(null, null, null, null) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code for http://thoughtsofthree.com/2012/04/scala-objectify4/