You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Tedman Leung <te...@sfu.ca> on 2009/03/25 17:48:30 UTC

how to store collection of enums as strings

Anyone know how to store a collection of enums as Strings instead of their
ordinal values? (preferably with annotations...)

i.e. 
	@ManyToMany
	private Set<MyEnum> myEnums=new HashSet<MyEnum>();


-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

It's time for a new bike when the bulb in your shift light burns out.

Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
actually no, it's not the simplest way.

The example I provided already works in openJpa with enums or strings, 
it's only 1 line of code / 1 annotation

   @PersistentCollection

given that annotation, it will already sort out everything you've 
mentioned.

   @PersistentCollection
   private HashSet<String> myStrings=new HashSet<String>();

or

   @PersistentCollection
   private HashSet<MyEnum> myEnums=new HashSet<MyEnum>();

already creates a second table, a join, and already loads and persists the 
2 items for you when the entity is persisted. It works great for the most 
part and is really as easy as the above code. (The one exception being 
another email I sent saying eager fetching seems to be broken.)

All I really wanted (and also see Craigs posting today) is to allow the 
@Enumerated annotation to be allowed and used in addition to the above, 
i.e.

   @Enumerated(STRING)
   @PersistentCollection
   private HashSet<MyEnum> myEnums=new HashSet<MyEnum>();
  
The difference is I'm not persisting another entity, I'm persisting a 
primitive or in my case a Enum which I'm considering an instance of the 
Enum primitive. The concept extends to peristing other primitives like 
java.util.Date too which probably works out of the box right now with 
@PersistenCollection but also probably (I haven't tried it) doesn't allow 
@Temporal to be annotated at the same time either.

So, to go from having just 2 of code which works but stores the ordinal 
value, to having the entire setup you've describe - just to change the 
persisted type from ordinal to string, seems like a seriously large work 
around especially considering changing an ordinal to string for a single 
enum is only 1 extra line too, @Enumerated(STRING).



On Tue, Mar 31, 2009 at 08:23:52AM -0700, Paul Copeland wrote:
> Hi Ted -
>
> Interesting point of view.  I would have thought this is the plain  
> vanilla "simple clean out of the box" way to persist a Collection of  
> Enums or any other kind of collection.
>
> This results in a bare minimum join with two tables and a foreign key  
> which is how a Collection maps into the world of relational databases  
> whether you are using straight JDBC or JPA or Hibernate or brand X ORM.   
> In the end you wind up with two database tables and two objects that  
> model the association.
>
> - Paul
>
> On 3/31/2009 7:40 AM, Tedman Leung wrote:
>> thanks but I was hoping there was a simple clean out of the box style  
>> solution. Considering the code could/should have just been a single  
>> annotation to the collection, the work around looks like an awful lot 
>> of work for a framework which is suppose to reduce work.
>>
>> In the end, I just used raw JDBC in a nativeQuery. At least this way 
>> other developers can understand the code I've written and it's only 
>> actually about 15 lines of simple straight forward code.
>>
>> thanks.
>>
>>
>> On Mon, Mar 30, 2009 at 11:16:52AM -0700, Paul Copeland wrote:
>>   
>>> Hi Ted - See example code below
>>>
>>> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>>     
>>>>> Isn't the same thing true that OneToMany should annotate a 
>>>>> Collection of  Entities?  So the Enums would be a field on the 
>>>>> ManyToOne side of that  kind of mapping.  Then you can specify 
>>>>> the @Column and @Enumerated  annotations on the actual field or 
>>>>> property.
>>>>>             
>>>> no you can't, I tried it. I can't remember the specific error but 
>>>> it was along the lines of  - you're not allowed to specify a column 
>>>> attribute here.
>>>>         
>>> Here is an example that uses the @Enumerated and @Column annotations 
>>> on  a OneToMany relation.
>>>
>>> This stores a Set of Enums as String values.  A similar example would 
>>>  work for ManyToMany.  The same approach can also be used with   
>>> Collections of Temporal values.
>>>
>>> There are two classes - an EnumHolder that holds the Collection and 
>>> an  EnumValue that holds an Enum and stores it as a String.  Here is 
>>> the  MappingTool generated SQL (Postgres database).  As you can see 
>>> the Enum  is stored as a String VARCHAR(10).
>>>
>>> CREATE TABLE enum_holder (id BIGSERIAL NOT NULL, PRIMARY KEY (id));
>>>
>>> CREATE TABLE enum_value (id BIGSERIAL NOT NULL, StringEnum 
>>> VARCHAR(10),  ENUMHOLDER_ID BIGINT, PRIMARY KEY (id));
>>>
>>> package com.jpa.test;
>>>
>>> import javax.persistence.*;
>>> import java.util.*;
>>>
>>> @Entity
>>> @Table (name="enum_holder")
>>> public class EnumHolder
>>>    implements java.io.Serializable
>>> {
>>>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>>>    @Id private long id;
>>>
>>>    @OrderBy
>>>    @OneToMany(mappedBy="enumHolder", fetch=FetchType.LAZY,
>>>               cascade={CascadeType.PERSIST,CascadeType.REMOVE})
>>>    private Set<EnumValue> enums;
>>>
>>>    public Set<EnumValue> getEnums()
>>>    {
>>>        if (enums == null)
>>>            enums = new HashSet<EnumValue>();
>>>        return enums;
>>>    }
>>> }
>>>
>>> package com.jpa.test;
>>>
>>> import javax.persistence.*;
>>>
>>> @Entity
>>> @Table (name="enum_value")
>>> public class EnumValue
>>>    implements java.io.Serializable
>>> {
>>>    public static enum MyEnum{foo, Bar, Batz, woohoo}
>>>
>>>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>>>    @Id private long id;
>>>
>>>    @ManyToOne (optional=false, fetch=FetchType.LAZY,
>>>                cascade=CascadeType.PERSIST)
>>>    private EnumHolder enumHolder;
>>>
>>>    @Enumerated (EnumType.STRING)
>>>    @Column(name="StringEnum", length=10)
>>>    private MyEnum myEnum;
>>>
>>>    public MyEnum getMyEnum() { return myEnum; }
>>> }
>>>
>>>
>>>
>>> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>>     
>>>>> How does your original example of ManyToMany annotation on a  
>>>>> Collection  of Enums actually work?  My understanding is that for 
>>>>> ManyToMany both  sides must be persistent Entities.  So therefore 
>>>>> the ManyToMany is  mapping the join table, not the Enum.
>>>>>             
>>>> yes oddly enough it actually does work. As per my subsequent email, 
>>>> I  switched it to @PersistentCollection and it performed the same 
>>>> way. I  considered trying to annotate my Enum like an Entity but I 
>>>> figured that would be pushing expected behaviour... even if it 
>>>> worked, so I decided not to.
>>>>
>>>>
>>>>         
>>>>> Isn't the same thing true that OneToMany should annotate a 
>>>>> Collection of  Entities?  So the Enums would be a field on the 
>>>>> ManyToOne side of that  kind of mapping.  Then you can specify 
>>>>> the @Column and @Enumerated  annotations on the actual field or 
>>>>> property.
>>>>>             
>>>> no you can't, I tried it. I can't remember the specific error but 
>>>> it was along the lines of  - you're not allowed to specify a column 
>>>> attribute here.
>>>>
>>>>         
>>>>> Could you explain why using the @PersistentCollection annotation 
>>>>> is   useful or necessary for OneToMany or ManyToMany collections 
>>>>> generally or  specifically in the case you are looking at?  I 
>>>>> don't understand the use  case that @PersistentCollection 
>>>>> satisfies.
>>>>>             
>>>> My understanding is @ManyToMany is a JPA standard and is only used 
>>>> to map entity to entities. 
>>>>
>>>> My understanding of @PersistentCollection is that is is an OpenJpa  
>>>> extention which allows people to map Entity to java Primities.
>>>>
>>>> i.e. 
>>>>
>>>> @Entity
>>>> class Bar
>>>> {
>>>> }
>>>>
>>>> @Entity
>>>> class Foo
>>>> {
>>>>    @ManyToMany
>>>>    HashSet<Bar> myBars=new HashSet<Bar>();
>>>>
>>>>    @PersistentCollection
>>>>    HashSet<String> myStrings=new HashSet<String>();
>>>> }
>>>>
>>>>
>>>> I guess there's a reason why the original JPA standard left out   
>>>> primitives... it gets complicated fast.
>>>>
>>>>
>>>>         
>>>>> - Paul
>>>>>
>>>>>
>>>>> On 3/29/2009 8:43 PM, Tedman Leung wrote:
>>>>>             
>>>>>> Just to document more thoughts and findings.
>>>>>>
>>>>>> 1) I was using the wrong annotation, I should have been using
>>>>>> @PersistentCollection
>>>>>>
>>>>>> 2) this seems to be a more wide spread problem with defining    
>>>>>> attributes to collections, i.e. - collection of enums needs to 
>>>>>> have @Enumerated configurations
>>>>>> - collection of Dates needs to have @Temporal configurations
>>>>>> - even collection of Strings seems to be missing a     
>>>>>> @Column(nullable=false, length=32) style annotation.
>>>>>>
>>>>>> I think the problem is much bigger and wide spread than I originaly though,
>>>>>> I don't think I'll be able to sort out a patch.
>>>>>>
>>>>>>
>>>>>> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>>>>>>                   
>>>>>>> yes I know I can do manual hacks to get them stored the way I 
>>>>>>> want to, but I was asking if there was a jpa / openjpa 
>>>>>>> annotation to allow that.
>>>>>>>
>>>>>>> As an example, I can just use @Enumerated on a single enum, 
>>>>>>> so it seems logical that some one would have thought about 
>>>>>>> storing a  collection of enums. 
>>>>>>>
>>>>>>> As for why I want them as strings instead of ordinals - the 
>>>>>>> usual reasons, i.e. it's safer for changes as the ordering 
>>>>>>> won't  accidentally corrupt my data (and without telling me), 
>>>>>>> and it's  easier to look into the database via sql or 
>>>>>>> something and just know what's going on / get simple reports 
>>>>>>> etc.
>>>>>>>
>>>>>>> I'm guessing right now that it's not possible at all and that 
>>>>>>> no one has done anything towards this. I might have to 
>>>>>>> rummage through the source and try to see if I can figure out 
>>>>>>> how it works and hack a patch to submit. It seemed like a 
>>>>>>> very straight forward use case though so I'm surprised no one 
>>>>>>> has asked or done anything about this before.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>>>>>>                         
>>>>>>>> What is your objective?  Do you want some non-JPA 
>>>>>>>> application to see  them in the database as Strings?
>>>>>>>>
>>>>>>>> At some point you have add these Enums to the collection 
>>>>>>>> one at a time.   You can use an addEnum() method or an 
>>>>>>>> Entity listener to convert them to  Strings at that point 
>>>>>>>> one at a time. And a  subclass of the Collection  type to 
>>>>>>>> getEnum() from the Collection when you fetch them back.
>>>>>>>>
>>>>>>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>>>>>>
>>>>>>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>>>>>>                               
>>>>>>>>> No, I'm talking about when the enum is in a collection.
>>>>>>>>>
>>>>>>>>> i.e.
>>>>>>>>>
>>>>>>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>>>>>>
>>>>>>>>> So no, either the @Enumerated helps, nor does calling 
>>>>>>>>> name() or  toString as neither are possible.
>>>>>>>>>
>>>>>>>>> I'm storing a Collection of enums , not a single Enum.
>>>>>>>>>
>>>>>>>>> There seems to be no examples of this nor any 
>>>>>>>>> documentation about the  ability to do this that I can 
>>>>>>>>> find. The default seems to use the ordinal value both for 
>>>>>>>>> table generation and storage value.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>>>>>>                                       
>>>>>>>>>> Hi - This is from the OpenJPA relations example -
>>>>>>>>>>
>>>>>>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>>>>>>    private Gender gender;
>>>>>>>>>>
>>>>>>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>>>>>>
>>>>>>>>>>   public void setGender(Gender gender) {
>>>>>>>>>>        this.gender = gender;
>>>>>>>>>>   }
>>>>>>>>>>
>>>>>>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>>>>>>
>>>>>>>>>> - Paul
>>>>>>>>>>
>>>>>>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>>>>>>                                               
>>>>>>>>>>> Ted,
>>>>>>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>>>>>>
>>>>>>>>>>> Catalina
>>>>>>>>>>>
>>>>>>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>>>>>>
>>>>>>>>>>>                                                       
>>>>>>>>>>>   
>>>>>>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>>>>>>
>>>>>>>>>>>> i.e.
>>>>>>>>>>>>        @ManyToMany
>>>>>>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> --
>>>>>>>>>>>>                                                           Ted Leung
>>>>>>>>>>>>                                                           tedman@sfu.ca
>>>>>>>>>>>>
>>>>>>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>>>>>>
>>>>>>>>>>>>                                                     
>>>>>>>>>>>>               
>>>>>>>>>>>                                                       
>>>>>>>>>>>   
>>>>>>>>>                                       
>>>>>>> -- 
>>>>>>>                                                            Ted Leung
>>>>>>>                                                            tedman@sfu.ca
>>>>>>>
>>>>>>> The most important words I've learned to say - "I don't know".
>>>>>>>                         
>>>>>>                   
>>>>         
>>
>>   
>

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

The most important words I've learned to say - "I don't know".

Re: how to store collection of enums as strings

Posted by Paul Copeland <te...@jotobjects.com>.
Hi Ted -

Interesting point of view.  I would have thought this is the plain 
vanilla "simple clean out of the box" way to persist a Collection of 
Enums or any other kind of collection.

This results in a bare minimum join with two tables and a foreign key 
which is how a Collection maps into the world of relational databases 
whether you are using straight JDBC or JPA or Hibernate or brand X ORM.  
In the end you wind up with two database tables and two objects that 
model the association.

- Paul

On 3/31/2009 7:40 AM, Tedman Leung wrote:
> thanks but I was hoping there was a simple clean out of the box style 
> solution. Considering the code could/should have just been a single 
> annotation to the collection, the work around looks like an awful lot of 
> work for a framework which is suppose to reduce work.
>
> In the end, I just used raw JDBC in a nativeQuery. At least this way other 
> developers can understand the code I've written and it's only actually 
> about 15 lines of simple straight forward code.
>
> thanks.
>
>
> On Mon, Mar 30, 2009 at 11:16:52AM -0700, Paul Copeland wrote:
>   
>> Hi Ted - See example code below
>>
>> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>     
>>>> Isn't the same thing true that OneToMany should annotate a Collection 
>>>> of  Entities?  So the Enums would be a field on the ManyToOne side of 
>>>> that  kind of mapping.  Then you can specify the @Column and 
>>>> @Enumerated  annotations on the actual field or property.
>>>>     
>>>>         
>>> no you can't, I tried it. I can't remember the specific error but it 
>>> was along the lines of  - you're not allowed to specify a column 
>>> attribute here.
>>>   
>>>       
>> Here is an example that uses the @Enumerated and @Column annotations on  
>> a OneToMany relation.
>>
>> This stores a Set of Enums as String values.  A similar example would  
>> work for ManyToMany.  The same approach can also be used with  
>> Collections of Temporal values.
>>
>> There are two classes - an EnumHolder that holds the Collection and an  
>> EnumValue that holds an Enum and stores it as a String.  Here is the  
>> MappingTool generated SQL (Postgres database).  As you can see the Enum  
>> is stored as a String VARCHAR(10).
>>
>> CREATE TABLE enum_holder (id BIGSERIAL NOT NULL, PRIMARY KEY (id));
>>
>> CREATE TABLE enum_value (id BIGSERIAL NOT NULL, StringEnum VARCHAR(10),  
>> ENUMHOLDER_ID BIGINT, PRIMARY KEY (id));
>>
>> package com.jpa.test;
>>
>> import javax.persistence.*;
>> import java.util.*;
>>
>> @Entity
>> @Table (name="enum_holder")
>> public class EnumHolder
>>    implements java.io.Serializable
>> {
>>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>>    @Id private long id;
>>
>>    @OrderBy
>>    @OneToMany(mappedBy="enumHolder", fetch=FetchType.LAZY,
>>               cascade={CascadeType.PERSIST,CascadeType.REMOVE})
>>    private Set<EnumValue> enums;
>>
>>    public Set<EnumValue> getEnums()
>>    {
>>        if (enums == null)
>>            enums = new HashSet<EnumValue>();
>>        return enums;
>>    }
>> }
>>
>> package com.jpa.test;
>>
>> import javax.persistence.*;
>>
>> @Entity
>> @Table (name="enum_value")
>> public class EnumValue
>>    implements java.io.Serializable
>> {
>>    public static enum MyEnum{foo, Bar, Batz, woohoo}
>>
>>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>>    @Id private long id;
>>
>>    @ManyToOne (optional=false, fetch=FetchType.LAZY,
>>                cascade=CascadeType.PERSIST)
>>    private EnumHolder enumHolder;
>>
>>    @Enumerated (EnumType.STRING)
>>    @Column(name="StringEnum", length=10)
>>    private MyEnum myEnum;
>>
>>    public MyEnum getMyEnum() { return myEnum; }
>> }
>>
>>
>>
>> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>     
>>>> How does your original example of ManyToMany annotation on a 
>>>> Collection  of Enums actually work?  My understanding is that for 
>>>> ManyToMany both  sides must be persistent Entities.  So therefore the 
>>>> ManyToMany is  mapping the join table, not the Enum.
>>>>     
>>>>         
>>> yes oddly enough it actually does work. As per my subsequent email, I  
>>> switched it to @PersistentCollection and it performed the same way. I  
>>> considered trying to annotate my Enum like an Entity but I figured that 
>>> would be pushing expected behaviour... even if it worked, so I decided 
>>> not to.
>>>
>>>
>>>   
>>>       
>>>> Isn't the same thing true that OneToMany should annotate a Collection 
>>>> of  Entities?  So the Enums would be a field on the ManyToOne side of 
>>>> that  kind of mapping.  Then you can specify the @Column and 
>>>> @Enumerated  annotations on the actual field or property.
>>>>     
>>>>         
>>> no you can't, I tried it. I can't remember the specific error but it 
>>> was along the lines of  - you're not allowed to specify a column 
>>> attribute here.
>>>
>>>   
>>>       
>>>> Could you explain why using the @PersistentCollection annotation is   
>>>> useful or necessary for OneToMany or ManyToMany collections generally 
>>>> or  specifically in the case you are looking at?  I don't understand 
>>>> the use  case that @PersistentCollection satisfies.
>>>>     
>>>>         
>>> My understanding is @ManyToMany is a JPA standard and is only used to 
>>> map entity to entities. 
>>>
>>> My understanding of @PersistentCollection is that is is an OpenJpa  
>>> extention which allows people to map Entity to java Primities.
>>>
>>> i.e. 
>>>
>>> @Entity
>>> class Bar
>>> {
>>> }
>>>
>>> @Entity
>>> class Foo
>>> {
>>>    @ManyToMany
>>>    HashSet<Bar> myBars=new HashSet<Bar>();
>>>
>>>    @PersistentCollection
>>>    HashSet<String> myStrings=new HashSet<String>();
>>> }
>>>
>>>
>>> I guess there's a reason why the original JPA standard left out  
>>> primitives... it gets complicated fast.
>>>
>>>
>>>   
>>>       
>>>> - Paul
>>>>
>>>>
>>>> On 3/29/2009 8:43 PM, Tedman Leung wrote:
>>>>     
>>>>         
>>>>> Just to document more thoughts and findings.
>>>>>
>>>>> 1) I was using the wrong annotation, I should have been using
>>>>> @PersistentCollection
>>>>>
>>>>> 2) this seems to be a more wide spread problem with defining   
>>>>> attributes to collections, i.e. - collection of enums needs to have 
>>>>> @Enumerated configurations
>>>>> - collection of Dates needs to have @Temporal configurations
>>>>> - even collection of Strings seems to be missing a    
>>>>> @Column(nullable=false, length=32) style annotation.
>>>>>
>>>>> I think the problem is much bigger and wide spread than I originaly though,
>>>>> I don't think I'll be able to sort out a patch.
>>>>>
>>>>>
>>>>> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>>>>>         
>>>>>           
>>>>>> yes I know I can do manual hacks to get them stored the way I 
>>>>>> want to, but I was asking if there was a jpa / openjpa annotation 
>>>>>> to allow that.
>>>>>>
>>>>>> As an example, I can just use @Enumerated on a single enum, so it 
>>>>>> seems logical that some one would have thought about storing a  
>>>>>> collection of enums. 
>>>>>>
>>>>>> As for why I want them as strings instead of ordinals - the usual 
>>>>>> reasons, i.e. it's safer for changes as the ordering won't  
>>>>>> accidentally corrupt my data (and without telling me), and it's  
>>>>>> easier to look into the database via sql or something and just 
>>>>>> know what's going on / get simple reports etc.
>>>>>>
>>>>>> I'm guessing right now that it's not possible at all and that no 
>>>>>> one has done anything towards this. I might have to rummage 
>>>>>> through the source and try to see if I can figure out how it 
>>>>>> works and hack a patch to submit. It seemed like a very straight 
>>>>>> forward use case though so I'm surprised no one has asked or done 
>>>>>> anything about this before.
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>>>>>             
>>>>>>             
>>>>>>> What is your objective?  Do you want some non-JPA application 
>>>>>>> to see  them in the database as Strings?
>>>>>>>
>>>>>>> At some point you have add these Enums to the collection one at 
>>>>>>> a time.   You can use an addEnum() method or an Entity listener 
>>>>>>> to convert them to  Strings at that point one at a time. And a  
>>>>>>> subclass of the Collection  type to getEnum() from the 
>>>>>>> Collection when you fetch them back.
>>>>>>>
>>>>>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>>>>>
>>>>>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>>>>>                 
>>>>>>>               
>>>>>>>> No, I'm talking about when the enum is in a collection.
>>>>>>>>
>>>>>>>> i.e.
>>>>>>>>
>>>>>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>>>>>
>>>>>>>> So no, either the @Enumerated helps, nor does calling name() 
>>>>>>>> or  toString as neither are possible.
>>>>>>>>
>>>>>>>> I'm storing a Collection of enums , not a single Enum.
>>>>>>>>
>>>>>>>> There seems to be no examples of this nor any documentation 
>>>>>>>> about the  ability to do this that I can find. The default 
>>>>>>>> seems to use the ordinal value both for table generation and 
>>>>>>>> storage value.
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>>>>>                       
>>>>>>>>                 
>>>>>>>>> Hi - This is from the OpenJPA relations example -
>>>>>>>>>
>>>>>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>>>>>    private Gender gender;
>>>>>>>>>
>>>>>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>>>>>
>>>>>>>>>   public void setGender(Gender gender) {
>>>>>>>>>        this.gender = gender;
>>>>>>>>>   }
>>>>>>>>>
>>>>>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>>>>>
>>>>>>>>> - Paul
>>>>>>>>>
>>>>>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>>>>>                             
>>>>>>>>>                   
>>>>>>>>>> Ted,
>>>>>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>>>>>
>>>>>>>>>> Catalina
>>>>>>>>>>
>>>>>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>>>>>
>>>>>>>>>>                                     
>>>>>>>>>>                     
>>>>>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>>>>>
>>>>>>>>>>> i.e.
>>>>>>>>>>>        @ManyToMany
>>>>>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> --
>>>>>>>>>>>                                                           Ted Leung
>>>>>>>>>>>                                                           tedman@sfu.ca
>>>>>>>>>>>
>>>>>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>>>>>
>>>>>>>>>>>                                             
>>>>>>>>>>>                       
>>>>>>>>>>                                     
>>>>>>>>>>                     
>>>>>>>>                       
>>>>>>>>                 
>>>>>> -- 
>>>>>>                                                            Ted Leung
>>>>>>                                                            tedman@sfu.ca
>>>>>>
>>>>>> The most important words I've learned to say - "I don't know".
>>>>>>             
>>>>>>             
>>>>>         
>>>>>           
>>>   
>>>       
>
>   


Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
thanks but I was hoping there was a simple clean out of the box style 
solution. Considering the code could/should have just been a single 
annotation to the collection, the work around looks like an awful lot of 
work for a framework which is suppose to reduce work.

