Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 312244

Summary: can't map optional one-to-one relationship using @PrimaryKeyJoinColumn
Product: z_Archived Reporter: Adrian Goerler <adrian.goerler>
Component: EclipselinkAssignee: Guy Pelletier <guy.pelletier>
Status: RESOLVED FIXED QA Contact:
Severity: minor    
Priority: P2 CC: eclipselink.orm-inbox, guy.pelletier, sabine.heider, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
URL: http://wiki.eclipse.org/Talk:EclipseLink/Bugs/309681#Optional_one-to-one_relationship_using_.40PrimaryKeyJoinColumn
Whiteboard:
Bug Depends on:    
Bug Blocks: 309681    
Attachments:
Description Flags
repro for this issue and workaround for issues in TestList
none
Proposed changes
none
Updated patch after review from Gordon Yorke
none
activate the test for this issue none

Description Adrian Goerler CLA 2010-05-10 08:39:38 EDT
I've got a one-to-one relationship between the entities Course and Material.

The relationship is annotated as follows:

@Entity
@Table(name = "TMP_COURSE")
public class Course {

...

    @PrimaryKeyJoinColumn
    @OneToOne(cascade = CascadeType.PERSIST, optional=true)
    public Material getMaterial() {
        return material;
    }

...

}

@Entity
@Table(name = "TMP_MATERIAL")
public class Material {

    ...

    @Id
    @Column(name = "COURSE_ID")
    public long getCourseId() {
        return m_id;
    }

    @OneToOne(mappedBy = "material")
    public Course getCourse() {
        return course;
    }


    @PrePersist
    public void prePersist() {
        m_id = course.getCourseId();
    }

    ... 

}

EclipseLink's forward mapper generates a FK constraint from TMP_COURSE to TMP_MATERIAL.COURSE_ID. The constraint is generated although the relationship is annotated as optional.

Inserting a Course without a Material fail with a FK constraint violation.
Comment 1 Adrian Goerler CLA 2010-05-10 09:24:18 EDT
Created attachment 167700 [details]
repro for this issue and workaround for issues in TestList

The issue can be reproduced by 

jpa/eclipselink.jpa.wdf.test
- org.eclipse.persistence.testing.tests.wdf.jpa1.relation.TestPrimaryKeyCoinColumn

As a workaround for this issue, in TestList a Material ia persisted along with a Course
Comment 2 Adrian Goerler CLA 2010-05-10 09:31:17 EDT
repro and workaround for TestList checked in at #7189

tested on MySQL
reviewed by Andreas
Comment 3 Tom Ware CLA 2010-05-28 14:43:03 EDT
Setting initial priority.
Comment 4 Guy Pelletier CLA 2011-01-06 11:17:22 EST
Created attachment 186184 [details]
Proposed changes
Comment 5 Guy Pelletier CLA 2011-01-06 14:21:27 EST
Created attachment 186214 [details]
Updated patch after review from Gordon Yorke
Comment 6 Guy Pelletier CLA 2011-01-06 15:00:22 EST
Changes submitted.

Reviewed by: Gordon Yorke

Tests: New test ( testOptionalPrimaryKeyJoinColumnRelationship) added to the DDLGenerationJUnitTestSuite to test. All tests pass (full regression test suite and extended jpa test suite)
Comment 7 Adrian Goerler CLA 2011-01-10 05:44:46 EST
Created attachment 186370 [details]
activate the test for this issue

Tests on MaxDB -> OK
Comment 8 Eclipse Webmaster CLA 2022-06-09 10:08:55 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink