A: 

JPA 2.0 improved considerably the support of derived identifiers with the Id and MapsId annotations (they should be preferred now) that you can use on XxxToOne associations. Below one of the example taken from the specification:

2.4.1.3 Examples of Derived Identities

...

Example 4:

The parent entity has a simple primary key:

@Entity
public class Person {
    @Id String ssn;
    ...
}

Case (a): The dependent entity has a single primary key attribute which is mapped by the relationship attribute. The primary key of MedicalHistory is of type String.

@Entity
public class MedicalHistory {
    // default join column name is overridden
    @Id
    @OneToOne
    @JoinColumn(name="FK")
    Person patient;
    ...
}

Sample query:

SELECT m
FROM MedicalHistory m
WHERE m.patient.ssn = '123-45-6789'

Case (b): The dependent entity has a single primary key attribute corresponding to the relationship attribute. The primary key attribute is of the same basic type as the primary key of the parent entity. The MapsId annotation applied to the relationship attribute indicates that the primary key is mapped by the relationship attribute.

@Entity
public class MedicalHistory {
    @Id String id; // overriding not allowed
    ...
    // default join column name is overridden
    @MapsId
    @JoinColumn(name="FK")
    @OneToOne Person patient;
    ...
}

Sample query:

SELECT m
FROM MedicalHistory m WHERE m.patient.ssn = '123-45-6789'

I think that the MapsId might be what you're looking for.

Follow-up: Instead of this:

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "prs_pty_id", nullable = false)
    @MapsId
    private Long prsPtyId;
    @Column(name = "name", length = 255)
    private String name;
    @Column(name = "cellphone", length = 55)
    private String cellphone;
    @Column(name = "officephone", length = 55)
    private String officephone;
    @JoinColumn(name = "prs_pty_id", referencedColumnName = "pty_id", nullable = false, insertable = false, updatable = false)
    @OneToOne(optional = false)
    private Party party;
    ...
}

Try this:

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "prs_pty_id", nullable = false)
    private Long prsPtyId;
    @Column(name = "name", length = 255)
    private String name;
    @Column(name = "cellphone", length = 55)
    private String cellphone;
    @Column(name = "officephone", length = 55)
    private String officephone;
    @MapsId
    @JoinColumn(name = "prs_pty_id", referencedColumnName = "pty_id")
    @OneToOne
    private Party party;
    ...
}

References

  • JPA 2.0 specification
    • Section 2.4.1 "Primary Keys Corresponding to Derived Identities"
    • Section 2.4.1.1 "Specification of Derived Identities"
    • Section 2.4.1.2 "Mapping of Derived Identities"
    • Section 2.4.1.3 "Examples of Derived Identities"
  • JPA Wikibook
Pascal Thivent
Sorry, it didn't work as expected, Eclipse-link still tries to create a person row with a null pk. Sample code: em.getTransaction().begin(); Party par = new Party(); Person per = new Person(); par.setComment("Nice"); per.setName("Per Vers"); par.setPerson(per); em.persist(par); em.getTransaction().commit();No luck with Hibernate either.
Jon Martin Solaas
@user275886 Please edit your question to show your entities and mappings. The mappings are the "problem", not the code used to persist so if you don't show them them, I can't say anything.
Pascal Thivent
I'll do that as soon as I can, until then, entitiets are basically just as netbeans generate them, and I don't use any special mappings.
Jon Martin Solaas
I've added bean source and DDL for the schema now, I don't have any particular mapping configuration, only a minimal persistence.xml declaring eclipselink as provider, connection data etc. Thanks for looking into this :-)
Jon Martin Solaas
Ok, getting closer I think, now I get "Exception Description: There should be one non-read-only mapping defined for the primary key field [webmarin.webmarin.person.prs_pty_id]." So it seems I need a mapping, but how and where and ... ?
Jon Martin Solaas
@user275886 Try to remove the `nullable = false, insertable = false, updatable = false` from the JoinColumn annotation.
Pascal Thivent
Didn't work, still pts_pty_id is null, while I want pty_id from party to be used automatically. No go with Hibernate either, so I must be doing something wrong here ...
Jon Martin Solaas
OK, finally got it working ... started over by generating entitybeans with netbeans, added the @GeneratedValue annotation for pk sequence, added the @MapsId to the Party attribute of Person, and removed the tree nullable, insertable and updatable annotations from the @JoinColumn. And then it worked. I'm not so sure what I've done now that I didn't do earlier on ... but there must have been something I've forgotten ... It also seems essensial that I do person.setParty(party); It is not enough to party.setPerson(person);
Jon Martin Solaas
@user275886 Yes, you need the @GeneratedValue on the parent if you want it to get generated by the DB. And yes, you have to set both sides of the relation between person and party to have things working properly. Glad it's solved.
Pascal Thivent
Yup, I guess it was setting both sides of the relationship that was the problem. @GeneratedValue has been working all the time. Thanks a bunch :-)
Jon Martin Solaas
btw, does anyone know why netbeans doesn't generate correct code in the first place?
Jon Martin Solaas