You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Matthew Adams <ma...@matthewadams.me> on 2010/04/14 18:59:36 UTC
OpenJPA 1.2.x & map key as enum stored as string (probably a FAQ)
Hi all,
Sorry if this is a FAQ. I can't seem to find it in the docs or googling.
I have the following scenario and just need to know which
OpenJPA-specific annotations to apply to my field. The scenario is a
field of type Map where the map key is an enum that I want to store as
@Enumerated(EnumType.STRING) and is not an attribute of the values in
the map using OpenJPA 1.2.x & JPA 1.0 (can't go to JPA 2.0 yet).
Assume all classes use a simple @Id field of type Long.
public enum Place { HOUSE, CAVE, LEAN_TO, BARE_SPOT; }
@Entity
public class WildThing {
/* NOTE: does **not** have a field of type Place! */
@Id
protected Long id;
protected String name;
}
@Entity
public class WhereTheWildThingsAre {
@Id
protected Long id;
// what annotation(s) do I put here?
@OneToMany(cascade =
{ CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST })
protected Map<Place,WildThing> thingsByClassification
= new HashMap<Place,WildThing>;
}
Again, I want the key to be stored as the string representation of the
enum constant.
TIA,
Matthew
--
mailto:matthew@matthewadams.me
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12@gmail.com
msn:matthew@matthewadams.me
http://matthewadams.me
http://www.linkedin.com/in/matthewadams
Re: OpenJPA 1.2.x & map key as enum stored as string (probably a FAQ)
Posted by Patrick Linskey <pl...@gmail.com>.
> Yes, I've tried that & OpenJPA wants to use the enum's ordinal instead
> of its string representation. How do I force the use of its string
> representation?
In JPA2, you could use the EnumType annotation.
In earlier versions, it looks like you could either change the default
across the board via re-configuring the PersistenceMapping (a bit
invasive), or you could do something like
@KeyStrategy("StoreOrdinal=false")
> PS: Who's Max? ;)
http://amzn.to/10UcX2
-Patrick
On Wed, Apr 14, 2010 at 1:17 PM, Matthew Adams <ma...@matthewadams.me> wrote:
> Yes, I've tried that & OpenJPA wants to use the enum's ordinal instead
> of its string representation. How do I force the use of its string
> representation?
>
> Thanks,
> Matthew
>
> PS: Who's Max? ;)
>
> On Wed, Apr 14, 2010 at 10:56 AM, Patrick Linskey <pl...@gmail.com> wrote:
>> Hi, Max!
>>
>> I think that @PersistentMap is your friend here. Have you tried that annotation?
>>
>> Happy sailing,
>>
>> -Patrick
>>
>> On Wed, Apr 14, 2010 at 9:59 AM, Matthew Adams <ma...@matthewadams.me> wrote:
>>> Hi all,
>>>
>>> Sorry if this is a FAQ. I can't seem to find it in the docs or googling.
>>>
>>> I have the following scenario and just need to know which
>>> OpenJPA-specific annotations to apply to my field. The scenario is a
>>> field of type Map where the map key is an enum that I want to store as
>>> @Enumerated(EnumType.STRING) and is not an attribute of the values in
>>> the map using OpenJPA 1.2.x & JPA 1.0 (can't go to JPA 2.0 yet).
>>> Assume all classes use a simple @Id field of type Long.
>>>
>>> public enum Place { HOUSE, CAVE, LEAN_TO, BARE_SPOT; }
>>>
>>> @Entity
>>> public class WildThing {
>>> /* NOTE: does **not** have a field of type Place! */
>>> @Id
>>> protected Long id;
>>> protected String name;
>>> }
>>>
>>> @Entity
>>> public class WhereTheWildThingsAre {
>>>
>>> @Id
>>> protected Long id;
>>>
>>> // what annotation(s) do I put here?
>>> @OneToMany(cascade =
>>> { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST })
>>> protected Map<Place,WildThing> thingsByClassification
>>> = new HashMap<Place,WildThing>;
>>>
>>> }
>>>
>>> Again, I want the key to be stored as the string representation of the
>>> enum constant.
>>>
>>> TIA,
>>> Matthew
>>>
>>> --
>>> mailto:matthew@matthewadams.me
>>> skype:matthewadams12
>>> yahoo:matthewadams
>>> aol:matthewadams12
>>> google-talk:matthewadams12@gmail.com
>>> msn:matthew@matthewadams.me
>>> http://matthewadams.me
>>> http://www.linkedin.com/in/matthewadams
>>>
>>
>>
>>
>> --
>> Patrick Linskey
>> 202 669 5907
>>
>
>
>
> --
> mailto:matthew@matthewadams.me
> skype:matthewadams12
> yahoo:matthewadams
> aol:matthewadams12
> google-talk:matthewadams12@gmail.com
> msn:matthew@matthewadams.me
> http://matthewadams.me
> http://www.linkedin.com/in/matthewadams
>
--
Patrick Linskey
202 669 5907
Re: OpenJPA 1.2.x & map key as enum stored as string (probably a FAQ)
Posted by Matthew Adams <ma...@matthewadams.me>.
Yes, I've tried that & OpenJPA wants to use the enum's ordinal instead
of its string representation. How do I force the use of its string
representation?
Thanks,
Matthew
PS: Who's Max? ;)
On Wed, Apr 14, 2010 at 10:56 AM, Patrick Linskey <pl...@gmail.com> wrote:
> Hi, Max!
>
> I think that @PersistentMap is your friend here. Have you tried that annotation?
>
> Happy sailing,
>
> -Patrick
>
> On Wed, Apr 14, 2010 at 9:59 AM, Matthew Adams <ma...@matthewadams.me> wrote:
>> Hi all,
>>
>> Sorry if this is a FAQ. I can't seem to find it in the docs or googling.
>>
>> I have the following scenario and just need to know which
>> OpenJPA-specific annotations to apply to my field. The scenario is a
>> field of type Map where the map key is an enum that I want to store as
>> @Enumerated(EnumType.STRING) and is not an attribute of the values in
>> the map using OpenJPA 1.2.x & JPA 1.0 (can't go to JPA 2.0 yet).
>> Assume all classes use a simple @Id field of type Long.
>>
>> public enum Place { HOUSE, CAVE, LEAN_TO, BARE_SPOT; }
>>
>> @Entity
>> public class WildThing {
>> /* NOTE: does **not** have a field of type Place! */
>> @Id
>> protected Long id;
>> protected String name;
>> }
>>
>> @Entity
>> public class WhereTheWildThingsAre {
>>
>> @Id
>> protected Long id;
>>
>> // what annotation(s) do I put here?
>> @OneToMany(cascade =
>> { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST })
>> protected Map<Place,WildThing> thingsByClassification
>> = new HashMap<Place,WildThing>;
>>
>> }
>>
>> Again, I want the key to be stored as the string representation of the
>> enum constant.
>>
>> TIA,
>> Matthew
>>
>> --
>> mailto:matthew@matthewadams.me
>> skype:matthewadams12
>> yahoo:matthewadams
>> aol:matthewadams12
>> google-talk:matthewadams12@gmail.com
>> msn:matthew@matthewadams.me
>> http://matthewadams.me
>> http://www.linkedin.com/in/matthewadams
>>
>
>
>
> --
> Patrick Linskey
> 202 669 5907
>
--
mailto:matthew@matthewadams.me
skype:matthewadams12
yahoo:matthewadams
aol:matthewadams12
google-talk:matthewadams12@gmail.com
msn:matthew@matthewadams.me
http://matthewadams.me
http://www.linkedin.com/in/matthewadams
Re: OpenJPA 1.2.x & map key as enum stored as string (probably a FAQ)
Posted by Patrick Linskey <pl...@gmail.com>.
Hi, Max!
I think that @PersistentMap is your friend here. Have you tried that annotation?
Happy sailing,
-Patrick
On Wed, Apr 14, 2010 at 9:59 AM, Matthew Adams <ma...@matthewadams.me> wrote:
> Hi all,
>
> Sorry if this is a FAQ. I can't seem to find it in the docs or googling.
>
> I have the following scenario and just need to know which
> OpenJPA-specific annotations to apply to my field. The scenario is a
> field of type Map where the map key is an enum that I want to store as
> @Enumerated(EnumType.STRING) and is not an attribute of the values in
> the map using OpenJPA 1.2.x & JPA 1.0 (can't go to JPA 2.0 yet).
> Assume all classes use a simple @Id field of type Long.
>
> public enum Place { HOUSE, CAVE, LEAN_TO, BARE_SPOT; }
>
> @Entity
> public class WildThing {
> /* NOTE: does **not** have a field of type Place! */
> @Id
> protected Long id;
> protected String name;
> }
>
> @Entity
> public class WhereTheWildThingsAre {
>
> @Id
> protected Long id;
>
> // what annotation(s) do I put here?
> @OneToMany(cascade =
> { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST })
> protected Map<Place,WildThing> thingsByClassification
> = new HashMap<Place,WildThing>;
>
> }
>
> Again, I want the key to be stored as the string representation of the
> enum constant.
>
> TIA,
> Matthew
>
> --
> mailto:matthew@matthewadams.me
> skype:matthewadams12
> yahoo:matthewadams
> aol:matthewadams12
> google-talk:matthewadams12@gmail.com
> msn:matthew@matthewadams.me
> http://matthewadams.me
> http://www.linkedin.com/in/matthewadams
>
--
Patrick Linskey
202 669 5907