Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 241308 - Primary key is incorrectly assigned to embeddable class field with the same name as the primary key field's name
Summary: Primary key is incorrectly assigned to embeddable class field with the same n...
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P3 major with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-17 15:06 EDT by Nathan Beyer (Cerner) CLA
Modified: 2022-06-09 10:31 EDT (History)
2 users (show)

See Also:


Attachments
Java Project Archive export; contains all necessary dependencies (6.13 MB, application/zip)
2008-07-17 15:09 EDT, Nathan Beyer (Cerner) CLA
peter.krogh: iplog+
Details
Proposed changes (10.13 KB, patch)
2008-08-20 11:00 EDT, Guy Pelletier CLA
no flags Details | Diff
Updated patch, namely copyrights. (10.60 KB, patch)
2008-08-20 11:48 EDT, Guy Pelletier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Beyer (Cerner) CLA 2008-07-17 15:06:30 EDT
Build ID:  I20080617-2000

Steps To Reproduce:
1. Import attached project archive (zip) into Eclipse 3.4 and a JavaSE-1.6 EE available.
2. Execute the 'pkbug.Main' class. This should result in a lot of output to the console and a final error about a constraint violation on a duplicated primary key.


More information:
Given the following classes and persistence.xml file --

-- ThreadInfo.java --
package pkbug;

import javax.persistence.Embeddable;

@Embeddable
public class ThreadInfo {

    private long id;
    private String name;

    public ThreadInfo() {}

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

-- MachineState.java --
package pkbug;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class MachineState {

    @Id
    private long id;

    @AttributeOverrides( { 
        @AttributeOverride(name = "id", column = @Column(name = "THREAD_ID")),
        @AttributeOverride(name = "name", column = @Column(name = "THREAD_NAME")) })
    private ThreadInfo thread;

    public MachineState() {}

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public ThreadInfo getThread() {
        return thread;
    }

    public void setThread(ThreadInfo thread) {
        this.thread = thread;
    }

}

-- persistence.xml --
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="pkbug" transaction-type="RESOURCE_LOCAL">
        <class>pkbug.MachineState</class>
        <class>pkbug.ThreadInfo</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

EclipseLink will dynamically generate the following DDL statement to create the table.

CREATE TABLE MACHINESTATE (ID BIGINT, THREAD_ID BIGINT NOT NULL, THREAD_NAME VARCHAR(255), PRIMARY KEY (THREAD_ID))

Note that 'PIMARY KEY' is on 'THREAD_ID' instead of 'ID', which is the field that has the @Id annotation.
Comment 1 Nathan Beyer (Cerner) CLA 2008-07-17 15:09:35 EDT
Created attachment 107767 [details]
Java Project Archive export; contains all necessary dependencies
Comment 2 Nathan Beyer (Cerner) CLA 2008-07-17 15:31:20 EDT
I discovered a work-around. If you declare the correct primary key field after the embedded entity's field, the primary key field is picked correctly.

The workaround version of MachineState.java from comment #1 would look something like this.

@Entity
public class MachineState {

    @AttributeOverrides( { 
        @AttributeOverride(name = "id", column = @Column(name = "THREAD_ID")),
        @AttributeOverride(name = "name", column = @Column(name = "THREAD_NAME"))})
    private ThreadInfo thread;

    @Id
    private long id;

...

}
Comment 3 Nathan Beyer (Cerner) CLA 2008-07-17 15:35:07 EDT
BTW - If someone can point the way to the bit of code this is might be localized to, I'll try to do my bit and come up with a fix.
Comment 4 Peter Krogh CLA 2008-07-25 15:24:26 EDT
Moving into 1.0.1 based on Bug triage
Comment 5 Guy Pelletier CLA 2008-08-20 11:00:39 EDT
Created attachment 110456 [details]
Proposed changes
Comment 6 Nathan Beyer (Cerner) CLA 2008-08-20 11:15:36 EDT
(In reply to comment #5)
> Created an attachment (id=110456) [details]
> Proposed changes
> 

Regarding the addition of the test sources in this patch. Can Oracle claim copyright (in the header of the file) over these files, which were mostly sourced from public postings to this bug?
Comment 7 Guy Pelletier CLA 2008-08-20 11:48:15 EDT
Created attachment 110460 [details]
Updated patch, namely copyrights.
Comment 8 Guy Pelletier CLA 2008-08-20 14:07:01 EDT
Changes have been submitted.

Reviewed by: Tom Ware

New tests: testBug241308 added to the DDLGenerationJUnitTestSuite along with the necessary new model classes.
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:31:03 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink