Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 331386 - NPE when mapping chain of 2 multi-column relationships using JPA 2.0 and @EmbeddedId composite PK-FK
Summary: NPE when mapping chain of 2 multi-column relationships using JPA 2.0 and @Emb...
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P2 critical with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Guy Pelletier CLA
QA Contact:
URL: http://stackoverflow.com/questions/42...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-29 20:09 EST by Karsten Wutzke CLA
Modified: 2022-06-09 10:15 EDT (History)
6 users (show)

See Also:


Attachments
SSCCE: non-working example using transitive, nested composite primary key classes (1.47 MB, application/octet-stream)
2010-11-30 13:49 EST, Karsten Wutzke CLA
guy.pelletier: iplog+
Details
Proposed changes (13.48 KB, patch)
2010-12-23 10:16 EST, 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 Karsten Wutzke CLA 2010-11-29 20:09:39 EST
I have three tables:

    CREATE TABLE PostAddresses
    (
      contact_id INTEGER NOT NULL,
      ordinal_nbr SMALLINT NOT NULL,
      PRIMARY KEY (contact_id, ordinal_nbr)
    );

    CREATE TABLE Foos
    (
      contact_id INTEGER NOT NULL,
      ordinal_nbr SMALLINT NOT NULL,
      PRIMARY KEY (contact_id, ordinal_nbr),
      FOREIGN KEY (contact_id, ordinal_nbr) REFERENCES PostAddresses (contact_id, ordinal_nbr)
    );

    CREATE TABLE Bars
    (
      contact_id INTEGER NOT NULL,
      ordinal_nbr SMALLINT NOT NULL,
      numba INTEGER NOT NULL,
      PRIMARY KEY (contact_id, ordinal_nbr, numba),
      FOREIGN KEY (contact_id, ordinal_nbr) REFERENCES Foos (contact_id, ordinal_nbr)
    );

Simple logic: Bars -> Foos -> PostAddresses all by (contact_id, ordinal_nbr), whatever it means.

Here the six entity and respective composite key classes.

    @Entity
    @Table(name = "PostAddresses")
    public class PostAddress implements Serializable
    {
        @EmbeddedId
        private PostAddressId embeddedId;
        
        ...
    }

    @Embeddable
    public class PostAddressId implements Serializable
    {
        @Column(name = "contact_id")
        private Integer contactId;

        @Column(name = "ordinal_nbr")
        private Integer ordinalNbr = 1;

        ...
    }

    @Entity
    @Table(name = "Foos")
    public class Foo implements Serializable
    {
        @EmbeddedId
        private FooId embeddedId;

        @MapsId(value = "postAddressId")
        @OneToOne
        @JoinColumns(value = {@JoinColumn(name = "contact_id", referencedColumnName = "contact_id"), @JoinColumn(name = "ordinal_nbr", referencedColumnName = "ordinal_nbr")})
        private PostAddress postAddress = null;
        
        ...
    }

    @Embeddable
    public class FooId implements Serializable
    {
        @Embedded
        private PostAddressId postAddressId; //just one field!

        ...
    }

    @Entity
    @Table(name = "Bars")
    public class Bar implements Serializable
    {
        @EmbeddedId
        private BarId embeddedId;

        ...
    }

    @Embeddable
    public class BarId implements Serializable
    {
        @Embedded
        private FooId fooId;

        @Column(name = "numba")
        private Integer numba;

        ...
    }

It's really nothing special, Bar references Foo, Foo references PostAddress, all via composite key class. Since the foreign keys are composite and part of the PK, I must nest the ID classes into the ID classes. I thought this is correct. However, I get the following stack trace with EclipseLink 2.2.0:

    Exception in thread "main" Local Exception Stack: 
    Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
    Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1f7182c1
    Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
    Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
    Internal Exception: java.lang.NullPointerException
        at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
        at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:136)
        at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:65)
        at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
        at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
        at tld.transmuc.Main.main(Main.java:27)
    Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
    Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
    Internal Exception: java.lang.NullPointerException
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1021)
        at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:95)
        at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:127)
        ... 4 more
    Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.2.0.v20101118-r8514): org.eclipse.persistence.exceptions.EntityManagerSetupException
    Exception Description: Predeployment of PersistenceUnit [transmuc] failed.
    Internal Exception: java.lang.NullPointerException
        at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:210)
        ... 7 more
    Caused by: java.lang.NullPointerException
        at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.EmbeddedIdAccessor.process(EmbeddedIdAccessor.java:189)
        at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processMappingAccessors(MetadataDescriptor.java:1417)
        at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processMappingAccessors(ClassAccessor.java:1405)
        at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(EntityAccessor.java:1061)
        at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:601)
        at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1464)
        at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:483)
        at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:453)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:975)
        ... 6 more

EclipseLink gives me absolutely no hint as to which class is the offending one. I found out using Hibernate telling me about a problem with the @MapsId annotation and the Foo class:

    org.hibernate.AssertionFailure: Unexpected nested component on the referenced entity when mapping a @MapsId: tld.transmuc.model.Foo

It appears to be an EclipseLink bug. Even if I omit the @Embedded annotation from the ID classes, the exception is still there.
Comment 1 Karsten Wutzke CLA 2010-11-30 10:39:02 EST
I'd really like to know what the JPA defined in that situation...
Comment 2 Karsten Wutzke CLA 2010-11-30 13:49:28 EST
Created attachment 184168 [details]
SSCCE: non-working example using transitive, nested composite primary key classes
Comment 3 Tom Ware CLA 2010-12-03 09:04:52 EST
If you haven't done so, it is likely a good idea to go through the mailing
lists or forums prior to posting these kinds of bugs.  The reason: There is a
good chance someone will be able to provide you with a reasonable workaround.
Comment 4 Michael OBrien CLA 2010-12-03 09:27:59 EST
>Verify not related to or since Jan 2010 changes in bug# 300051
- this change only moved up the validation check on multiple embeddedId's
Comment 5 Gordon Yorke CLA 2010-12-06 09:20:21 EST
At the very least this null pointer exception is a bug.
Comment 6 Guy Pelletier CLA 2010-12-23 10:16:21 EST
Created attachment 185775 [details]
Proposed changes

Patch corrects the NPE.
Comment 7 Guy Pelletier CLA 2010-12-23 11:41:03 EST
Changes have been submitted.

Reviewed by: Tom Ware

Tests: No, metadata processing exception. With this change, that exception goes away and all tests (FullRegressionTestSuite and extended JPA) pass successfully.
Comment 8 Eclipse Webmaster CLA 2022-06-09 10:09:47 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:15:29 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink