Duration storage #5955
Replies: 3 comments 11 replies
-
That looks quite nice, and I have no objections at all in adding such handy mapping annotations - however let me point out that I don't agree with the premise of "user friendlyness" of not storing nanoseconds. Encoding such values at high resolution (not precision) at least it ensures that on reading a stored value, the same value is returned. Let's remember that time-zone dependent values are a mess as they depend on politics and systems being updated timely and consistently to be aware of whatever weird non-technical decisions politicians made; even assuming time-zone settings are correct from a technical point of view, it's quite possible that the JVM and the DB are at different stages or quality of patching in such regard (i.e. one might not have been updated to the latest oddness yet), and since storing at lower precision implies truncations based on such inconsistent rules, the "user friendliness" is arguable. Storing at highest resolution at least prevents some classes of such oddities. Also, most users won't care what you store. So while I like being able to control the resolution I'd keep high resolution as a default. |
Beta Was this translation helpful? Give feedback.
-
I'd argue that's an endless problem: when I'm expressing a duration in years, the number of days will likely be so large it will feel meaningless.
I very much doubt all durations expressed as a fraction of a day can be represented as a decimal number (with a finite number of decimal digits). You're bound to hit rational but non-decimal numbers at some point and then you might lose precision. That's the kind of limitation I'd hate to expose users to, especially just for the convenience of reading "understandable" number in the DB, and especially if the problem can be introduced by something so seemingly innocuous as picking one enum value over another. For example take a duration representing a prime number of seconds, say Rounding might work out fine in this case (I don't know), but then it might not in other cases.
I suppose you meant
I believe either alternative is preferable to |
Beta Was this translation helpful? Give feedback.
-
So it seems to me that we all agree the following options would be useful:
I think that a truncating Apart from that, the idea to have a storage type enum kind of would help mapping |
Beta Was this translation helpful? Give feedback.
-
Now that I've got our
Duration
support working properly again, it has re-occurred to me that the way Hibernate storesDuration
s is not very user-friendly: sure, Java represents all durations with nanosecond precision, but it's not very nice to open up the database and see a value with 11 zeros on it representing one measly hour. A nanosecond is not actually a very useful human-scale measure of time.So to my mind, a much friendlier way to store a duration is as seconds, i.e. using
numeric(21,9)
instead ofnumeric(21)
.Or, hell, even as days would make sense, using something like
numeric(22,14)
.How could we accommodate this in a way that doesn't break backward compatibility with our own data?
Well, I know I've resisted introducing new special-purpose annotations for this kind of thing in the past, but here I guess I would propose a new
@DurationStorage
annotation, withenum TimeZoneStorageType { NANOSECOND, SECOND, DAY }
.This would actually be pretty straightforward to implement: all the machinery is there. I was very pleasantly surprised to notice that actually implemented HQL duration support in a pretty robust way, where durations know about their own precision and the translation code automatically adapts to the precision of the duration. So that's easy enough.
Finally, I believe that @beikov would like to be able to store a
Duration
in a SQLinterval second
column, and I think we can accommodate that here too, though it would take a bit more work to get it working in HQL. (What was there previously was not working.) Ultimately we could have something like:Alternatives?
SecondDurationJavaType
andDayDurationJavaType
, and use the@JavaType
annotation. This would work well-enough, I believe, and I guess it could also be extended to accommodateIntervalDurationJavaType
. Note, however, that the HQL translator would need to be aware of these newJavaType
s and special-case them usinginstanceof
, so it's not quite as clean as it looks.JdbcType
, though I think that would be an abuse of the APIs.)Either of these alternative approaches has the advantage of not introducing new annotations, though on the flip side that makes the functionality less-discoverable to users. And I guess we could argue about which of:
vs
is more aesthetically pleasing. I'm not sure, but I guess I lean toward the first option. Especially since we have already gone there with
@TimeZoneStorage
.Beta Was this translation helpful? Give feedback.
All reactions