Play Framework and autogenerated evolutions
我喜欢用Play引导项目! Ebean,在内存数据库中:当我需要一个新模型时,进化会自动生成,这很棒。
我正在学习Play Scala,Ebean和Scala之间缺乏支持
它可以与
但是案例类不支持作为Ebean模型。
我想知道,您是否知道以下ORM:
- 给定模型类自动生成mysql或postgresql模式
- 是Scala友好的(尤其是案例类)
- 简约样板(这是一个Ebean模型,不需要像任何存储库一样的其他文件)
我认为光滑或JPA不会产生变化吗? 我尝试了但没有成功。
PS:有了case类,您会从/到json隐含读取器和写入器,这也很棒。
我认为答案中提到的Slick ORM绝对是解决问题的方法。
光滑的ORM在scala中非常有效,因为您可以在此特定的ORM中使用诸如
http://slick.lightbend.com/doc/3.1.0/
假设您对ORM开放,我们可以轻松地继续使用
该文档特别在
http://slick.lightbend.com/doc/3.1.0/code-generation.html
但我将为您分解它,使其变得更加容易。对于
在您的项目中创建以下scala文件(您可以右键单击该文件以在IntelliJ中运行,如果不使用IntelliJ,则可能必须将其更改为可执行的Scala文件。人们还想出了一种通过sbt本身运行该文件的方法在编译时,但我不会进入那儿):
object SlickCodeGen {
def main(args: Array[String]): Unit = {
slick.codegen.SourceCodeGenerator.main(
Array("slick.jdbc.PostgresProfile",
"org.postgresql.Driver",
DATABASE_URL,
DIRECTORY_TO_PLACE_FILE,
PACKAGE,
USERNAME,
PASSWORD)
)
}
}
运行scala文件后,您将在先前指定的目录和包中看到一个名为
该文件包含对您来说最少且仅有必要的组件,因此,例如,对于链接中显示的诸如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package // AUTO-GENERATED Slick data model /** Stand-alone Slick data model for immediate use */ object Tables extends { val profile = slick.jdbc.PostgresProfile } with Tables /** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */ trait Tables { val profile: slick.jdbc.JdbcProfile import profile.api._ import slick.model.ForeignKeyAction // NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns. import slick.jdbc.{GetResult => GR} /** DDL for all tables. Call .create to execute. */ lazy val schema : profile.SchemaDescription = Computer.schema @deprecated("Use .schema instead of .ddl","3.0") def ddl = schema case class ComputerRow(name: String, introduced: Date, discontinued: Date, company: Company) /** GetResult implicit for fetching ComputerRow objects using plain SQL queries */ implicit def GetResultComputerRow(implicit e0: GR[String], e1: GR[Date], e2: GR[Company]): GR[ComputerRow] = GR { prs => import prs._ ComputerRow.tupled( (<<[String], <<[Date], <<[Date], <<[Company])) } /** Table description of table computer. Objects of this class serve as prototypes for rows in queries. */ class Computers(_tableTag: Tag) extends profile.api.Table[ComputerRow](_tableTag, None, "computer") { def * = (name, introduced, discontinued, company) <> (ComputerRow.tupled, ComputerRow.unapply) /** Maps whole row to an option. Useful for outer joins. */ def ? = (Rep.Some(name), Rep.Some(introduced), Rep.Some(discontinued), Rep.Some(company).shaped.<>( { r => import r._; _1.map( _ => ComputerRow.tupled( (_1.get, _2.get, _3.get, _4.get))) }, (_: Any) => throw new Exception("Inserting into ? projection not supported.") ) /** Database column name SqlType(text) */ val name: Rep[String] = column[String]("name", O.PrimaryKey) /** Database column introduced SqlType(date) */ val firstName: Rep[Date] = column[Date]("introduced") /** Database column discontinued SqlType(date) */ val lastName: Rep[Date] = column[Date]("discontinued") /** Database column company SqlType(text) */ val gender: Rep[Company] = column[Company]("company") } /** Collection-like TableQuery object for table Computer */ lazy val Computer = new TableQuery(tag => new Computer(tag)) } |
注意,这里的ComputerRow是一个case类。
综上所述,
我认为您在这里使用Slick不会出错。