You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Riccardo De Menna <de...@tuorlo.net> on 2022/10/24 06:41:36 UTC

About ValueObjectType and Extended types and Collection based objects

Hi all,

I’m new to Cayenne and coming from the WebObjects world I’m trying to figure out how to adjust to the different environment. I was looking for some simple advice on how to do conversions for custom types.

In particular, how would I go to save a collection based object. Let’s say I have a List<LocalDateTime> and my database is holding data as a string of pipe separated longs representing the epoch millis. In webobjects I would be able to individually map that single column to factory methods and convert them. In cayenne I see I’m supposed to use ValueObjectType (or extended types) but I encountered two issues:

Issue 1:
If I want to use List<LocalDateTime> as my value type I can’t return the generic part with getValueType(). And eliminating the generics would make it a useless raw type because it would then clash with any other list based conversion (i.e. List<String>, List<Duration> bla bla) that requires a different encoding/decoding mechanism.
One thing that came to mind would be to attempt using a LocalDateTime[].class and then having to converted to a List<LocalDateTime> elsewhere. Is this the only way to go? 

Issue 2:
What if my database has different columns holding the same data type but serialized in different ways? I was checking some very old code of mine that I was trying to port and I found that in one cases I had serialized a list of strings as pipe separated and then somewhere else another list of strings as comma separated. How would one go to define different deserialization schemes for the same List<String> object. One based on a specific column of the database?

Thank you in advance for any support,
Riccardo De Menna

Re: About ValueObjectType and Extended types and Collection based objects

Posted by Riccardo De Menna <de...@tuorlo.net>.
Thank you Andrus,

It makes sense. I was able to draft down a few classes sharing common behaviour (lists of objects separated by a character) that also define as an inner static class the suggested list wrapper. It’s nicely self contained and tidy. Yes… do think about either adding generic support or the ability to point from the model to a specific factory or converter. This would allow to just pick different schemes without having to hide them behind empty classes.

Regards,
Riccardo De Menna

> On 24 Oct 2022, at 09:19, Andrus Adamchik <aa...@gmail.com> wrote:
> 
> Hi Riccardo,
> 
> Indeed, ValueObjectType and ExtendedType are not generics-aware. Something we may need to think about and maybe add to Cayenne. 
> 
> What I would recommend in your situation is to map your attributes using custom lightweight wrapper types, one class per specific case. E.g. CommaSeparatedString, PipeSeparatedDuration. These can be associated with ValueObjectTypes. In the code you can use them as is, or create cover methods to expose as Lists:
> 
> class MyEntity extends _MyEntity {
> 
>   List<Duration> getDurations() {
>      return getPipeSeparatedDurations() != null 
>         ? getPipeSeparatedDurations().asList() 
>         : Collections.emptyList();
>   }
> }
> 
> Andrus
> 
> 
>> On Oct 24, 2022, at 8:41 AM, Riccardo De Menna <de...@tuorlo.net> wrote:
>> 
>> Hi all,
>> 
>> I’m new to Cayenne and coming from the WebObjects world I’m trying to figure out how to adjust to the different environment. I was looking for some simple advice on how to do conversions for custom types.
>> 
>> In particular, how would I go to save a collection based object. Let’s say I have a List<LocalDateTime> and my database is holding data as a string of pipe separated longs representing the epoch millis. In webobjects I would be able to individually map that single column to factory methods and convert them. In cayenne I see I’m supposed to use ValueObjectType (or extended types) but I encountered two issues:
>> 
>> Issue 1:
>> If I want to use List<LocalDateTime> as my value type I can’t return the generic part with getValueType(). And eliminating the generics would make it a useless raw type because it would then clash with any other list based conversion (i.e. List<String>, List<Duration> bla bla) that requires a different encoding/decoding mechanism.
>> One thing that came to mind would be to attempt using a LocalDateTime[].class and then having to converted to a List<LocalDateTime> elsewhere. Is this the only way to go? 
>> 
>> Issue 2:
>> What if my database has different columns holding the same data type but serialized in different ways? I was checking some very old code of mine that I was trying to port and I found that in one cases I had serialized a list of strings as pipe separated and then somewhere else another list of strings as comma separated. How would one go to define different deserialization schemes for the same List<String> object. One based on a specific column of the database?
>> 
>> Thank you in advance for any support,
>> Riccardo De Menna
> 
> 


Re: About ValueObjectType and Extended types and Collection based objects

Posted by Andrus Adamchik <aa...@gmail.com>.
Hi Riccardo,

Indeed, ValueObjectType and ExtendedType are not generics-aware. Something we may need to think about and maybe add to Cayenne. 

What I would recommend in your situation is to map your attributes using custom lightweight wrapper types, one class per specific case. E.g. CommaSeparatedString, PipeSeparatedDuration. These can be associated with ValueObjectTypes. In the code you can use them as is, or create cover methods to expose as Lists:

class MyEntity extends _MyEntity {

   List<Duration> getDurations() {
      return getPipeSeparatedDurations() != null 
         ? getPipeSeparatedDurations().asList() 
         : Collections.emptyList();
   }
}

Andrus


> On Oct 24, 2022, at 8:41 AM, Riccardo De Menna <de...@tuorlo.net> wrote:
> 
> Hi all,
> 
> I’m new to Cayenne and coming from the WebObjects world I’m trying to figure out how to adjust to the different environment. I was looking for some simple advice on how to do conversions for custom types.
> 
> In particular, how would I go to save a collection based object. Let’s say I have a List<LocalDateTime> and my database is holding data as a string of pipe separated longs representing the epoch millis. In webobjects I would be able to individually map that single column to factory methods and convert them. In cayenne I see I’m supposed to use ValueObjectType (or extended types) but I encountered two issues:
> 
> Issue 1:
> If I want to use List<LocalDateTime> as my value type I can’t return the generic part with getValueType(). And eliminating the generics would make it a useless raw type because it would then clash with any other list based conversion (i.e. List<String>, List<Duration> bla bla) that requires a different encoding/decoding mechanism.
> One thing that came to mind would be to attempt using a LocalDateTime[].class and then having to converted to a List<LocalDateTime> elsewhere. Is this the only way to go? 
> 
> Issue 2:
> What if my database has different columns holding the same data type but serialized in different ways? I was checking some very old code of mine that I was trying to port and I found that in one cases I had serialized a list of strings as pipe separated and then somewhere else another list of strings as comma separated. How would one go to define different deserialization schemes for the same List<String> object. One based on a specific column of the database?
> 
> Thank you in advance for any support,
> Riccardo De Menna