0

I foundout that scalaquery uses java.sql.date as the date object. But it drops time when I create java.sql.date.

Is there any way that I can use to create mysql datetime field in scalaquery?

tiran
  • 2,389
  • 1
  • 16
  • 28
  • Don't have a mysql instance to test against, but have you tried using the implicit conversion between String and mysql datetime? – Noel Oct 21 '11 at 16:33

2 Answers2

1

There is no java.util.Date support in ScalaQuery. However, it's possible to enhance ScalaQuery with your own type mappers. For java.util.Date such a wrapper could look like this

 implicit val JavaUtilDateTypeMapper = MappedTypeMapper.base[java.util.Date, Long]  (_.getTime, new java.util.Date(_))

converting the java.util.Date into a simple Long. It depends on your database what's the best target, in my case a Long works perfectly.

Steffen
  • 8,033
  • 1
  • 28
  • 34
  • +1 nice tip, @Steffan. Pass Long date until you need a Date (i.e. at query time), vs. creating a Date (e.g. from string uri date) and passing to query method. – virtualeyes Feb 15 '12 at 17:45
1

Instead of using java.sql.Date, try using java.sql.Timestamp, when declaring the column.

It looks like that yields a better mapping for a column with both date and time elements.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92