オブジェクトが自動生成された値の割り当てを処理するように、特定の場合User
を除いてすべての値を提供することによってオブジェクトを作成する機能が必要です。id
User
このために、以下に示すように、コンパニオンオブジェクトのapplyメソッドをオーバーロードしました。しかし、これはコンパイル時エラーを引き起こしています:value tupled is not a member of object
。
StackOverflowや他のブログで言及されているソリューションは機能していません:http://queirozf.com/entries/slick-error-message-value-tupled-is-not-a-member-of-object
case class User(id: Long, firstName: String, lastName: String, mobile: Long, email: String)
object User {
private val seq = new AtomicLong
def apply(firstName: String, lastName: String, mobile: Long, email: String): User = {
User(seq.incrementAndGet(), firstName, lastName, mobile, email)
}
}
class UserTableDef(tag: Tag) extends Table[User](tag, "user") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("first_name")
def lastName = column[String]("last_name")
def mobile = column[Long]("mobile")
def email = column[String]("email")
override def * =
(id, firstName, lastName, mobile, email) <> (User.tupled, User.unapply)
}
あなたの問題の原因は、その過負荷のapply
defです。
tupled
case class
'swithless than 2 parameters
またはoverloaded apply
。では機能しません。
slickの*
(またはすべての)マッピング<>
に関する限り、次のようになっているはずです。
def * = (tupleMember1, tupleMember2, ...) <> (func1, func2)
そのような、
func1
そのタプル(tupleMember1, tupleMember2, ...)
を入力として受け取り、マップされたクラス/ケースクラスのインスタンスを返します。func1
マップされたクラス/ケースクラスのインスタンスを取得し、そのタプルを返します(tupleMember1, tupleMember2, ...)
。したがって、これらの要件を満たす任意の機能を提供できます。
case class User(id: Long, firstName: String, lastName: String, mobile: Long, email: String)
object User {
private val seq = new AtomicLong
def apply(firstName: String, lastName: String, mobile: Long, email: String): User = {
User(seq.incrementAndGet(), firstName, lastName, mobile, email)
}
def mapperTo(
id: Long, firstName: String,
lastName: String, mobile: Long, email: String
) = apply(id, firstName, lastName, mobile, email)
}
class UserTableDef(tag: Tag) extends Table[User](tag, "user") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("first_name")
def lastName = column[String]("last_name")
def mobile = column[Long]("mobile")
def email = column[String]("email")
override def * =
(id, firstName, lastName, mobile, email) <> ((User.mapperTo _).tupled, User.unapply)
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加