Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 312244 - can't map optional one-to-one relationship using @PrimaryKeyJoinColumn
Summary: can't map optional one-to-one relationship using @PrimaryKeyJoinColumn
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P2 minor (vote)
Target Milestone: ---   Edit
Assignee: Guy Pelletier CLA
QA Contact:
URL: http://wiki.eclipse.org/Talk:EclipseL...
Whiteboard:
Keywords:
Depends on:
Blocks: 309681
  Show dependency tree
 
Reported: 2010-05-10 08:39 EDT by Adrian Goerler CLA
Modified: 2022-06-09 10:08 EDT (History)
4 users (show)

See Also:


Attachments
repro for this issue and workaround for issues in TestList (6.67 KB, patch)
2010-05-10 09:24 EDT, Adrian Goerler CLA
no flags Details | Diff
Proposed changes (8.99 KB, patch)
2011-01-06 11:17 EST, Guy Pelletier CLA
no flags Details | Diff
Updated patch after review from Gordon Yorke (12.06 KB, patch)
2011-01-06 14:21 EST, Guy Pelletier CLA
no flags Details | Diff
activate the test for this issue (1.03 KB, patch)
2011-01-10 05:44 EST, Adrian Goerler CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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