In the end, I just used raw JDBC in a nativeQuery. At least this way other 
developers can understand the code I've written and it's only actually 
about 15 lines of simple straight forward code.

thanks.


On Mon, Mar 30, 2009 at 11:16:52AM -0700, Paul Copeland wrote:
> Hi Ted - See example code below
>
> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>> Isn't the same thing true that OneToMany should annotate a Collection 
>>> of  Entities?  So the Enums would be a field on the ManyToOne side of 
>>> that  kind of mapping.  Then you can specify the @Column and 
>>> @Enumerated  annotations on the actual field or property.
>>>     
>>
>> no you can't, I tried it. I can't remember the specific error but it 
>> was along the lines of  - you're not allowed to specify a column 
>> attribute here.
>>   
>
> Here is an example that uses the @Enumerated and @Column annotations on  
> a OneToMany relation.
>
> This stores a Set of Enums as String values.  A similar example would  
> work for ManyToMany.  The same approach can also be used with  
> Collections of Temporal values.
>
> There are two classes - an EnumHolder that holds the Collection and an  
> EnumValue that holds an Enum and stores it as a String.  Here is the  
> MappingTool generated SQL (Postgres database).  As you can see the Enum  
> is stored as a String VARCHAR(10).
>
> CREATE TABLE enum_holder (id BIGSERIAL NOT NULL, PRIMARY KEY (id));
>
> CREATE TABLE enum_value (id BIGSERIAL NOT NULL, StringEnum VARCHAR(10),  
> ENUMHOLDER_ID BIGINT, PRIMARY KEY (id));
>
> package com.jpa.test;
>
> import javax.persistence.*;
> import java.util.*;
>
> @Entity
> @Table (name="enum_holder")
> public class EnumHolder
>    implements java.io.Serializable
> {
>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>    @Id private long id;
>
>    @OrderBy
>    @OneToMany(mappedBy="enumHolder", fetch=FetchType.LAZY,
>               cascade={CascadeType.PERSIST,CascadeType.REMOVE})
>    private Set<EnumValue> enums;
>
>    public Set<EnumValue> getEnums()
>    {
>        if (enums == null)
>            enums = new HashSet<EnumValue>();
>        return enums;
>    }
> }
>
> package com.jpa.test;
>
> import javax.persistence.*;
>
> @Entity
> @Table (name="enum_value")
> public class EnumValue
>    implements java.io.Serializable
> {
>    public static enum MyEnum{foo, Bar, Batz, woohoo}
>
>    @GeneratedValue(strategy=GenerationType.IDENTITY)
>    @Id private long id;
>
>    @ManyToOne (optional=false, fetch=FetchType.LAZY,
>                cascade=CascadeType.PERSIST)
>    private EnumHolder enumHolder;
>
>    @Enumerated (EnumType.STRING)
>    @Column(name="StringEnum", length=10)
>    private MyEnum myEnum;
>
>    public MyEnum getMyEnum() { return myEnum; }
> }
>
>
>
> On 3/30/2009 9:47 AM, Tedman Leung wrote:
>>> How does your original example of ManyToMany annotation on a 
>>> Collection  of Enums actually work?  My understanding is that for 
>>> ManyToMany both  sides must be persistent Entities.  So therefore the 
>>> ManyToMany is  mapping the join table, not the Enum.
>>>     
>>
>> yes oddly enough it actually does work. As per my subsequent email, I  
>> switched it to @PersistentCollection and it performed the same way. I  
>> considered trying to annotate my Enum like an Entity but I figured that 
>> would be pushing expected behaviour... even if it worked, so I decided 
>> not to.
>>
>>
>>   
>>> Isn't the same thing true that OneToMany should annotate a Collection 
>>> of  Entities?  So the Enums would be a field on the ManyToOne side of 
>>> that  kind of mapping.  Then you can specify the @Column and 
>>> @Enumerated  annotations on the actual field or property.
>>>     
>>
>> no you can't, I tried it. I can't remember the specific error but it 
>> was along the lines of  - you're not allowed to specify a column 
>> attribute here.
>>
>>   
>>> Could you explain why using the @PersistentCollection annotation is   
>>> useful or necessary for OneToMany or ManyToMany collections generally 
>>> or  specifically in the case you are looking at?  I don't understand 
>>> the use  case that @PersistentCollection satisfies.
>>>     
>>
>> My understanding is @ManyToMany is a JPA standard and is only used to 
>> map entity to entities. 
>>
>> My understanding of @PersistentCollection is that is is an OpenJpa  
>> extention which allows people to map Entity to java Primities.
>>
>> i.e. 
>>
>> @Entity
>> class Bar
>> {
>> }
>>
>> @Entity
>> class Foo
>> {
>>    @ManyToMany
>>    HashSet<Bar> myBars=new HashSet<Bar>();
>>
>>    @PersistentCollection
>>    HashSet<String> myStrings=new HashSet<String>();
>> }
>>
>>
>> I guess there's a reason why the original JPA standard left out  
>> primitives... it gets complicated fast.
>>
>>
>>   
>>> - Paul
>>>
>>>
>>> On 3/29/2009 8:43 PM, Tedman Leung wrote:
>>>     
>>>> Just to document more thoughts and findings.
>>>>
>>>> 1) I was using the wrong annotation, I should have been using
>>>> @PersistentCollection
>>>>
>>>> 2) this seems to be a more wide spread problem with defining   
>>>> attributes to collections, i.e. - collection of enums needs to have 
>>>> @Enumerated configurations
>>>> - collection of Dates needs to have @Temporal configurations
>>>> - even collection of Strings seems to be missing a    
>>>> @Column(nullable=false, length=32) style annotation.
>>>>
>>>> I think the problem is much bigger and wide spread than I originaly though,
>>>> I don't think I'll be able to sort out a patch.
>>>>
>>>>
>>>> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>>>>         
>>>>> yes I know I can do manual hacks to get them stored the way I 
>>>>> want to, but I was asking if there was a jpa / openjpa annotation 
>>>>> to allow that.
>>>>>
>>>>> As an example, I can just use @Enumerated on a single enum, so it 
>>>>> seems logical that some one would have thought about storing a  
>>>>> collection of enums. 
>>>>>
>>>>> As for why I want them as strings instead of ordinals - the usual 
>>>>> reasons, i.e. it's safer for changes as the ordering won't  
>>>>> accidentally corrupt my data (and without telling me), and it's  
>>>>> easier to look into the database via sql or something and just 
>>>>> know what's going on / get simple reports etc.
>>>>>
>>>>> I'm guessing right now that it's not possible at all and that no 
>>>>> one has done anything towards this. I might have to rummage 
>>>>> through the source and try to see if I can figure out how it 
>>>>> works and hack a patch to submit. It seemed like a very straight 
>>>>> forward use case though so I'm surprised no one has asked or done 
>>>>> anything about this before.
>>>>>
>>>>>
>>>>>
>>>>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>>>>             
>>>>>> What is your objective?  Do you want some non-JPA application 
>>>>>> to see  them in the database as Strings?
>>>>>>
>>>>>> At some point you have add these Enums to the collection one at 
>>>>>> a time.   You can use an addEnum() method or an Entity listener 
>>>>>> to convert them to  Strings at that point one at a time. And a  
>>>>>> subclass of the Collection  type to getEnum() from the 
>>>>>> Collection when you fetch them back.
>>>>>>
>>>>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>>>>
>>>>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>>>>                 
>>>>>>> No, I'm talking about when the enum is in a collection.
>>>>>>>
>>>>>>> i.e.
>>>>>>>
>>>>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>>>>
>>>>>>> So no, either the @Enumerated helps, nor does calling name() 
>>>>>>> or  toString as neither are possible.
>>>>>>>
>>>>>>> I'm storing a Collection of enums , not a single Enum.
>>>>>>>
>>>>>>> There seems to be no examples of this nor any documentation 
>>>>>>> about the  ability to do this that I can find. The default 
>>>>>>> seems to use the ordinal value both for table generation and 
>>>>>>> storage value.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>>>>                       
>>>>>>>> Hi - This is from the OpenJPA relations example -
>>>>>>>>
>>>>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>>>>    private Gender gender;
>>>>>>>>
>>>>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>>>>
>>>>>>>>   public void setGender(Gender gender) {
>>>>>>>>        this.gender = gender;
>>>>>>>>   }
>>>>>>>>
>>>>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>>>>
>>>>>>>> - Paul
>>>>>>>>
>>>>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>>>>                             
>>>>>>>>> Ted,
>>>>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>>>>
>>>>>>>>> Catalina
>>>>>>>>>
>>>>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>>>>
>>>>>>>>>                                     
>>>>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>>>>
>>>>>>>>>> i.e.
>>>>>>>>>>        @ManyToMany
>>>>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>>>                                                           Ted Leung
>>>>>>>>>>                                                           tedman@sfu.ca
>>>>>>>>>>
>>>>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>>>>
>>>>>>>>>>                                             
>>>>>>>>>                                     
>>>>>>>                       
>>>>> -- 
>>>>>                                                            Ted Leung
>>>>>                                                            tedman@sfu.ca
>>>>>
>>>>> The most important words I've learned to say - "I don't know".
>>>>>             
>>>>         
>>
>>   
>

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

Being normal is vastly over rated.


Re: how to store collection of enums as strings

Posted by Paul Copeland <te...@jotobjects.com>.
Hi Ted - See example code below

On 3/30/2009 9:47 AM, Tedman Leung wrote:
>> Isn't the same thing true that OneToMany should annotate a Collection of  
>> Entities?  So the Enums would be a field on the ManyToOne side of that  
>> kind of mapping.  Then you can specify the @Column and @Enumerated  
>> annotations on the actual field or property.
>>     
>
> no you can't, I tried it. I can't remember the specific error but it was 
> along the lines of  - you're not allowed to specify a column attribute 
> here.
>   

Here is an example that uses the @Enumerated and @Column annotations on 
a OneToMany relation.

This stores a Set of Enums as String values.  A similar example would 
work for ManyToMany.  The same approach can also be used with 
Collections of Temporal values.

There are two classes - an EnumHolder that holds the Collection and an 
EnumValue that holds an Enum and stores it as a String.  Here is the 
MappingTool generated SQL (Postgres database).  As you can see the Enum 
is stored as a String VARCHAR(10).

CREATE TABLE enum_holder (id BIGSERIAL NOT NULL, PRIMARY KEY (id));

CREATE TABLE enum_value (id BIGSERIAL NOT NULL, StringEnum VARCHAR(10), 
ENUMHOLDER_ID BIGINT, PRIMARY KEY (id));

package com.jpa.test;

import javax.persistence.*;
import java.util.*;

@Entity
@Table (name="enum_holder")
public class EnumHolder
    implements java.io.Serializable
{
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id private long id;

    @OrderBy
    @OneToMany(mappedBy="enumHolder", fetch=FetchType.LAZY,
               cascade={CascadeType.PERSIST,CascadeType.REMOVE})
    private Set<EnumValue> enums;

    public Set<EnumValue> getEnums()
    {
        if (enums == null)
            enums = new HashSet<EnumValue>();
        return enums;
    }
}

package com.jpa.test;

import javax.persistence.*;

@Entity
@Table (name="enum_value")
public class EnumValue
    implements java.io.Serializable
{
    public static enum MyEnum{foo, Bar, Batz, woohoo}

    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id private long id;

    @ManyToOne (optional=false, fetch=FetchType.LAZY,
                cascade=CascadeType.PERSIST)
    private EnumHolder enumHolder;

    @Enumerated (EnumType.STRING)
    @Column(name="StringEnum", length=10)
    private MyEnum myEnum;

    public MyEnum getMyEnum() { return myEnum; }
}



On 3/30/2009 9:47 AM, Tedman Leung wrote:
>> How does your original example of ManyToMany annotation on a Collection  
>> of Enums actually work?  My understanding is that for ManyToMany both  
>> sides must be persistent Entities.  So therefore the ManyToMany is  
>> mapping the join table, not the Enum.
>>     
>
> yes oddly enough it actually does work. As per my subsequent email, I 
> switched it to @PersistentCollection and it performed the same way. I 
> considered trying to annotate my Enum like an Entity but I figured that 
> would be pushing expected behaviour... even if it worked, so I decided not 
> to.
>
>
>   
>> Isn't the same thing true that OneToMany should annotate a Collection of  
>> Entities?  So the Enums would be a field on the ManyToOne side of that  
>> kind of mapping.  Then you can specify the @Column and @Enumerated  
>> annotations on the actual field or property.
>>     
>
> no you can't, I tried it. I can't remember the specific error but it was 
> along the lines of  - you're not allowed to specify a column attribute 
> here.
>
>   
>> Could you explain why using the @PersistentCollection annotation is  
>> useful or necessary for OneToMany or ManyToMany collections generally or  
>> specifically in the case you are looking at?  I don't understand the use  
>> case that @PersistentCollection satisfies.
>>     
>
> My understanding is @ManyToMany is a JPA standard and is only used to map 
> entity to entities. 
>
> My understanding of @PersistentCollection is that is is an OpenJpa 
> extention which allows people to map Entity to java Primities.
>
> i.e. 
>
> @Entity
> class Bar
> {
> }
>
> @Entity
> class Foo
> {
>    @ManyToMany
>    HashSet<Bar> myBars=new HashSet<Bar>();
>
>    @PersistentCollection
>    HashSet<String> myStrings=new HashSet<String>();
> }
>
>
> I guess there's a reason why the original JPA standard left out 
> primitives... it gets complicated fast.
>
>
>   
>> - Paul
>>
>>
>> On 3/29/2009 8:43 PM, Tedman Leung wrote:
>>     
>>> Just to document more thoughts and findings.
>>>
>>> 1) I was using the wrong annotation, I should have been using
>>> @PersistentCollection
>>>
>>> 2) this seems to be a more wide spread problem with defining  
>>> attributes to collections, i.e. - collection of enums needs to have 
>>> @Enumerated configurations
>>> - collection of Dates needs to have @Temporal configurations
>>> - even collection of Strings seems to be missing a   
>>> @Column(nullable=false, length=32) style annotation.
>>>
>>> I think the problem is much bigger and wide spread than I originaly though,
>>> I don't think I'll be able to sort out a patch.
>>>
>>>
>>> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>>>   
>>>       
>>>> yes I know I can do manual hacks to get them stored the way I want 
>>>> to, but I was asking if there was a jpa / openjpa annotation to allow 
>>>> that.
>>>>
>>>> As an example, I can just use @Enumerated on a single enum, so it 
>>>> seems logical that some one would have thought about storing a 
>>>> collection of enums. 
>>>>
>>>> As for why I want them as strings instead of ordinals - the usual 
>>>> reasons, i.e. it's safer for changes as the ordering won't 
>>>> accidentally corrupt my data (and without telling me), and it's 
>>>> easier to look into the database via sql or something and just know 
>>>> what's going on / get simple reports etc.
>>>>
>>>> I'm guessing right now that it's not possible at all and that no one 
>>>> has done anything towards this. I might have to rummage through the 
>>>> source and try to see if I can figure out how it works and hack a 
>>>> patch to submit. It seemed like a very straight forward use case 
>>>> though so I'm surprised no one has asked or done anything about this 
>>>> before.
>>>>
>>>>
>>>>
>>>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>>>     
>>>>         
>>>>> What is your objective?  Do you want some non-JPA application to 
>>>>> see  them in the database as Strings?
>>>>>
>>>>> At some point you have add these Enums to the collection one at a 
>>>>> time.   You can use an addEnum() method or an Entity listener to 
>>>>> convert them to  Strings at that point one at a time. And a 
>>>>> subclass of the Collection  type to getEnum() from the Collection 
>>>>> when you fetch them back.
>>>>>
>>>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>>>
>>>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>>>       
>>>>>           
>>>>>> No, I'm talking about when the enum is in a collection.
>>>>>>
>>>>>> i.e.
>>>>>>
>>>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>>>
>>>>>> So no, either the @Enumerated helps, nor does calling name() or  
>>>>>> toString as neither are possible.
>>>>>>
>>>>>> I'm storing a Collection of enums , not a single Enum.
>>>>>>
>>>>>> There seems to be no examples of this nor any documentation about 
>>>>>> the  ability to do this that I can find. The default seems to use 
>>>>>> the ordinal value both for table generation and storage value.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>>>           
>>>>>>             
>>>>>>> Hi - This is from the OpenJPA relations example -
>>>>>>>
>>>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>>>    private Gender gender;
>>>>>>>
>>>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>>>
>>>>>>>   public void setGender(Gender gender) {
>>>>>>>        this.gender = gender;
>>>>>>>   }
>>>>>>>
>>>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>>>
>>>>>>> - Paul
>>>>>>>
>>>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>>>               
>>>>>>>               
>>>>>>>> Ted,
>>>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>>>
>>>>>>>> Catalina
>>>>>>>>
>>>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>>>
>>>>>>>>                     
>>>>>>>>                 
>>>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>>>
>>>>>>>>> i.e.
>>>>>>>>>        @ManyToMany
>>>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> --
>>>>>>>>>                                                           Ted Leung
>>>>>>>>>                                                           tedman@sfu.ca
>>>>>>>>>
>>>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>>>
>>>>>>>>>                           
>>>>>>>>>                   
>>>>>>>>                     
>>>>>>>>                 
>>>>>>           
>>>>>>             
>>>> -- 
>>>>                                                            Ted Leung
>>>>                                                            tedman@sfu.ca
>>>>
>>>> The most important words I've learned to say - "I don't know".
>>>>     
>>>>         
>>>   
>>>       
>
>   


Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
> How does your original example of ManyToMany annotation on a Collection  
> of Enums actually work?  My understanding is that for ManyToMany both  
> sides must be persistent Entities.  So therefore the ManyToMany is  
> mapping the join table, not the Enum.

yes oddly enough it actually does work. As per my subsequent email, I 
switched it to @PersistentCollection and it performed the same way. I 
considered trying to annotate my Enum like an Entity but I figured that 
would be pushing expected behaviour... even if it worked, so I decided not 
to.


> Isn't the same thing true that OneToMany should annotate a Collection of  
> Entities?  So the Enums would be a field on the ManyToOne side of that  
> kind of mapping.  Then you can specify the @Column and @Enumerated  
> annotations on the actual field or property.

no you can't, I tried it. I can't remember the specific error but it was 
along the lines of  - you're not allowed to specify a column attribute 
here.

> Could you explain why using the @PersistentCollection annotation is  
> useful or necessary for OneToMany or ManyToMany collections generally or  
> specifically in the case you are looking at?  I don't understand the use  
> case that @PersistentCollection satisfies.

My understanding is @ManyToMany is a JPA standard and is only used to map 
entity to entities. 

My understanding of @PersistentCollection is that is is an OpenJpa 
extention which allows people to map Entity to java Primities.

i.e. 

@Entity
class Bar
{
}

@Entity
class Foo
{
   @ManyToMany
   HashSet<Bar> myBars=new HashSet<Bar>();

   @PersistentCollection
   HashSet<String> myStrings=new HashSet<String>();
}


I guess there's a reason why the original JPA standard left out 
primitives... it gets complicated fast.


> - Paul
>
>
> On 3/29/2009 8:43 PM, Tedman Leung wrote:
>> Just to document more thoughts and findings.
>>
>> 1) I was using the wrong annotation, I should have been using
>> @PersistentCollection
>>
>> 2) this seems to be a more wide spread problem with defining  
>> attributes to collections, i.e. - collection of enums needs to have 
>> @Enumerated configurations
>> - collection of Dates needs to have @Temporal configurations
>> - even collection of Strings seems to be missing a   
>> @Column(nullable=false, length=32) style annotation.
>>
>> I think the problem is much bigger and wide spread than I originaly though,
>> I don't think I'll be able to sort out a patch.
>>
>>
>> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>>   
>>> yes I know I can do manual hacks to get them stored the way I want 
>>> to, but I was asking if there was a jpa / openjpa annotation to allow 
>>> that.
>>>
>>> As an example, I can just use @Enumerated on a single enum, so it 
>>> seems logical that some one would have thought about storing a 
>>> collection of enums. 
>>>
>>> As for why I want them as strings instead of ordinals - the usual 
>>> reasons, i.e. it's safer for changes as the ordering won't 
>>> accidentally corrupt my data (and without telling me), and it's 
>>> easier to look into the database via sql or something and just know 
>>> what's going on / get simple reports etc.
>>>
>>> I'm guessing right now that it's not possible at all and that no one 
>>> has done anything towards this. I might have to rummage through the 
>>> source and try to see if I can figure out how it works and hack a 
>>> patch to submit. It seemed like a very straight forward use case 
>>> though so I'm surprised no one has asked or done anything about this 
>>> before.
>>>
>>>
>>>
>>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>>     
>>>> What is your objective?  Do you want some non-JPA application to 
>>>> see  them in the database as Strings?
>>>>
>>>> At some point you have add these Enums to the collection one at a 
>>>> time.   You can use an addEnum() method or an Entity listener to 
>>>> convert them to  Strings at that point one at a time. And a 
>>>> subclass of the Collection  type to getEnum() from the Collection 
>>>> when you fetch them back.
>>>>
>>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>>
>>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>>       
>>>>> No, I'm talking about when the enum is in a collection.
>>>>>
>>>>> i.e.
>>>>>
>>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>>
>>>>> So no, either the @Enumerated helps, nor does calling name() or  
>>>>> toString as neither are possible.
>>>>>
>>>>> I'm storing a Collection of enums , not a single Enum.
>>>>>
>>>>> There seems to be no examples of this nor any documentation about 
>>>>> the  ability to do this that I can find. The default seems to use 
>>>>> the ordinal value both for table generation and storage value.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>>           
>>>>>> Hi - This is from the OpenJPA relations example -
>>>>>>
>>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>>    private Gender gender;
>>>>>>
>>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>>
>>>>>>   public void setGender(Gender gender) {
>>>>>>        this.gender = gender;
>>>>>>   }
>>>>>>
>>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>>
>>>>>> - Paul
>>>>>>
>>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>>               
>>>>>>> Ted,
>>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>>
>>>>>>> Catalina
>>>>>>>
>>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>>
>>>>>>>                     
>>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>>
>>>>>>>> i.e.
>>>>>>>>        @ManyToMany
>>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>>                                                           Ted Leung
>>>>>>>>                                                           tedman@sfu.ca
>>>>>>>>
>>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>>
>>>>>>>>                           
>>>>>>>                     
>>>>>           
>>> -- 
>>>                                                            Ted Leung
>>>                                                            tedman@sfu.ca
>>>
>>> The most important words I've learned to say - "I don't know".
>>>     
>>
>>   
>

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

