JPA (Hibernate, EclipseLink) mapping: why doesn't this code work (chain of 2 relationships using JPA 2.0, @EmbeddedId composite PK-FK)?

前端 未结 1 1272
攒了一身酷
攒了一身酷 2021-01-19 01:04

I have three tables:

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


        
相关标签:
1条回答
  • 2021-01-19 01:45

    Simply removing the @Embedded annotation from within the EmbeddedId types that reference the dependant EmbeddedIds may resolve your issue.:

        @Embeddable
        public class FooId implements Serializable
        {
            // no annotation.
            private PostAddressId postAddressId; //just one field!
    
            ...
        }
    
        ...
            @MapsId(value = "postAddressId")
            @OneToOne
            @JoinColumns(value = {@JoinColumn(name = "contact_id", referencedColumnName = "contact_id"), @JoinColumn(name = "ordinal_nbr", referencedColumnName = "ordinal_nbr")})
            private PostAddress postAddress = null;
     

    (EDIT): Another issue is the use of the FooId. This class should not be used. The ID class for Foo Enitity is the PostAddressId and the OneToOne "postAddress" should simply be marked @Id and not @MapsId. Likewise BarId should not have an Embedded defined but should directly reference the PostAddressId. The Bar Mapping that references PostAddress should use MapsId.

    Please file a bug against EclipseLink for the terrible exception and if removing the @Embedded and the other updates in the edit does not resolve your issue please file an EclipseLink bug for that as well.

    0 讨论(0)
提交回复
热议问题