users@glassfish.java.net

Re: Enumeration mapping JPA

From: John_Africa_Brokers <john_at_africabrokers.com>
Date: Wed, 17 Oct 2007 14:10:52 +0200

---------------------------------------------------------------------------
HARBOR: http://coolharbor.100free.com/index.htm
Now Tomcat is also a cool application server
---------------------------------------------------------------------------
----- Original Message -----
From: <glassfish_at_javadesktop.org>
To: <users_at_glassfish.dev.java.net>
Sent: Wednesday, October 17, 2007 11:08 AM
Subject: Enumeration mapping JPA


> Hi,
>
> In our database we have an enumeration of strings. All these strings are
> actually numbers, for example ['-2', '-1' ,'1' ,'2']. At first I wanted to
> create an enumerator in Java for this but I couldn't find out how to do
> it, since enumerated values cannot start with a number. Then I thought
> that I could use an int in my entity bean. This seem to work pretty good
> but I could still set the attribute to a value which is not in the
> enumeration. Also, I cannot confirm that all JPA providers would convert
> these string values to int values in the same way.
>
> Could someone please help me? I would either like to use an enum or int if
> enum doesn't work. But I only want to use int if the conversion is within
> the JPA specification, '-2' becomes int with value -2 for example,
> otherwise I guess I'll have to use String.

Interesting and I'm not sure, but essentially enum is a class, and I think
what it amounts to it defining a custom enum class.
ie the persistance is really just a string moving back and forth and your
custom enum class interprets it.
Yes, not the kind of thing we do every day ;)
After a little searching I found this cool site
http://www.java2s.com/Code/Java/Language-Basics/Howtouseenum.htm

That should get you going with a custom enum class, then its just a little
JPA on top of that.
Good Luck ;)





> Thanks!
>
> Leo
> [Message sent by forum member 'woel' (woel)]
>
> http://forums.java.net/jive/thread.jspa?messageID=240608
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>