It's time for a new bike when the bulb in your shift light burns out.

Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
yes! amazing!


	@PersistentCollection(elementType=String.class)
	@Externalizer("User.rolesToStrings")
	@Factory("User.rolesFromStrings")
	private HashSet<Role> testRoles=new HashSet<Role>();

works exactly like I want it to...

except that it also has problems with the eager fetching which I don't 
quite understand. As a result I've resorted to sticking with my 
nativeQuery work around. The eager fetching error is attached for anyone 
who is really interested :


openjpa exception :
-------
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: java.lang.String cannot be cast to 
org.apache.openjpa.enhance.PersistenceCapable
	at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:875)
	at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:774)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:982)
	at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:278)
	at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2400)
	at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:272)
	at org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:59)
	at org.apache.openjpa.datacache.QueryCacheStoreQuery$CachingResultObjectProvider.getResultObject(QueryCacheStoreQuery.java:597)
	at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
	at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1228)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:990)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:805)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:775)
	at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:533)
	at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:252)
	at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:294)
	... 26 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.openjpa.enhance.PersistenceCapable
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.setInverseRelation(JDBCStoreManager.java:408)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initializeState(JDBCStoreManager.java:380)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.initialize(JDBCStoreManager.java:278)
	at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
	at org.apache.openjpa.datacache.DataCacheStoreManager.initialize(DataCacheStoreManager.java:352)
	at org.apache.openjpa.kernel.DelegatingStoreManager.initialize(DelegatingStoreManager.java:111)
	at org.apache.openjpa.kernel.ROPStoreManager.initialize(ROPStoreManager.java:57)
	at org.apache.openjpa.kernel.BrokerImpl.initialize(BrokerImpl.java:894)
	at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:852)
	... 59 more




On Tue, Mar 31, 2009 at 10:46:03AM -0500, Jody Grassel wrote:
> It sounds like you're trying to persist a collection of enumerations as a
> set of data and not as a reference to other entities, is that correct?
> 
> If so, wouldn't a combination of @PersistentCollection and Externalization
> (section 6.6 in the manual) provide the function you are looking for?
> 
> 
> On Wed, Mar 25, 2009 at 11:48 AM, Tedman Leung <te...@sfu.ca> wrote:
> 
> > Anyone know how to store a collection of enums as Strings instead of their
> > ordinal values? (preferably with annotations...)
> >
> > i.e.
> >        @ManyToMany
> >        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
> >
> >
> > --
> >                                                           Ted Leung
> >                                                           tedman@sfu.ca
> >
> > It's time for a new bike when the bulb in your shift light burns out.
> >

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

Being normal is vastly over rated.


Re: how to store collection of enums as strings

Posted by Paul Copeland <te...@jotobjects.com>.
Hi Ted - A few more questions to fill out my knowledge -

How does your original example of ManyToMany annotation on a Collection 
of Enums actually work?  My understanding is that for ManyToMany both 
sides must be persistent Entities.  So therefore the ManyToMany is 
mapping the join table, not the Enum.

Isn't the same thing true that OneToMany should annotate a Collection of 
Entities?  So the Enums would be a field on the ManyToOne side of that 
kind of mapping.  Then you can specify the @Column and @Enumerated 
annotations on the actual field or property.

Could you explain why using the @PersistentCollection annotation is 
useful or necessary for OneToMany or ManyToMany collections generally or 
specifically in the case you are looking at?  I don't understand the use 
case that @PersistentCollection satisfies.
 
- Paul


On 3/29/2009 8:43 PM, Tedman Leung wrote:
> Just to document more thoughts and findings.
>
> 1) I was using the wrong annotation, I should have been using
> @PersistentCollection
>
> 2) this seems to be a more wide spread problem with defining 
> attributes to collections, i.e. 
> - collection of enums needs to have @Enumerated configurations
> - collection of Dates needs to have @Temporal configurations
> - even collection of Strings seems to be missing a 
>   @Column(nullable=false, length=32) style annotation.
>
> I think the problem is much bigger and wide spread than I originaly though,
> I don't think I'll be able to sort out a patch.
>
>
> On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
>   
>> yes I know I can do manual hacks to get them stored the way I want to, but 
>> I was asking if there was a jpa / openjpa annotation to allow that.
>>
>> As an example, I can just use @Enumerated on a single enum, so it seems 
>> logical that some one would have thought about storing a collection of 
>> enums. 
>>
>> As for why I want them as strings instead of ordinals - the usual reasons, 
>> i.e. it's safer for changes as the ordering won't accidentally corrupt my 
>> data (and without telling me), and it's easier to look into the database 
>> via sql or something and just know what's going on / get simple reports 
>> etc.
>>
>> I'm guessing right now that it's not possible at all and that no one has 
>> done anything towards this. I might have to rummage through the source 
>> and try to see if I can figure out how it works and hack a patch to 
>> submit. It seemed like a very straight forward use case though so I'm 
>> surprised no one has asked or done anything about this before.
>>
>>
>>
>> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
>>     
>>> What is your objective?  Do you want some non-JPA application to see  
>>> them in the database as Strings?
>>>
>>> At some point you have add these Enums to the collection one at a time.   
>>> You can use an addEnum() method or an Entity listener to convert them to  
>>> Strings at that point one at a time. And a subclass of the Collection  
>>> type to getEnum() from the Collection when you fetch them back.
>>>
>>> new MyStringEnumHashSet<MyStringEnumType>()
>>>
>>> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>>>       
>>>> No, I'm talking about when the enum is in a collection.
>>>>
>>>> i.e.
>>>>
>>>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>>>
>>>> So no, either the @Enumerated helps, nor does calling name() or 
>>>> toString as neither are possible.
>>>>
>>>> I'm storing a Collection of enums , not a single Enum.
>>>>
>>>> There seems to be no examples of this nor any documentation about the  
>>>> ability to do this that I can find. The default seems to use the 
>>>> ordinal value both for table generation and storage value.
>>>>
>>>>
>>>>
>>>>
>>>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>>>   
>>>>         
>>>>> Hi - This is from the OpenJPA relations example -
>>>>>
>>>>>    @Basic @Enumerated(EnumType.STRING)
>>>>>    private Gender gender;
>>>>>
>>>>>    public static enum Gender { MALE, FEMALE }
>>>>>
>>>>>   public void setGender(Gender gender) {
>>>>>        this.gender = gender;
>>>>>   }
>>>>>
>>>>> See section 12.8.1.2 in the OpenJPA Overview
>>>>>
>>>>> - Paul
>>>>>
>>>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>>>     
>>>>>           
>>>>>> Ted,
>>>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>>>> Enum to get the name of the enum constant in String value.
>>>>>>
>>>>>> Catalina
>>>>>>
>>>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>>>
>>>>>>         
>>>>>>             
>>>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>>>> ordinal values? (preferably with annotations...)
>>>>>>>
>>>>>>> i.e.
>>>>>>>        @ManyToMany
>>>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>                                                           Ted Leung
>>>>>>>                                                           tedman@sfu.ca
>>>>>>>
>>>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>>>
>>>>>>>             
>>>>>>>               
>>>>>>         
>>>>>>             
>>>>   
>>>>         
>> -- 
>>                                                            Ted Leung
>>                                                            tedman@sfu.ca
>>
>> The most important words I've learned to say - "I don't know".
>>     
>
>   


bug in @PersistentCollection(fetch=FetchType.EAGER)

Posted by Tedman Leung <te...@sfu.ca>.
I think I've found a bug in @PersistentCollection(fetch=FetchType.EAGER) 
when used with Strings.  It just doesn't seem to retrieve.

This only happens with FetchType.EAGER and it only happens if the entity 
is being loaded from the database / not cached via openjpa.DataCache. (If 
I create the entity then persist it, then do a find on it, it'll work, I 
think it's because it's just hitting my cache, if I shutdown the jvm, 
then start it up again, then load the entity I see the error.)

The declaration of the column is as follows : 

	@PersistentCollection(fetch=FetchType.EAGER)
	@ContainerTable(name="UserRole", joinColumns=@XJoinColumn(name="userId", referencedColumnName="id"), joinForeignKey=@ForeignKey)
	@ElementJoinColumn(name="Role")
	private Set<String> roles=new HashSet<String>();

I'm using compile time enhancement.
I'm using java :
	java version "1.6.0_12"
	Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
	Java HotSpot(TM) Server VM (build 11.2-b01, mixed mode)

The openjpa is 1.2.1 as retrieved via the apache maven repositories.

The exception is as follows : 
-----------------------------
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal general error> 
org.apache.openjpa.persistence.PersistenceException: java.lang.String 
cannot be cast to org.apache.openjpa.enhance.PersistenceCapable
	at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:875)
	at org.apache.openjpa.kernel.BrokerImpl.find(BrokerImpl.java:774)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:982)
	at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:278)
	at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2400)
	at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:272)
	at org.apache.openjpa.jdbc.kernel.InstanceResultObjectProvider.getResultObject(InstanceResultObjectProvider.java:59)
	at org.apache.openjpa.datacache.QueryCacheStoreQuery$CachingResultObjectProvider.getResultObject(QueryCacheStoreQuery.java:597)
	at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
	at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1228)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:990)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:805)
	at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:775)
	at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:533)
	at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:252)
	at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:294)
 
-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

It is easier to speak wisely than to act wisely.

Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
Just to document more thoughts and findings.

1) I was using the wrong annotation, I should have been using
@PersistentCollection

2) this seems to be a more wide spread problem with defining 
attributes to collections, i.e. 
- collection of enums needs to have @Enumerated configurations
- collection of Dates needs to have @Temporal configurations
- even collection of Strings seems to be missing a 
  @Column(nullable=false, length=32) style annotation.

I think the problem is much bigger and wide spread than I originaly though,
I don't think I'll be able to sort out a patch.


On Sun, Mar 29, 2009 at 07:55:06AM -0700, Tedman Leung wrote:
> yes I know I can do manual hacks to get them stored the way I want to, but 
> I was asking if there was a jpa / openjpa annotation to allow that.
> 
> As an example, I can just use @Enumerated on a single enum, so it seems 
> logical that some one would have thought about storing a collection of 
> enums. 
> 
> As for why I want them as strings instead of ordinals - the usual reasons, 
> i.e. it's safer for changes as the ordering won't accidentally corrupt my 
> data (and without telling me), and it's easier to look into the database 
> via sql or something and just know what's going on / get simple reports 
> etc.
> 
> I'm guessing right now that it's not possible at all and that no one has 
> done anything towards this. I might have to rummage through the source 
> and try to see if I can figure out how it works and hack a patch to 
> submit. It seemed like a very straight forward use case though so I'm 
> surprised no one has asked or done anything about this before.
> 
> 
> 
> On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
> > What is your objective?  Do you want some non-JPA application to see  
> > them in the database as Strings?
> >
> > At some point you have add these Enums to the collection one at a time.   
> > You can use an addEnum() method or an Entity listener to convert them to  
> > Strings at that point one at a time. And a subclass of the Collection  
> > type to getEnum() from the Collection when you fetch them back.
> >
> > new MyStringEnumHashSet<MyStringEnumType>()
> >
> > On 3/28/2009 9:27 PM, Tedman Leung wrote:
> >> No, I'm talking about when the enum is in a collection.
> >>
> >> i.e.
> >>
> >>    Private HashSet<Gender> genders=new HashSet<Gender>();
> >>
> >> So no, either the @Enumerated helps, nor does calling name() or 
> >> toString as neither are possible.
> >>
> >> I'm storing a Collection of enums , not a single Enum.
> >>
> >> There seems to be no examples of this nor any documentation about the  
> >> ability to do this that I can find. The default seems to use the 
> >> ordinal value both for table generation and storage value.
> >>
> >>
> >>
> >>
> >> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
> >>   
> >>> Hi - This is from the OpenJPA relations example -
> >>>
> >>>    @Basic @Enumerated(EnumType.STRING)
> >>>    private Gender gender;
> >>>
> >>>    public static enum Gender { MALE, FEMALE }
> >>>
> >>>   public void setGender(Gender gender) {
> >>>        this.gender = gender;
> >>>   }
> >>>
> >>> See section 12.8.1.2 in the OpenJPA Overview
> >>>
> >>> - Paul
> >>>
> >>> On 3/28/2009 1:33 PM, catalina wei wrote:
> >>>     
> >>>> Ted,
> >>>> If you are using Java 5, then you could use name() or toString() API on the
> >>>> Enum to get the name of the enum constant in String value.
> >>>>
> >>>> Catalina
> >>>>
> >>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
> >>>>
> >>>>         
> >>>>> Anyone know how to store a collection of enums as Strings instead of their
> >>>>> ordinal values? (preferably with annotations...)
> >>>>>
> >>>>> i.e.
> >>>>>        @ManyToMany
> >>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
> >>>>>
> >>>>>
> >>>>> --
> >>>>>                                                           Ted Leung
> >>>>>                                                           tedman@sfu.ca
> >>>>>
> >>>>> It's time for a new bike when the bulb in your shift light burns out.
> >>>>>
> >>>>>             
> >>>>         
> >>
> >>   
> >
> 
> -- 
>                                                            Ted Leung
>                                                            tedman@sfu.ca
> 
> The most important words I've learned to say - "I don't know".

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

It is easier to speak wisely than to act wisely.

Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
yes I know I can do manual hacks to get them stored the way I want to, but 
I was asking if there was a jpa / openjpa annotation to allow that.

As an example, I can just use @Enumerated on a single enum, so it seems 
logical that some one would have thought about storing a collection of 
enums. 

As for why I want them as strings instead of ordinals - the usual reasons, 
i.e. it's safer for changes as the ordering won't accidentally corrupt my 
data (and without telling me), and it's easier to look into the database 
via sql or something and just know what's going on / get simple reports 
etc.

I'm guessing right now that it's not possible at all and that no one has 
done anything towards this. I might have to rummage through the source 
and try to see if I can figure out how it works and hack a patch to 
submit. It seemed like a very straight forward use case though so I'm 
surprised no one has asked or done anything about this before.



On Sat, Mar 28, 2009 at 10:13:16PM -0700, Paul Copeland wrote:
> What is your objective?  Do you want some non-JPA application to see  
> them in the database as Strings?
>
> At some point you have add these Enums to the collection one at a time.   
> You can use an addEnum() method or an Entity listener to convert them to  
> Strings at that point one at a time. And a subclass of the Collection  
> type to getEnum() from the Collection when you fetch them back.
>
> new MyStringEnumHashSet<MyStringEnumType>()
>
> On 3/28/2009 9:27 PM, Tedman Leung wrote:
>> No, I'm talking about when the enum is in a collection.
>>
>> i.e.
>>
>>    Private HashSet<Gender> genders=new HashSet<Gender>();
>>
>> So no, either the @Enumerated helps, nor does calling name() or 
>> toString as neither are possible.
>>
>> I'm storing a Collection of enums , not a single Enum.
>>
>> There seems to be no examples of this nor any documentation about the  
>> ability to do this that I can find. The default seems to use the 
>> ordinal value both for table generation and storage value.
>>
>>
>>
>>
>> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>>   
>>> Hi - This is from the OpenJPA relations example -
>>>
>>>    @Basic @Enumerated(EnumType.STRING)
>>>    private Gender gender;
>>>
>>>    public static enum Gender { MALE, FEMALE }
>>>
>>>   public void setGender(Gender gender) {
>>>        this.gender = gender;
>>>   }
>>>
>>> See section 12.8.1.2 in the OpenJPA Overview
>>>
>>> - Paul
>>>
>>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>>     
>>>> Ted,
>>>> If you are using Java 5, then you could use name() or toString() API on the
>>>> Enum to get the name of the enum constant in String value.
>>>>
>>>> Catalina
>>>>
>>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>>
>>>>         
>>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>>> ordinal values? (preferably with annotations...)
>>>>>
>>>>> i.e.
>>>>>        @ManyToMany
>>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>>
>>>>>
>>>>> --
>>>>>                                                           Ted Leung
>>>>>                                                           tedman@sfu.ca
>>>>>
>>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>>
>>>>>             
>>>>         
>>
>>   
>

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

The most important words I've learned to say - "I don't know".

Re: how to store collection of enums as strings

Posted by Paul Copeland <te...@jotobjects.com>.
What is your objective?  Do you want some non-JPA application to see 
them in the database as Strings?

At some point you have add these Enums to the collection one at a time.  
You can use an addEnum() method or an Entity listener to convert them to 
Strings at that point one at a time. And a subclass of the Collection 
type to getEnum() from the Collection when you fetch them back.

new MyStringEnumHashSet<MyStringEnumType>()

