cancel
Showing results for 
Search instead for 
Did you mean: 

In JDO, can a foreign key be part of a primary key?

Former Member
0 Kudos

Hi, it's possible to have a foreing key a part of a primary key in JDO?

The JDO checker is complaining about that. It says "Primary key field 'person' cannot declare relationship".

For example, if I have an entity 'Person' (id, name) and and entity 'Pet' (name, age). Can I have the primary key of 'Person' (id) as a foreign key in 'Pet', and can I have a compound primary key in 'Pet' with 'name' and 'id' as the compound key?

Thanks in advance.

Rafael

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

read the section named identity mapping in this <a href="http://www.jdocentral.com/JDO_Commentary_AbeWhite_1.html">article</a> for an answer to your question.

regards,

Dion

Former Member
0 Kudos

Hi Dino,

First of all, thank you very much for your answer.

I read the article, but the solution proposed isn’t clear to me. The theorical solution is: “You'll have to map the foreign key columns to individual simple fields in your class rather than a single related object. At runtime, you can use the values of these fields to construct an application identity instance, which you can then use to fetch the related object”

I don’t understand the second sentence… how would I fecht the related object at runtime?

Did you try it? Could you please send me an example? I change the type of my attributes to primitive types, and I left my .jdo and .map files as if those attributes were only primary keys… how would I specify the relation at runtime with the other table?

My email is gato_lopez76@yahoo.es, I would really appreciate your help.

Best regards,

Isidro

Former Member
0 Kudos

Hi Rafael,

I have just found your post. I'm trying to do something similar, so I needed that the primary key of my class would be also a foreign key to another table.

Did you finally solve the problem? Did you find a solution?

Thank you very much in advance for your answer!!

Regards,

Isidro