Quantcast

Use a Date subclass as a GORM property

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Use a Date subclass as a GORM property

Sam Carr
I have a Date subclass called Day that adds a bunch of utility functionality and some domain specific stuff on top of Date (e.g. always fixing the time to midnight). This class doesn't introduce any new data fields so naturally I'd like it to end up stored in the DB in a 'date' typed column. However by default it gets serialized to a bin(255) column, which is inefficient and means I can't run date comparison queries in the DB.

What do I have to do to convince GORM / Hibernate to store it seamlessly as a date?

I tried setting the mapping with type: "date" which seems to be fine for saving data but causes a failure when fetching it as internally Hibernate tries to assign Date objects to my Day field, which barfs. At least I think that's what's happening. Alternatively I could write a custom UserType and specify that to GORM but it seems a bit over the top - especially if you read up on how convoluted and confusing custom UserTypes can be!

Is there are simple solution here? Or even a complex one that I haven't recognised...

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

johnrengelman
We use Joda Time for this because the Grails plugin also provides
Hibernate mappings as custom types for all of the Joda classes. So in
your example, you could use Date instead of DateTime of you dont want
time to matter.

John

On Jan 20, 2012, at 8:07 AM, Sam Carr <[hidden email]> wrote:

> I have a Date subclass called Day that adds a bunch of utility functionality and some domain specific stuff on top of Date (e.g. always fixing the time to midnight). This class doesn't introduce any new data fields so naturally I'd like it to end up stored in the DB in a 'date' typed column. However by default it gets serialized to a bin(255) column, which is inefficient and means I can't run date comparison queries in the DB.
>
> What do I have to do to convince GORM / Hibernate to store it seamlessly as a date?
>
> I tried setting the mapping with type: "date" which seems to be fine for saving data but causes a failure when fetching it as internally Hibernate tries to assign Date objects to my Day field, which barfs. At least I think that's what's happening. Alternatively I could write a custom UserType and specify that to GORM but it seems a bit over the top - especially if you read up on how convoluted and confusing custom UserTypes can be!
>
> Is there are simple solution here? Or even a complex one that I haven't recognised...
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

Sam Carr
I've managed to cobble together a working custom UserType, though it seems a shame that it's this hard, and that I can't globally register it so any Day properties are mapped with it automatically. Or maybe that is possible and somebody can enlighten me?

On 20 Jan 2012, at 14:10, John Engelman wrote:

> We use Joda Time for this because the Grails plugin also provides
> Hibernate mappings as custom types for all of the Joda classes. So in
> your example, you could use Date instead of DateTime of you dont want
> time to matter.
>
> John
>
> On Jan 20, 2012, at 8:07 AM, Sam Carr <[hidden email]> wrote:
>
>> I have a Date subclass called Day that adds a bunch of utility functionality and some domain specific stuff on top of Date (e.g. always fixing the time to midnight). This class doesn't introduce any new data fields so naturally I'd like it to end up stored in the DB in a 'date' typed column. However by default it gets serialized to a bin(255) column, which is inefficient and means I can't run date comparison queries in the DB.
>>
>> What do I have to do to convince GORM / Hibernate to store it seamlessly as a date?
>>
>> I tried setting the mapping with type: "date" which seems to be fine for saving data but causes a failure when fetching it as internally Hibernate tries to assign Date objects to my Day field, which barfs. At least I think that's what's happening. Alternatively I could write a custom UserType and specify that to GORM but it seems a bit over the top - especially if you read up on how convoluted and confusing custom UserTypes can be!
>>
>> Is there are simple solution here? Or even a complex one that I haven't recognised...


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

basejump (Josh)
is there a reason you can't use Joda time? It ROCKS and is the only way to work with date stuff in Grails/Gorm/Hibernate in my experience.
Why reinvent the wheel?

On Jan 20, 2012, at 8:45 AM, Sam Carr wrote:

> I've managed to cobble together a working custom UserType, though it seems a shame that it's this hard, and that I can't globally register it so any Day properties are mapped with it automatically. Or maybe that is possible and somebody can enlighten me?
>
> On 20 Jan 2012, at 14:10, John Engelman wrote:
>
>> We use Joda Time for this because the Grails plugin also provides
>> Hibernate mappings as custom types for all of the Joda classes. So in
>> your example, you could use Date instead of DateTime of you dont want
>> time to matter.
>>
>> John
>>
>> On Jan 20, 2012, at 8:07 AM, Sam Carr <[hidden email]> wrote:
>>
>>> I have a Date subclass called Day that adds a bunch of utility functionality and some domain specific stuff on top of Date (e.g. always fixing the time to midnight). This class doesn't introduce any new data fields so naturally I'd like it to end up stored in the DB in a 'date' typed column. However by default it gets serialized to a bin(255) column, which is inefficient and means I can't run date comparison queries in the DB.
>>>
>>> What do I have to do to convince GORM / Hibernate to store it seamlessly as a date?
>>>
>>> I tried setting the mapping with type: "date" which seems to be fine for saving data but causes a failure when fetching it as internally Hibernate tries to assign Date objects to my Day field, which barfs. At least I think that's what's happening. Alternatively I could write a custom UserType and specify that to GORM but it seems a bit over the top - especially if you read up on how convoluted and confusing custom UserTypes can be!
>>>
>>> Is there are simple solution here? Or even a complex one that I haven't recognised...
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

johnrengelman
Agreed. Not sure why you aren't using Joda Time, but if you have a
reason for what you are doing, you can map custom UserType to Classes
in Config.groovy. I don't know the exact syntax off hand, but I know
Joda Time does this to map the Joda classes to the Joda UserTypes. If
you look at the scripts in the Joda Time plugin, you'll see how they
do it.

John

On Jan 20, 2012, at 1:25 PM, "Josh (basejump)" <[hidden email]> wrote:

> is there a reason you can't use Joda time? It ROCKS and is the only way to work with date stuff in Grails/Gorm/Hibernate in my experience.
> Why reinvent the wheel?
>
> On Jan 20, 2012, at 8:45 AM, Sam Carr wrote:
>
>> I've managed to cobble together a working custom UserType, though it seems a shame that it's this hard, and that I can't globally register it so any Day properties are mapped with it automatically. Or maybe that is possible and somebody can enlighten me?
>>
>> On 20 Jan 2012, at 14:10, John Engelman wrote:
>>
>>> We use Joda Time for this because the Grails plugin also provides
>>> Hibernate mappings as custom types for all of the Joda classes. So in
>>> your example, you could use Date instead of DateTime of you dont want
>>> time to matter.
>>>
>>> John
>>>
>>> On Jan 20, 2012, at 8:07 AM, Sam Carr <[hidden email]> wrote:
>>>
>>>> I have a Date subclass called Day that adds a bunch of utility functionality and some domain specific stuff on top of Date (e.g. always fixing the time to midnight). This class doesn't introduce any new data fields so naturally I'd like it to end up stored in the DB in a 'date' typed column. However by default it gets serialized to a bin(255) column, which is inefficient and means I can't run date comparison queries in the DB.
>>>>
>>>> What do I have to do to convince GORM / Hibernate to store it seamlessly as a date?
>>>>
>>>> I tried setting the mapping with type: "date" which seems to be fine for saving data but causes a failure when fetching it as internally Hibernate tries to assign Date objects to my Day field, which barfs. At least I think that's what's happening. Alternatively I could write a custom UserType and specify that to GORM but it seems a bit over the top - especially if you read up on how convoluted and confusing custom UserTypes can be!
>>>>
>>>> Is there are simple solution here? Or even a complex one that I haven't recognised...
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

Sam Carr
My app is heavily date oriented and I have a bunch of methods added to my Day class as part of my business logic. I would have to add the same stuff to the JODA class just the same, and hence I'd be in much the same situation. But thanks for the suggestion and it sounds like there are at the very least some useful examples as part of the way JODA solves this problem.

I've searched for UserType throughout http://grails.org/doc/latest/guide/single.html and I couldn't find a documented way to specify default UserType mappings in Config. If anybody knows the way I'd love to hear it.

Thanks

On 20 Jan 2012, at 22:28, John Engelman wrote:

> Agreed. Not sure why you aren't using Joda Time, but if you have a
> reason for what you are doing, you can map custom UserType to Classes
> in Config.groovy. I don't know the exact syntax off hand, but I know
> Joda Time does this to map the Joda classes to the Joda UserTypes. If
> you look at the scripts in the Joda Time plugin, you'll see how they
> do it.
>
> John
>
> On Jan 20, 2012, at 1:25 PM, "Josh (basejump)" <[hidden email]> wrote:
>
>> is there a reason you can't use Joda time? It ROCKS and is the only way to work with date stuff in Grails/Gorm/Hibernate in my experience.
>> Why reinvent the wheel?
>>
>> On Jan 20, 2012, at 8:45 AM, Sam Carr wrote:


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

