我正在使用scala play 2框架的项目中工作,我使用光滑的FRM和postgres数据库。
在我的项目中,客户是一个实体。所以我也创建了一个客户表和客户案例类和对象。另一个实体是帐户。代码如下所示
case class Customer(id: Option[Int],
status: String,
balance: Double,
payable: Double,
created: Option[Instant],
updated: Option[Instant]) extends GenericEntity {
def this(status: String,
balance: Double,
payable: Double) = this(None, status, balance, payable, None, None)
}
class CustomerTable(tag: Tag) extends GenericTable[Customer](tag, "customer"){
override def id = column[Option[Int]]("id")
def status = column[String]("status")
def balance = column[Double]("balance")
def payable = column[Double]("payable")
def account = foreignKey("fk_customer_account", id, Accounts.table)(_.id, onUpdate = ForeignKeyAction.Restrict, onDelete = ForeignKeyAction.Cascade)
def * = (id, status, balance, payable, created, updated) <> ((Customer.apply _).tupled, Customer.unapply)
}
object Customers extends GenericService[Customer, CustomerTable] {
override val table = TableQuery[CustomerTable]
val accountTable = TableQuery[AccountTable]
override def copyEntityFields(entity: Customer, id: Option[Int],
created: Option[Instant], updated: Option[Instant]): Customer = {
entity.copy(id = id, created = created, updated = updated)
}
}
现在问题是如何在客户对象中创建帐户对象?或者我如何获得客户的帐户?
答案 0 :(得分:3)
Slick不是ORM
它只是一个功能关系映射器。嵌套对象不能直接用于slick
Hibernate
或任何其他ORMs
而是
<强>油滑强>
//Usual ORM mapping works with nested objects
case class Person(name: String, age: Int, address: Address) //nested object
//With Slick
case class Address(addressStr: String, id: Long) //id primary key
case class Person(name: String, age: Int, addressId: Long) //addressId is the id of the address object and its going to the foreign key
客户中的帐户对象
case class Account(id: Long, ....) // .... means other fields.
case class Customer(accountId: Long, ....)
将accountId
放在Customer
对象中,并将其设为外键
有关详细信息,请参阅作为文档
的一部分提供的光滑示例