Eclipse error on mapping with @EmbeddedId

后端 未结 5 1422
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 09:26

I have an entity with composite key so I use the @Embeddable and @EmbeddedId annotations. Embeddable class looks like this :

@Embeddable
public class DitaAdm         


        
相关标签:
5条回答
  • 2020-12-29 09:26

    According to section 11.1.15 of the JPA 2.0 specification: Relationship mappings defined within an embedded id class are not supported. However, this might be supported by the JPA implementation you're using, even if it's not officially supported by the standard itself.

    If this is the case here, you might want to turn off validation for this in Eclipse under Window -> Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attributes -> Cannot resolve attribute name.

    0 讨论(0)
  • 2020-12-29 09:35

    In my case, the problem was not resolved until I set the following to Ignore:

    Project Facets > JPA > Errors/Warnings > Type > Mapped Java Class is a member class
    
    0 讨论(0)
  • 2020-12-29 09:35

    Preferences -> Java Persistence -> JPA -> Errors/Warnings -> Attribute -> Embedded ID classes should not contain relationship mappings: (Ignore)

    0 讨论(0)
  • 2020-12-29 09:50

    Before trying any of the previous solutions check first your persistence.xml and ensure that either exclude-unlisted-classes is set to true or that all your mapped classes are listed within your persistence-unit.

    0 讨论(0)
  • 2020-12-29 09:52

    Thought I would post the solution that I found which is compliant with the JPA 2.0 specification and appears to function the same way.

    First off, the JPA 2.0 specification can be found here: JSR-000317 Persistence Specification for Eval 2.0 Eval. The relevant section would be 2.4.1 "Primary Keys Corresponding to Derived Identities"

    Here is an example using the classes you specified:

    Embedded Id Class:

    @Embeddable
    public class DitaAdminAccountSkillPK implements Serializable {
    
        //No further annotations are needed for the properties in the embedded Id.
    
        //Needs to match the type of the id of your DitaAdmin object. I added 'Id' to the end of the property name to be more explicit.
        //Making the assumption here that DitaAdmin has a simple Integer primary key.
        private Integer adminId;
    
        //Needs to match the type of the id of your DitaAccount object. I added 'Id' to the end of the property name to be more explicit.
        //Making the assumption here that DitaAccount has a simple Integer primary key.
        private Integer accountId;
    
    
        //I'm adding a third property to the primary key as an example
        private String accountName;
    
        //constructor, getters, setters...
    
        //hashCode() and equals() overrides
    }
    

    "Dependent" Entity Class:

    @Entity
    public class DitaAdminAccountSkill {
    
        @EmbeddedId
        //Any overrides to simple Id properties should be handled with an attribute override
        @AttributeOverride(name = "accountName", column = @Column(name = "account_name"))
        private DitaAdminAccountSkillPK id;
    
        //MapsId refers to the name of the property in the embedded Id
        @MapsId("adminId")
        @JoinColumn(name="admin_id")
        @ManyToOne
        private DitaAdmin admin;
    
        @MapsId("accountId")
        @JoinColumn(name="account_id")
        @ManyToOne
        private DitaAccount account;
    
        //constructor, getters, setters...
    }
    

    "Parent" Entity class:

    public class DitaAdmin {
    
        @Id
        private Integer id;
    
        //...
    
        //Now your mappedBy attribute can refer to the admin object defined on DitaAdminAccountSkill which is also part of the Primary Key
        @OneToMany(fetch = FetchType.LAZY, mappedBy="admin")
        private List<DitaAdminAccountSkill> accountSkills;
    
        //...
    }
    
    0 讨论(0)
提交回复
热议问题