Form fill logic throwing UnsupportedOperationException for sql date

366 views
Skip to first unread message

Benjamin Black

unread,
Oct 31, 2017, 5:06:18 PM10/31/17
to Play Framework
Hello,

I have a form and filling it with data:

case class Something(id: Long, date: java.util.Date)

val form = Form(
mapping(
"id" -> ignored(-1L),
"date" -> date
)(Something.apply)(Something.unapply))

form.fill(Something(0, new java.sql.Date(123l)))

Which throws an exception:

Caused by: java.lang.UnsupportedOperationException: null
at java.sql.Date.toInstant(Date.java:304)
at play.api.data.format.Formats$$anon$7.unbind(Format.scala:211)
at play.api.data.format.Formats$$anon$7.unbind(Format.scala:198)
at play.api.data.FieldMapping.unbind(Form.scala:913)
at play.api.data.ObjectMapping2$$anonfun$unbind$3.apply(ObjectMappings.scala:158)
at play.api.data.ObjectMapping2$$anonfun$unbind$3.apply(ObjectMappings.scala:156)
at scala.Option.map(Option.scala:146)
at play.api.data.ObjectMapping2.unbind(ObjectMappings.scala:156)
at play.api.data.Form.fill(Form.scala:113)
at controllers.Clients$$anonfun$edit$1$$anonfun$apply$4.apply(Clients.scala:175)
at controllers.Clients$$anonfun$edit$1$$anonfun$apply$4.apply(Clients.scala:159)
at scala.concurrent.Future$$anonfun$flatMap$1.apply(Future.scala:253)
... 7 common frames omitted

I encountered this when upgrading from 2.5 to 2.6.6 and was a little confused how this could happen until I realized that sql date extends java date. To fix I've had to convert sql date into java date when pulling from the database:

new Date(resultSet.getDate(5).getTime)

Seems like a bug to me?

Thanks,
Ben

Marcos Pereira

unread,
Nov 2, 2017, 11:08:26 PM11/2/17
to play-fr...@googlegroups.com
Hi Ben,

This should be related to issue #7522 (see also PRs #7565 and #7590). But basically, you can't call `toInstant` in a `java.sql.Date`.

But why are using a `java.sql.Date` when filling the form? Why not a `java.util.Date`?

Best.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/3aecedb8-415c-4044-aa01-7b67973a7294%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcos Pereira
Software Engineer, Lightbend.com

Benjamin Black

unread,
Nov 3, 2017, 10:23:07 AM11/3/17
to Play Framework
We get the data from a database and jdbc/java.sql.* code, which returns a sql date.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Marcos Pereira

unread,
Nov 3, 2017, 11:26:07 AM11/3/17
to play-fr...@googlegroups.com
Hey Ben,

The recommendation then is to map declare your case class using `java.sql.Date`, then the correct format will be picked.

Best.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/0a990c49-83c7-4675-b20e-b906901b8cd7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Yannick De Turck

unread,
Dec 30, 2017, 11:06:16 AM12/30/17
to Play Framework
I also ran into this today whilst upgrading my Play Framework project from 2.4 to 2.6.10.
In my project I'm still using Anorm to fetch data from the database and all the dates are passed as java.sql.Date objects by Anorm.

This issue is caused by changes in the unbind function in Format.scala:
def unbind(key: String, value: Date) = Map(key -> formatter.format(value.toInstant.atZone(javaTimeZone)))
I assume the previous implementation didn't make use of the JDK 8 Date API as it didn't have any issue treating both java.util.Date and java.sql.Date.

Would've been useful if it still knew how to handle java.sql.Date properly.
Now in order to get around this, all the java.util.Date fields in my model classes need to be refactored to LocalDate and need to get either mapped to java.util.Date when filling the forms or I need to completely refactor everything from java.util.Date to LocalDate.




Reply all
Reply to author
Forward
0 new messages