Created
August 11, 2012 06:53
-
-
Save tototoshi/3321920 to your computer and use it in GitHub Desktop.
Squeryl's KeyedEntityDef
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 org.squeryl.annotations.Column | |
import org.squeryl.PrimitiveTypeMode._ | |
import org.squeryl.{ Session, SessionFactory, Schema, KeyedEntityDef } | |
import org.squeryl.adapters.{ PostgreSqlAdapter } | |
import java.util.Date | |
object KeyedEntityDefsExample extends App { | |
class Member ( | |
val name: String, | |
val instrument: String | |
) { | |
override def toString = "Member(%s, %s, %s)".format(id, name, instrument) | |
val id: Int = 0 | |
} | |
object Beatles extends Schema { | |
implicit object BeatlesKED extends KeyedEntityDef[Member, Int] { | |
def getId(a: Member) = a.id | |
def isPersisted(a: Member) = a.id > 0 | |
def idPropertyName = "id" | |
} | |
val member = table[Member]("member") | |
} | |
Class.forName("org.postgresql.Driver"); | |
def session = Session.create( | |
java.sql.DriverManager.getConnection("jdbc:postgresql://localhost/beatles", "toshi", ""), | |
new PostgreSqlAdapter { | |
override def usePostgresSequenceNamingScheme: Boolean = true | |
} | |
) | |
transaction(session) { | |
val john = Beatles.member.insert(new Member("john", "guitar")) | |
val paul = Beatles.member.insert(new Member("paul", "bass")) | |
val george = Beatles.member.insert(new Member("George", "guitar")) | |
val ringo = Beatles.member.insert(new Member("Ringo", "drums")) | |
println(john) | |
println(paul) | |
println(george) | |
println(ringo) | |
/* | |
Member(1, john, guitar) | |
Member(2, paul, bass) | |
Member(3, George, guitar) | |
Member(4, Ringo, drums) | |
*/ | |
} | |
} |
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 table member ( | |
id serial primary key, | |
name varchar(30) not null, | |
instrument varchar(30) not null | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment