• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum and JPA

 
deepak prasee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
deepak prasee
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Rouvinez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jean-Claude Rouvinez wrote:
deepak prasee wrote:[color=green]
...
My question is: How can I persist this Enum using the code?
...


I used this solution Posted by Sahoo (Oct 17, 2007 4:39 AM) on Java Net and it works fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic