This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Invalid composite primary key specification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Invalid composite primary key specification" Watch "Invalid composite primary key specification" New topic
Author

Invalid composite primary key specification

Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
I'm trying to deploy a web module to Sun Java System Application Server Platform Edition 9.0_01 (JRE 1.5.0_12) and I get the following exception:

Exception Description: Invalid composite primary key specification. The names of the primary key fields or properites in the primary key class [edu.ldsbc.payroll.model.TimeEntryId] and those of the entity bean class [class edu.ldsbc.payroll.model.TimeEntry] must correspond and their types must be the same. Also, ensure that you have specified id elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class.

Here are the two classes:





When you break the big rules, you get a lot of little rules.
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
If you want a composite primary key, the annotations to use are @EmbeddedId
and @IdClass.
Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
In this case I'm using the @IdClass annotation which I understand only requires a @IdClass annotation on the entity class while the id class just needs to implement serializable and define a few methods that hibernate uses.

I tried deploying this app to tomcat and it thinks it just fine (actually I think tomcat is just being more forgiving). Is there a Best Practices standard that I'm violating here?
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Originally posted by Matthew Snow:
In this case I'm using the @IdClass annotation which I understand only requires a @IdClass annotation on the entity class while the id class just needs to implement serializable and define a few methods that hibernate uses.[/QUOTE
Your entity code looks correct enough. This looks like a bug in the persistence provider. I have no idea what you mean by "few methods that Hibernate uses" though. If you are using the Sun app server aren't you using the TopLink Essentials persistence provider?
I tried deploying this app to tomcat and it thinks it just fine (actually I think tomcat is just being more forgiving). Is there a Best Practices standard that I'm violating here?
Well, the annotations on the PK class are not going to be used and should not really be there. You can start by removing those.


-Mike
Pro JPA 2: Mastering the Java Persistence API
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
Just looked up that the specification allows to use an @IdClass with @Id on the entity properties . Still, it is a duplication of code (the property definitions), and, especially if you encounter problems with it, an @EmbeddedId within the entity class appears easier.
Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
I changed all my composite primary key definitions from @ClassId annotations to @EmbeddedId annotations and I am now able to deploy my web module.

I have now, however, encountered another error that's more vague when I try to Enable the web module after I've deployed it. It's a big ugly exception saying it could not create inner bean 'transactionInterceptor' due to error creating bean 'transactionManager' due to error creating bean 'entityManagerFactory'. This is all caused by the following exception:

Caused by: org.hibernate.MappingException: Could not determine type for: oracle.toplink.essentials.indirection.ValueHolderInterface, for columns: [org.hibernate.mapping.Column(_toplink_personalData_vh)]

Here is the code for entity PersonalData:

Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
It looks like you are deploying the entities using TopLink Essentials but then trying to use a Hibernate provider with them. That's not going to work because they do different things to the class at runtime. Hibernate generates an extra modified proxy class for it while TopLink weaves it directly.

BTW, you should not put the @Basic annotation on your relationship as it will typically get ignored in the best case and in the worst case be an error. Use the fetch attribute in the @ManyToOne/@OneToOne annotations.
Matthew Snow
Ranch Hand

Joined: May 02, 2007
Posts: 82
Yes, you are right. Sun App Server uses TopLink Essentials by default when verifying a WAR durring deploy, and according to documentation I can't change that. I can however disable verifiction when I deploy and I can define a separate persistence providor for the module to run under after it's deployed. Apparently this is done by copying the necesary jar files to the lib directory of the domain that it's installed to. However I'm still getting the same error. Since this is a server problem, I'm continuing this post under forum "Other Java Products and Servers" and topic "Sun App Server PE 9 and Hibernate".
Tom Tomm
Greenhorn

Joined: Mar 01, 2012
Posts: 5
After 5 years the bug is still here !!!

My id class:
Integer origID;
String informationAI;

and my normal class
@Transient
private static final long serialVersionUID = 1L;
//@EmbeddedId
//protected origID origID;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "origID")
private Integer origID;
@Id
//@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "informationAI")
private String informationAI;

and before you ask, yes I tried commenting out unnecessary lines/various lines.

The types ARE the same, yet the java says THEY ARE NOT

After 3 hours I am going crazy now.... Somone help please....


---------------
edit solved. I won't tell you how, because no one wanted to help me in first place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid composite primary key specification