On 3/28/2009 9:27 PM, Tedman Leung wrote:
> No, I'm talking about when the enum is in a collection.
>
> i.e.
>
>    Private HashSet<Gender> genders=new HashSet<Gender>();
>
> So no, either the @Enumerated helps, nor does calling name() or toString 
> as neither are possible.
>
> I'm storing a Collection of enums , not a single Enum.
>
> There seems to be no examples of this nor any documentation about the 
> ability to do this that I can find. The default seems to use the ordinal 
> value both for table generation and storage value.
>
>
>
>
> On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
>   
>> Hi - This is from the OpenJPA relations example -
>>
>>    @Basic @Enumerated(EnumType.STRING)
>>    private Gender gender;
>>
>>    public static enum Gender { MALE, FEMALE }
>>
>>   public void setGender(Gender gender) {
>>        this.gender = gender;
>>   }
>>
>> See section 12.8.1.2 in the OpenJPA Overview
>>
>> - Paul
>>
>> On 3/28/2009 1:33 PM, catalina wei wrote:
>>     
>>> Ted,
>>> If you are using Java 5, then you could use name() or toString() API on the
>>> Enum to get the name of the enum constant in String value.
>>>
>>> Catalina
>>>
>>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>>
>>>   
>>>       
>>>> Anyone know how to store a collection of enums as Strings instead of their
>>>> ordinal values? (preferably with annotations...)
>>>>
>>>> i.e.
>>>>        @ManyToMany
>>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>>
>>>>
>>>> --
>>>>                                                           Ted Leung
>>>>                                                           tedman@sfu.ca
>>>>
>>>> It's time for a new bike when the bulb in your shift light burns out.
>>>>
>>>>     
>>>>         
>>>   
>>>       
>
>   


Re: how to store collection of enums as strings

Posted by Tedman Leung <te...@sfu.ca>.
No, I'm talking about when the enum is in a collection.

i.e.

   Private HashSet<Gender> genders=new HashSet<Gender>();

So no, either the @Enumerated helps, nor does calling name() or toString 
as neither are possible.

I'm storing a Collection of enums , not a single Enum.

There seems to be no examples of this nor any documentation about the 
ability to do this that I can find. The default seems to use the ordinal 
value both for table generation and storage value.




On Sat, Mar 28, 2009 at 01:56:13PM -0700, Paul Copeland wrote:
> Hi - This is from the OpenJPA relations example -
>
>    @Basic @Enumerated(EnumType.STRING)
>    private Gender gender;
>
>    public static enum Gender { MALE, FEMALE }
>
>   public void setGender(Gender gender) {
>        this.gender = gender;
>   }
>
> See section 12.8.1.2 in the OpenJPA Overview
>
> - Paul
>
> On 3/28/2009 1:33 PM, catalina wei wrote:
>> Ted,
>> If you are using Java 5, then you could use name() or toString() API on the
>> Enum to get the name of the enum constant in String value.
>>
>> Catalina
>>
>> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>
>>   
>>> Anyone know how to store a collection of enums as Strings instead of their
>>> ordinal values? (preferably with annotations...)
>>>
>>> i.e.
>>>        @ManyToMany
>>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>
>>>
>>> --
>>>                                                           Ted Leung
>>>                                                           tedman@sfu.ca
>>>
>>> It's time for a new bike when the bulb in your shift light burns out.
>>>
>>>     
>>
>>   
>

-- 
                                                           Ted Leung
                                                           tedman@sfu.ca

// /*
You know things are getting a little fishy when you're commenting out 
comments.
// */

Re: how to store collection of enums as strings

Posted by Paul Copeland <te...@jotobjects.com>.
Hi - This is from the OpenJPA relations example -

    @Basic @Enumerated(EnumType.STRING)
    private Gender gender;

    public static enum Gender { MALE, FEMALE }

   public void setGender(Gender gender) {
        this.gender = gender;
   }

See section 12.8.1.2 in the OpenJPA Overview

- Paul

On 3/28/2009 1:33 PM, catalina wei wrote:
> Ted,
> If you are using Java 5, then you could use name() or toString() API on the
> Enum to get the name of the enum constant in String value.
>
> Catalina
>
> On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>
>   
>> Anyone know how to store a collection of enums as Strings instead of their
>> ordinal values? (preferably with annotations...)
>>
>> i.e.
>>        @ManyToMany
>>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>
>>
>> --
>>                                                           Ted Leung
>>                                                           tedman@sfu.ca
>>
>> It's time for a new bike when the bulb in your shift light burns out.
>>
>>     
>
>   


Re: how to store collection of enums as strings

Posted by catalina wei <ca...@gmail.com>.
Ted,
If you are using Java 5, then you could use name() or toString() API on the
Enum to get the name of the enum constant in String value.

Catalina

On Wed, Mar 25, 2009 at 9:48 AM, Tedman Leung <te...@sfu.ca> wrote:

> Anyone know how to store a collection of enums as Strings instead of their
> ordinal values? (preferably with annotations...)
>
> i.e.
>        @ManyToMany
>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>
>
> --
>                                                           Ted Leung
>                                                           tedman@sfu.ca
>
> It's time for a new bike when the bulb in your shift light burns out.
>

Re: how to store collection of enums as strings

Posted by Jeremy Bauer <te...@gmail.com>.
Craig,

The latest public draft (3/13/2009) of the JPA 2.0 specification defines the
ability to apply the Enumeration (also Temporal and Lob) annotation to
element collection.  Support within OpenJPA trunk will follow in the weeks
to come.

-Jeremy

On Tue, Mar 31, 2009 at 12:04 PM, Craig L Russell <Cr...@sun.com>wrote:

> I'd like to see a simple way to persist an EnumSet (which if I understand
> this use case is really what is desired):
>
> @Enumerated(STRING)
> @ElementCollection
> private EnumSet<MyEnum> myEnums = new EnumSet<MyEnum>();
>
> If no volunteers to implement this one (too bad it's not in the
> specification) then this would be my second choice:
>
> @Enumerated(STRING)
> @ElementCollection
> private Set<MyEnum> myEnums = new HashSet<MyEnum>();
>
> The @Enumerated tells us that you want the String value of the enum to be
> persisted (not the int value); the @ElementCollection tells us to persist
> the values individually and not as a serialized blob.
>
> Craig
>
>
> On Mar 31, 2009, at 8:46 AM, Jody Grassel wrote:
>
>  It sounds like you're trying to persist a collection of enumerations as a
>> set of data and not as a reference to other entities, is that correct?
>>
>> If so, wouldn't a combination of @PersistentCollection and Externalization
>> (section 6.6 in the manual) provide the function you are looking for?
>>
>>
>> On Wed, Mar 25, 2009 at 11:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>>
>>  Anyone know how to store a collection of enums as Strings instead of
>>> their
>>> ordinal values? (preferably with annotations...)
>>>
>>> i.e.
>>>      @ManyToMany
>>>      private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>>
>>>
>>> --
>>>                                                         Ted Leung
>>>                                                         tedman@sfu.ca
>>>
>>> It's time for a new bike when the bulb in your shift light burns out.
>>>
>>>
> Craig L Russell
> Architect, Sun Java Enterprise System http://db.apache.org/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>
>

Re: how to store collection of enums as strings

Posted by Craig L Russell <Cr...@Sun.COM>.
I'd like to see a simple way to persist an EnumSet (which if I  
understand this use case is really what is desired):

@Enumerated(STRING)
@ElementCollection
private EnumSet<MyEnum> myEnums = new EnumSet<MyEnum>();

If no volunteers to implement this one (too bad it's not in the  
specification) then this would be my second choice:

@Enumerated(STRING)
@ElementCollection
private Set<MyEnum> myEnums = new HashSet<MyEnum>();

The @Enumerated tells us that you want the String value of the enum to  
be persisted (not the int value); the @ElementCollection tells us to  
persist the values individually and not as a serialized blob.

Craig

On Mar 31, 2009, at 8:46 AM, Jody Grassel wrote:

> It sounds like you're trying to persist a collection of enumerations  
> as a
> set of data and not as a reference to other entities, is that correct?
>
> If so, wouldn't a combination of @PersistentCollection and  
> Externalization
> (section 6.6 in the manual) provide the function you are looking for?
>
>
> On Wed, Mar 25, 2009 at 11:48 AM, Tedman Leung <te...@sfu.ca> wrote:
>
>> Anyone know how to store a collection of enums as Strings instead  
>> of their
>> ordinal values? (preferably with annotations...)
>>
>> i.e.
>>       @ManyToMany
>>       private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>>
>>
>> --
>>                                                          Ted Leung
>>                                                           
>> tedman@sfu.ca
>>
>> It's time for a new bike when the bulb in your shift light burns out.
>>

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: how to store collection of enums as strings

Posted by Jody Grassel <fy...@gmail.com>.
It sounds like you're trying to persist a collection of enumerations as a
set of data and not as a reference to other entities, is that correct?

If so, wouldn't a combination of @PersistentCollection and Externalization
(section 6.6 in the manual) provide the function you are looking for?


On Wed, Mar 25, 2009 at 11:48 AM, Tedman Leung <te...@sfu.ca> wrote:

> Anyone know how to store a collection of enums as Strings instead of their
> ordinal values? (preferably with annotations...)
>
> i.e.
>        @ManyToMany
>        private Set<MyEnum> myEnums=new HashSet<MyEnum>();
>
>
> --
>                                                           Ted Leung
>                                                           tedman@sfu.ca
>
> It's time for a new bike when the bulb in your shift light burns out.
>