• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Witold Marshal
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
James Sutherland
Ranch Hand
Posts: 553
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have no problem defining keys as:


Maybe because Integer is an Object?

WP
 
Sudhir Ravindra
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Cesar Sportore
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the @Id annotation only works for variables of the Long type, isn't it?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic