This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Enum and JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Enum and JPA" Watch "Enum and JPA" New topic
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: 30130
    
150

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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum and JPA
 
Similar Threads
Update with Composite keys
Enum used for a primary key pair !
Storing enums in database
Mapping same POJO to two tables in Hibernate XML mapping files
Composite Primary Key in CMP Bean??