basejump (Josh)
I see. 
a couple of places to look for ideas then
and

On Jan 20, 2012, at 4:39 PM, Sam Carr wrote:

My app is heavily date oriented and I have a bunch of methods added to my Day class as part of my business logic. I would have to add the same stuff to the JODA class just the same, and hence I'd be in much the same situation. But thanks for the suggestion and it sounds like there are at the very least some useful examples as part of the way JODA solves this problem.

I've searched for UserType throughout http://grails.org/doc/latest/guide/single.html and I couldn't find a documented way to specify default UserType mappings in Config. If anybody knows the way I'd love to hear it.

Thanks

On 20 Jan 2012, at 22:28, John Engelman wrote:

Agreed. Not sure why you aren't using Joda Time, but if you have a
reason for what you are doing, you can map custom UserType to Classes
in Config.groovy. I don't know the exact syntax off hand, but I know
Joda Time does this to map the Joda classes to the Joda UserTypes. If
you look at the scripts in the Joda Time plugin, you'll see how they
do it.

John

On Jan 20, 2012, at 1:25 PM, "Josh (basejump)" <[hidden email]> wrote:

is there a reason you can't use Joda time? It ROCKS and is the only way to work with date stuff in Grails/Gorm/Hibernate in my experience.
Why reinvent the wheel?

On Jan 20, 2012, at 8:45 AM, Sam Carr wrote:


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

lucastex
In reply to this post by Sam Carr
I'm getting in this thread just now, sorry if my suggestion have been already discussed, but what if you still uses "Date" field, and add all your "black-magic" in the Date class metaclass?

[]s,




On Fri, Jan 20, 2012 at 8:39 PM, Sam Carr <[hidden email]> wrote:
My app is heavily date oriented and I have a bunch of methods added to my Day class as part of my business logic. I would have to add the same stuff to the JODA class just the same, and hence I'd be in much the same situation. But thanks for the suggestion and it sounds like there are at the very least some useful examples as part of the way JODA solves this problem.

I've searched for UserType throughout http://grails.org/doc/latest/guide/single.html and I couldn't find a documented way to specify default UserType mappings in Config. If anybody knows the way I'd love to hear it.

Thanks

On 20 Jan 2012, at 22:28, John Engelman wrote:

> Agreed. Not sure why you aren't using Joda Time, but if you have a
> reason for what you are doing, you can map custom UserType to Classes
> in Config.groovy. I don't know the exact syntax off hand, but I know
> Joda Time does this to map the Joda classes to the Joda UserTypes. If
> you look at the scripts in the Joda Time plugin, you'll see how they
> do it.
>
> John
>
> On Jan 20, 2012, at 1:25 PM, "Josh (basejump)" <[hidden email]> wrote:
>
>> is there a reason you can't use Joda time? It ROCKS and is the only way to work with date stuff in Grails/Gorm/Hibernate in my experience.
>> Why reinvent the wheel?
>>
>> On Jan 20, 2012, at 8:45 AM, Sam Carr wrote:


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a Date subclass as a GORM property

Sam Carr
Lucas,

I wouldn't want to modify the Date class itself as my extra methods are not just additional utilities but some minor mods to functionality - like insisting time is set to 00:00:00. I tend to prefer not to use meta-programming unless it's for very good reason as it obscures the code and makes it non-obvious and surprising what things are really doing. I'm reasonably happy with the solution I've got, with a custom Hibernate UserType, though a bit distressed by how complicated such beasts turn out to be. Thanks everyone for your suggestions.

Sam

On 21 Jan 2012, at 20:34, Lucas F. A. Teixeira wrote:

I'm getting in this thread just now, sorry if my suggestion have been already discussed, but what if you still uses "Date" field, and add all your "black-magic" in the Date class metaclass?

[]s,



On Fri, Jan 20, 2012 at 8:39 PM, Sam Carr <[hidden email]> wrote:
My app is heavily date oriented and I have a bunch of methods added to my Day class as part of my business logic. I would have to add the same stuff to the JODA class just the same, and hence I'd be in much the same situation. But thanks for the suggestion and it sounds like there are at the very least some useful examples as part of the way JODA solves this problem.

I've searched for UserType throughout http://grails.org/doc/latest/guide/single.html and I couldn't find a documented way to specify default UserType mappings in Config. If anybody knows the way I'd love to hear it.

Thanks

Loading...