Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 328114 - @AttributeOverride does not work with nested embeddables having attributes of the same name
Summary: @AttributeOverride does not work with nested embeddables having attributes of...
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-19 06:58 EDT by Sabine Heider CLA
Modified: 2022-06-09 10:32 EDT (History)
6 users (show)

See Also:


Attachments
Proposed changes (25.88 KB, patch)
2010-10-27 10:13 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 Sabine Heider CLA 2010-10-19 06:58:31 EDT
There seems to be a problem with @AttributeOverride when different nested embeddables inside an entity have an attribute of the same name. See the following example:

@Entity
@Table(name="MY_ENTITY")
public class MyEntity {

    @Id
    private int id;

    @Embedded
    @AttributeOverrides ({
        @AttributeOverride(name="quantity.value", column=@Column(name="QTYVALUE")),
        @AttributeOverride(name="quantity.unitCode", column= @Column(name="QTYUNIT")),
        @AttributeOverride(name="amount.value", column=@Column(name="AMOUNTVALUE")),
       @AttributeOverride(name="amount.currencyCode", column= @Column(name="AMOUNTCURR"))
    })	
    private Price price;
}


@Embeddable
public class Price {
    @Embedded
    Quantity quantity;

    @Embedded
    Amount amount;

}


@Embeddable
public class Quantity {
    int value;
    String unitCode;
}


@Embeddable
public class Amount {
    private int value;
    private String currencyCode;
}

Note that both Quantity and Amount have an attribute called "value". 

When I try to persist an instance of MyEntity, I get the following exception:

Internal Exception: com.sap.dbtech.jdbc.exceptions.jdbc40.SQLSyntaxErrorException: [-4005] (at 53): Unknown column name:VALUE
Error Code: -4005
Call: INSERT INTO MY_ENTITY (ID, AMOUNTCURR, AMOUNTVALUE, VALUE, QTYUNIT) VALUES (?, ?, ?, ?, ?)
	bind => [1, EUR, 3, 2, kg]

When I rename one of the "value" fields, everything works fine.
Comment 1 Guy Pelletier CLA 2010-10-25 15:22:26 EDT
You can also get around this by using the following model instead:

@Entity
@Table(name="MY_ENTITY")
public class MyEntity {

    @Id
    private int id;

    @Embedded
    private Price price;
}

@Embeddable
public class Price {
    @Embedded
    @AttributeOverrides ({
        @AttributeOverride(name="value", column=@Column(name="QTYVALUE")),
        @AttributeOverride(name="unitCode", column=@Column(name="QTYUNIT")),
    })    
    Quantity quantity;

    @Embedded
    @AttributeOverrides ({
        @AttributeOverride(name="value", column=@Column(name="AMOUNTVALUE")),
        @AttributeOverride(name="currencyCode",
column=@Column(name="AMOUNTCURR"))
    })    
    Amount amount;
}

Since both quantity and amount map to a similar column by default (VALUE),
internally during initialization only one attribute override is captured (the
last one processed) hence the error.
Comment 2 Guy Pelletier CLA 2010-10-27 10:13:00 EDT
Created attachment 181839 [details]
Proposed changes
Comment 3 Guy Pelletier CLA 2010-10-27 13:31:41 EDT
Changes have been submitted.

Reviewed by: Gordon Yorke

New test (testAttributeOverrideToMultipleSameDefaultColumnName) added to AdvancedJPAJunitTest
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:32:46 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink