• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA - Problems with primary key field (@Id) that is a type of Integer

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created an entity class with int @Id field (property exactly so I put annotation before "get" methods). It worked fine. Then, I thought that for my project it would be better to have an Integer Primry Key property instead of int and I tried to change it. And now I see that this causes a problem.
Changing field type goes well. But changing returning type in "get" method (into Integer) or changing internal parameter (explicit) in "set" method causes that my Eclipse IDE can't recognize primary key field. I get warning mark at @Entity annotation with tip "The entity has no primary key attribute defined"
Code looks like this:



If I remove Integer types from methods the Eclipse warning disappears.



But during deployment server throws Exceptions for that JPA entity class.

What is wrong with taht Integer type?
I can't find in documentation statement that Integer can't be used for primary key field.

 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer is valid. Seems to be an Eclipse issue. Try cleaning and rebuilding your project. Also try the latest version of Eclipse.

It should work at runtime, so you could just ignore the IDE warning.
 
Witold Marshal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think that it must be sth wrong with Eclipse.
I have a quite new version of Indigo release.
I will look for the latest.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have no problem defining keys as:


Maybe because Integer is an Object?

WP
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont see a problem with the way you entity primary key is structured. The only constraint is that the primary key should be serializable and Integer definitely is.
This should work. Eclipse could be an ide local issue. Please provide what errors you are facing during deployment.

Also what JPA implementation you are using?
 
Witold Marshal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get any errors during deployment (in the first case of code that I submitted) because I receive warning in Eclipse window while the code is written. It is the basic Eclipse check of the code - small icon of yellow bulb with red circle and white cross. I am usually using demployment on JBoss server merged to the Eclipse (the JPA provider is a native provider from JBoss 6.1). This warning doesn't allow me to deploy the appliaction on the server through Eclipse.
Right now I have done some check. I exported this whole project to EAR file and deployed it manually on the server and it works. I should have done it earlier : ) but changing PK type from Integer into int seemed for me faster solution than looking for a cause of the problem.
So the problem is Eclipse.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the @Id annotation only works for variables of the Long type, isn't it?
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cesar Sportore wrote:But the @Id annotation only works for variables of the Long type, isn't it?



No, from the java doc for Id

Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.

 
Yes, my master! Here is the tiny ad you asked for:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic