views:

459

answers:

3

Fields in a DB were of type DATETIME (mysql).

Why was it generated as a timestamp and will it be regenerated

during object persistence? That is not a desired scenario in my case,

seeing that as not a timestamp field means that it should only be set via setter,

no generation needed, I dont really understand why it did not generate it as Date type!

p.s. The problem as I see in that mysql JDBC driver returns datetime type as TIMESTAMP ! How to resolve all that?

A: 

Without knowing the inner workings of hibernate, we have our date fields in the database (mysql) as date. Not DATETIME. Maybe that is why it generated timestamp.

You should consider changing your date tables to date and then you can change your entity beans and remove the @Temporal

In any case, it is probably safe to remove the @Temporal(TemporalType.TIMESTAMP) alltogether from the entity beans if that is not what you want.

Shervin
@Shervin OK, but I need to keep time in the field as well, so I'm obliged to keep it DATETIME in mysql. You recommend to change itS MAPPING from timestamp to DateTime?
EugeneP
A: 

The @Temporal annotation denotes, whether annotated property should be mapped to java.sql.Date (holds only date part), java.sql.Time (holds only time part) or to java.sql.Timestamp (holds date and time also with fractions of second). It has nothing to do with generation of property value.

Tadeusz Kopec
It may be neccesary to annotate such field as "TemportalType.DATE" if you need to compare some dates without time in queries.
Vanger
Tadeusz Kopec You mean it's ok to keep it as Timestamp? I see. But then another question, what if a field is really a timestamp and should be regenerated [ on update etc ]. Does Hibernate knows about it? I still see getter and setter, while I do not need this field to set up manually. I can exclude it from mapping, right, but if I keep it and do not use a setter, will it update TIMESTAMP on a row update ?
EugeneP
It can be tested. I think - yes.
Vanger
AFAIK the only properties that Hibernate may generate for you are @Id and @Version. If your database generates values for some fields and you want Hibernate to be aware of it, use @Generated annotation.
Tadeusz Kopec
A: 

I think of two possibilities: Hibernate does not know about data type "dateTime" for mysql or "dateTime" explicitly mapped to java.sql.Timestamp or java.utils.Date (wich needs TemporalType.Timestamp annotation). And this mapping probably can be changed.

Vanger
@Vanger How do you think, will it be better to change annotation manually to TEMPORALTYPE.DATETIME?
EugeneP
i misspelled there's no "DATETIME" type. As "Shervin" said can try to remove annotation at all. The field value should be treaded correctly with time value.
Vanger