问题
Up until the anorm included with play 2.3, I could write the following:
implicit val arbitraryClassToStatement = new ToStatement[ArbitraryClass] {
def set(
s: java.sql.PreparedStatement,
index: Int,
aValue: ArbitraryClass
)
: Unit = {
s.setString(
index,
ArbitraryClass.definingString
)
}
}
and this would help insert the
SQL("INSERT INTO SomeTable Values( {nonNullAc}, {possiblyNullAc} )" ).on(
'nonNullAc -> ArbitraryClass( "abcd" ),
'possiblyNullAc -> Option( ArbitraryClass( "abcd" ) )
)
meaning that both ArbitraryClass and Option[ ArbitraryClass ] would be satisfied by it. This seems to no longer be the case as I get the following error:
[error] found : (Symbol, Option[models.Misc.Url])
[error] (which expands to) (Symbol, Option[java.net.URL])
[error] required: anorm.NamedParameter
Can someone please point me to what's the right way to handle this? I'd want minimal duplication of code..
回答1:
You need to create a ParameterMetaData[ArbitraryClass]
for this to work
implicit object ArbitraryClassMetaData extends ParameterMetaData[ArbitraryClass] {
val sqlType = ParameterMetaData.StringParameterMetaData.sqlType
val jdbcType = ParameterMetaData.StringParameterMetaData.jdbcType
}
Here I just retook the values of ParameterMetaData[String]
来源:https://stackoverflow.com/questions/33809995/migrating-to-anorm2-4-with-play-2-4-tostatementt-and-tostatementoptiont