aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA - Problems with primary key field (@Id) that is a type of Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA - Problems with primary key field (@Id) that is a type of Integer" Watch "JPA - Problems with primary key field (@Id) that is a type of Integer" New topic
Author

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

Witold Marshal
Ranch Hand

Joined: Feb 05, 2012
Posts: 48
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

Joined: Oct 01, 2007
Posts: 553
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.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Witold Marshal
Ranch Hand

Joined: Feb 05, 2012
Posts: 48
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

Joined: Mar 28, 2012
Posts: 859

We have no problem defining keys as:


Maybe because Integer is an Object?

WP
Sudhir Ravindra
Greenhorn

Joined: May 31, 2010
Posts: 16
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

Joined: Feb 05, 2012
Posts: 48
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

Joined: Sep 18, 2012
Posts: 6
But the @Id annotation only works for variables of the Long type, isn't it?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1680
    
    7

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.


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA - Problems with primary key field (@Id) that is a type of Integer