| Author |
Enum and JPA
|
deepak prasee
Greenhorn
Joined: Mar 02, 2009
Posts: 7
|
|
Hi
I am using Enumerated.oridnal in the PK class to insert an integer field in the mysql database.
Now i read somewhr that enum sholud not used to build the composite primary key..
Can any one suggest me whtr i can use enum as one of the datatype to build composite primary key...
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Deepak,
An enum usually has a small set of values - not very useful in a primary key. Can you elaborate more on what you are trying to accomplish?
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
deepak prasee
Greenhorn
Joined: Mar 02, 2009
Posts: 7
|
|
Ok ..Lets see in details..
In my java code i am maintaining a Enum constants with 3 values...
enum NUMBER
{
ONE,TWO,THREE
}
i need to insert the ordinal values of the corresponding enum into the databse.The type of that field is integer.Its one of the field i used to construct the composite primary key
Thats means in a table there are 3 fields used as primary keys and this ordinal is one of the primary keys used to construct the composite primary key...
so while creating entity class for this one, i should mention the three primary in an separate PK class. In that i should declare the field as typr NUMBER with the Annotation @Enumnerated(Enum.Ordinal)..but we are mapping the integer value to that integer field in database..
Now i need to clarify that is it good pratice to use Annotation @Enumnerated(Enum.Ordinal) in the PK Class..
|
 |
Jean-Claude Rouvinez
Ranch Hand
Joined: Aug 26, 2003
Posts: 35
|
|
deepak prasee wrote:
...
@Enumnerated(Enum.Ordinal)..but we are mapping the integer value to that integer field in database..
...
According to Effective Java Second Edition item 31, using ordinal is not recommended. So I implemented my Enum like this:
And the is stored in the database.
My question is: How can I persist this Enum using the code?
Best Regards
Jean-Claude
|
Jean-Claude
|
 |
Jean-Claude Rouvinez
Ranch Hand
Joined: Aug 26, 2003
Posts: 35
|
|
|
 |
 |
|
|
subject: Enum and JPA
|
|
|