This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 173335

Summary: derived/dependent identities not mapped correctly
Product: javaee Reporter: err <err>
Component: PersistenceAssignee: Sergey Petrov <sj-nb>
Status: NEW ---    
Severity: blocker    
Priority: P2    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:
Attachments: ERD showing relationship at issue

Description err 2009-09-30 05:36:24 UTC
In the spec:
    2.4.1 Primary Keys Corresponding to Derived Identities
        The identity of an entity may be derived from the identity of another entity (the "parent" entity) when
        the former entity (the "dependent" entity) is the owner of a many-to-one or one-to-one relationship to
        the parent entity and a foreign key maps the relationship from dependent to parent.

For example, in the attached ERD, consider the aspectDisplay table. "Enitty Classes from Database" generates something like:

    @Entity
    @Table(name = "ASPECTDISPLAY", catalog = "", schema = "ERNIE")
    public class Aspectdisplay implements Serializable {
        private static final long serialVersionUID = 1L;
        @EmbeddedId
        protected AspectdisplayPK aspectdisplayPK;
        @Basic(optional = false)
        @Lob
        @Column(name = "DAT", nullable = false)
        private Serializable dat;
        @JoinColumns({@JoinColumn(name = "CHART_ID", referencedColumnName =
                "CHART_ID", nullable = false, insertable = false, updatable = false), @JoinColumn(name =
                "EVENT_ID", referencedColumnName = "EVENT_ID", nullable = false, insertable =
                false, updatable = false)})
        @OneToOne(optional = false)
        private ChartEvent chartEvent;

wheras instead of using @JoinColumns, it should be done with @MappedById.

    @Entity
    @Table(name = "ASPECTDISPLAY", catalog = "", schema = "ERNIE")
    public class Aspectdisplay implements Serializable {
        private static final long serialVersionUID = 1L;
        @EmbeddedId
        protected ChartEventPK chartEventPK;
        @Basic(optional = false)
        @Lob
        @Column(name = "DAT", nullable = false)
        private Serializable dat;
        @MappedById // <<<<<<<<<<<<<<<<<<<<<<< instead of @JoinColumns
        @OneToOne(optional = false)
        private ChartEvent chartEvent;

Mapping this way allows the persistence provider to automatically set the primary key (chartEvent must be set).

(Note in the JPA 1.0 release this should be mapped with @PrimaryKeyJoinColumns, @MappedById is now the preferred mechanism)

This is loosely related to Issue 172075, but there is no dependency.
Comment 1 err 2009-09-30 05:37:11 UTC
Created attachment 88561 [details]
ERD showing relationship at issue
Comment 2 Sergey Petrov 2009-09-30 07:31:49 UTC
looks like enhancement of feature for better support new features from jpa 2.0, it's closely correlate with initial plan
for 6.8, but it seems it will not be implemented in 6.8.
Comment 3 err 2009-09-30 15:30:27 UTC
> > Note in the JPA 1.0 release this should be mapped with @PrimaryKeyJoinColumns, 
> > @MappedById is now the preferred mechanism

> enhancement of feature for better support new features from jpa 2.0

Looks like this was an issue with the JPA 1.0 support as well; since there is a mapping specifically for "primary key
column that is used as a foreign key to join to another table". 

However, I understand that there probably is not time to get it into 6.8; I'm just really glad I found it, it was
driving me nuts not being able to specify this in the mappings.
Comment 4 Sergey Petrov 2009-10-01 20:00:43 UTC
also if we are talking about derived identifiers, it seems this area isn't final yet in jpa 2.0 specification.