我以the following method call导入到Intellij的Play / Scala项目,由于我不了解的原因而导致编译错误。
此编译错误的原因是什么?
编译器不能选择正确的重载方法吗?
我正在使用Java 8,Scala 2.11.7,Play 2.4.3,JOOQ 3.7.1,SBT 0.13.18。
def receive = {
case UserRegistered(phoneNumber, userName, timestamp) =>
database.withTransaction { sql =>
sql.insertInto(TWITTER_USER)
.columns(TWITTER_USER.CREATED_ON, TWITTER_USER.PHONE_NUMBER, TWITTER_USER.TWITTER_USER_NAME)
//THE FOLLOWING METHOD CALL HAS A COMPILATION ERROR:
.values(new Timestamp(timestamp.getMillis), phoneNumber, userName)
.execute()
}
case ClientEvent(phoneNumber, userName, MentionsSubscribed(timestamp), _) =>
database.withTransaction { sql =>
sql.insertInto(MENTION_SUBSCRIPTIONS)
.columns(MENTION_SUBSCRIPTIONS.USER_ID, MENTION_SUBSCRIPTIONS.CREATED_ON)
.select(
select(TWITTER_USER.ID, value(new Timestamp(timestamp.getMillis)))
.from(TWITTER_USER)
.where(
TWITTER_USER.PHONE_NUMBER.equal(phoneNumber)
.and(
TWITTER_USER.TWITTER_USER_NAME.equal(userName)
)
)
).execute()
}
case ClientEvent(phoneNumber, userName, MentionReceived(id, created_on, from, text, timestamp), _) =>
database.withTransaction { sql =>
sql.insertInto(MENTIONS)
.columns(
MENTIONS.USER_ID,
MENTIONS.CREATED_ON,
MENTIONS.TWEET_ID,
MENTIONS.AUTHOR_USER_NAME,
MENTIONS.TEXT
)
.select(
select(
TWITTER_USER.ID,
value(new Timestamp(timestamp.getMillis)),
value(id),
value(from),
value(text)
)
.from(TWITTER_USER)
.where(
TWITTER_USER.PHONE_NUMBER.equal(phoneNumber)
.and(
TWITTER_USER.TWITTER_USER_NAME.equal(userName)
)
)
).execute()
}
}
您可以看到,方法调用.values(new Timestamp(timestamp.getMillis), phoneNumber, userName)
出现编译错误。错误输出为:
[info] Compiling 30 Scala sources and 1 Java source to /home/me/projects/book/CH07/target/scala-2.11/classes...
[error] /home/me/projects/book/CH07/app/actors/CQRSEventHandler.scala:21: overloaded method value values with alternatives:
[error] (x$1: org.jooq.Field[java.time.OffsetDateTime],x$2: org.jooq.Field[String],x$3: org.jooq.Field[String])org.jooq.InsertValuesStep3[generated.tables.records.TwitterUserRecord,java.time.OffsetDateTime,String,String] <and>
[error] (x$1: java.time.OffsetDateTime,x$2: String,x$3: String)org.jooq.InsertValuesStep3[generated.tables.records.TwitterUserRecord,java.time.OffsetDateTime,String,String]
[error] cannot be applied to (java.sql.Timestamp, String, String)
[error] .values(new Timestamp(timestamp.getMillis), phoneNumber, userName)
[error] ^
[error] /home/me/projects/book/CH07/app/actors/CQRSEventHandler.scala:31: type mismatch;
[error] found : org.jooq.SelectConditionStep[org.jooq.Record2[Long,java.sql.Timestamp]]
[error] required: org.jooq.Select[_ <: org.jooq.Record2[Long,java.time.OffsetDateTime]]
[error] .where(
[error] ^
[error] /home/me/projects/book/CH07/app/actors/CQRSEventHandler.scala:58: type mismatch;
[error] found : org.jooq.SelectConditionStep[org.jooq.Record5[Long,java.sql.Timestamp,String,String,String]]
[error] required: org.jooq.Select[_ <: org.jooq.Record5[Long,java.time.OffsetDateTime,String,String,String]]
[error] .where(
[error] ^
[error] /home/me/projects/book/CH07/app/actors/CQRSQueryHandler.scala:29: overloaded method value greaterOrEqual with alternatives:
[error] (x$1: org.jooq.QuantifiedSelect[_ <: org.jooq.Record1[java.time.OffsetDateTime]])org.jooq.Condition <and>
[error] (x$1: org.jooq.Select[_ <: org.jooq.Record1[java.time.OffsetDateTime]])org.jooq.Condition <and>
[error] (x$1: org.jooq.Field[java.time.OffsetDateTime])org.jooq.Condition <and>
[error] (x$1: java.time.OffsetDateTime)org.jooq.Condition
[error] cannot be applied to (org.jooq.Field[java.sql.Timestamp])
[error] MENTIONS.CREATED_ON.greaterOrEqual(currentDate().cast(PostgresDataType.TIMESTAMP))
[error] ^
[error] four errors found
[error] (compile:compileIncremental) Compilation failed
[error] application -
! @7fjed5mfe - Internal server error, for (GET) [/] ->
play.sbt.PlayExceptions$CompilationException: Compilation error[Overloaded method value [values] cannot be applied to (java.sql.Timestamp, String, String)]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
at scala.Option.map(Option.scala:145) ~[scala-library-2.11.7.jar:na]
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
at scala.Option.map(Option.scala:145) ~[scala-library-2.11.7.jar:na]
at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
org.jooq.InsertValuesStep3 (3.7.1)
中的重载方法:
package org.jooq;
import java.util.Collection;
import javax.annotation.Generated;
/**
* This type is used for the {@link Insert}'s DSL API.
* <p>
* Example: <code><pre>
* using(configuration)
* .insertInto(table, field1, field2, field3)
* .values(field1, field2, field3)
* .values(field1, field2, field3)
* .onDuplicateKeyUpdate()
* .set(field1, value1)
* .set(field2, value2)
* .execute();
* </pre></code>
*
* @author Lukas Eder
*/
@Generated("This class was generated using jOOQ-tools")
public interface InsertValuesStep3<R extends Record, T1, T2, T3> extends InsertOnDuplicateStep<R> {
/**
* Add values to the insert statement.
*/
@Support
InsertValuesStep3<R, T1, T2, T3> values(T1 value1, T2 value2, T3 value3);
/**
* Add values to the insert statement.
*/
@Support
InsertValuesStep3<R, T1, T2, T3> values(Field<T1> value1, Field<T2> value2, Field<T3> value3);
/**
* Add values to the insert statement.
*/
@Support
InsertValuesStep3<R, T1, T2, T3> values(Collection<?> values);
/**
* Use a <code>SELECT</code> statement as the source of values for the
* <code>INSERT</code> statement
* <p>
* This variant of the <code>INSERT .. SELECT</code> statement expects a
* select returning exactly as many fields as specified previously in the
* <code>INTO</code> clause:
* {@link DSLContext#insertInto(Table, Field, Field, Field)}
*/
@Support
InsertOnDuplicateStep<R> select(Select<? extends Record3<T1, T2, T3>> select);
}
重载消息令人困惑,但是真正的问题是代码正在传递java.sql.Timestamp
,其中需要java.time.OffsetDateTime
。它们是不兼容的类型。