0

Using ActiveJDBC with MySQL, the column declaration:

`start` datetime NOT NULL

Java Model:

    public class MyTable extends Model {
        private static final String COL_START = "start";
        ...
        public Date getStart() {
            return getDate(COL_START);
        }
    }

I only get the date like yyyy-MM-dd, the time (HH:mm:ss) is missing. Same thing when I want to save a date, the hours/minutes/seconds are not saved.
Why?
Thanks!

Maxime Laval
  • 4,068
  • 8
  • 40
  • 60

1 Answers1

1

The answer is in the name of the method. You are asking for a date, and not date with time. The JavaDoc: http://javalite.github.io/activejdbc/org/javalite/activejdbc/Model.html#getDate-java.lang.String- .

You need to use:

public class MyTable extends Model {
    private static final String COL_START = "start";
    ...
    public Timestamp getStart() {
        return getTimestamp(COL_START);
    }
}

in order to get date with a time component.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • 1
    I don't want a Timestamp, I want a Date, and in Java a Date can handle hours, minutes and seconds. Sorry I come from the Hibernate world ;-) Anyway a date is a date and a timestamp is a timestamp, 2 different things, why ActiveJDBC adds confusion? – Maxime Laval Feb 24 '15 at 16:50
  • @MaximeLaval, you probably heard that the date/time API in Java was garbage for a long time, until Java 8. So, a Date is just that a date in a real world. However, you are incorrect stating that *a date is a date and a timestamp is a timestamp*. In Java, `java.sql.Date` is a super class of `java.sql.Timestamp`. Please, see this for more details: http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html. This means that when you use this solution, you will get what you want: `java.sql.Date`. – ipolevoy Feb 24 '15 at 21:06
  • "a date is a date and a timestamp is a timestamp" I meant in SQL. Anyway I don't use java.sql.Date I use java.util.Date, with Hibernate I map the columns with the type java.util.Date, I guess I shouldn't consider ActiveJDBC as the same kind of ORM, I am new to it. What is your recommended way to get/set a java.util.Date with ActiveJDBC? – Maxime Laval Feb 24 '15 at 23:47
  • @MaximeLaval, the `java.sql.Date` is `java.util.Date` because it extends it too:). ActiveJDBC is certainly is not Hibernate. I suggest you use so-called conversion methods, as they can convert values from one type to another. For instance: `model.setDate(obj)`, `model.setTimestamp(obj)`, etc. – ipolevoy Feb 25 '15 at 03:36
  • Same for getters. You might even get away with just setting a string as date: http://javalite.io/data_conversions#different-data-types-for-the-same-attribute. Data conversion is implemented with Convert class: https://github.com/javalite/activejdbc/blob/master/javalite-common/src/main/java/org/javalite/common/Convert.java. Look at sources, and you will see it is trying to bend backwards to convert one thing to another. Also, http://javalite.io/data_conversions - ActiveJDBC is a pass-through framework. What you stick to a model, will be sent to a DB. Whatever driver returns is set to a model! – ipolevoy Feb 25 '15 at 03:37
  • @ipolevoy When I use `p.setTimestamp("created_at", new Date().getTime());` I get error: `java.lang.IllegalArgumentException: cannot set 'created_at'` `at org.javalite.activejdbc.Model.setRaw(Model.java:323)` Why? and the date is automatically created in DB – kittu Feb 06 '17 at 07:40
  • 1
    @kittu, this is because the 'created_at' is auto-generated, please follow the docs: http://javalite.io/autogenerated_fields – ipolevoy Feb 06 '17 at 17:28
  • @ipolevoy Is it possible to get column names as alias from mysql db. I didn't find any documentation about this – kittu Feb 10 '17 at 12:00
  • @kittu, what do you mean? you might want to file an issue and provide more info: https://github.com/javalite/activejdbc/issues – ipolevoy Feb 10 '17 at 14:13
  • @ipolevoy I mean selecting db column names as alias. For example, "select car from motors as vehicle". Here vehicle is alias name for car. How do I achieve this in active jdbc? – kittu Feb 10 '17 at 14:35
  • @kittu, you can use List records = Base.find("select car from motors as vehicle"); If you are using models, they are expecting SQL that ties them to a table structure. – ipolevoy Feb 10 '17 at 14:45
  • @kittu, please file an issue: https://github.com/javalite/activejdbc/issues and provide details there, as your comments do not apply to this question. – ipolevoy Feb 10 '17 at 17:11
  • @ipolevoy OK I will file the issue – kittu Feb 10 '17 at 17:15