Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Enum and JPA

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author & internet detective
Posts: 41262
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic