ManyAoMany关系中的JPA不兼容映射。基数与其backpointer的基数不一致

时间:2015-12-17 15:07:19

标签: java jpa many-to-many eclipselink

eclipse链接在应用初始化期间抛出以下错误 尽管在这里进行了彻底的搜索,但我找不到原因。

这是错误:

Caused by: Exception [EclipseLink-7244] 
(Eclipse Persistence Services - 2.6.1.qualifier): org.eclipse.persistence.exceptions.ValidationException

Exception Description: 
An incompatible mapping has been encountered between [class User] and [class UserAuthProvider]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.

at org.eclipse.persistence.exceptions.ValidationException.invalidMapping(ValidationException.java:1296)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.ManyToManyAccessor.process(ManyToManyAccessor.java:158)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processNonOwningRelationshipAccessors(MetadataProject.java:1628)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1917)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:577)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:604)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1943)
... 48 more

我的课程。

用户

@Entity
@Table(name = "users")
@XmlRootElement

            public class User implements Serializable {

                private static final long serialVersionUID = 1L;

                @Basic(optional = false)
                @NotNull
                @Size(min = 1, max = 255)
                @GeneratedValue(strategy = GenerationType.AUTO)
                @Column(name = "id")
                private Long id;

                @Id
                @Basic(optional = false)
                @NotNull
                @Size(min = 1, max = 255)
                @Column(name = "username")
                private String username;

                @Size(max = 512)
                @Column(name = "password")
                private char[] password;

                @Column(name = "enabled")
                private Boolean enabled;

                @ManyToOne
                @JoinColumn(name = "user_role")
                private Role role;

                @ManyToMany(mappedBy = "users")
                private Collection<UserAuthProvider> authProviders;

                @OneToOne(mappedBy = "user", cascade = CascadeType.PERSIST)
                @NotNull
                private UserInfo userInfo;

                @Column(name = "firstname")
                private String firstName;

                @Column(name = "lastname")
                private String lastName;

                @Column(name = "displayname")
                private String displayName;

                @Column(name = "gender")
                private Gender gender;

                @Column(name = "address", length = 512)
                private String address;

                @Column(name = "address_geo", length = 512)
                private String addressGeo;

                @Temporal(javax.persistence.TemporalType.DATE)
                @Column(name = "birthdate")
                private Date birthDate;

                @Temporal(javax.persistence.TemporalType.DATE)
                @Column(name = "date_created")
                private Date dateCreated;

                @Temporal(javax.persistence.TemporalType.DATE)
                @Column(name = "date_modified")
                private Date dateModified;

                public enum Gender {

                    MALE, FEMALE, UNKNOWN
                }

                public User() {
                    gender = Gender.UNKNOWN;
                    Locale greekLocale = new Locale("el");
                    dateCreated = Calendar.getInstance(greekLocale).getTime();
                    enabled = true;
                    authProviders = new HashSet<>();
                    userInfo = new UserInfo(this);
                }

                public User(String username) {
                    this();
                    this.username = username;
                }

                public User(String username, char[] password) {
                    this();
                    this.username = username;
                    this.password = password;
                }

                //getters and setters

                @Override
                public int hashCode() {
                    int hash = 0;
                    hash += (username != null ? username.hashCode() : 0);
                    return hash;
                }

                @Override
                public boolean equals(Object object) {
                    // TODO: Warning - this method won't work in the case the id fields are not set
                    if (!(object instanceof User)) {
                        return false;
                    }
                    User other = (User) object;
                    if ((this.username != null && other.username != null) && (this.username.equals(other.username))) {
                        return true;
                    }
                    return true;
                }
            }

UserAuthProvider

@Entity
@Table(name = "user_auth_provider")
@XmlRootElement

public class UserAuthProvider implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;

@Basic(optional = false)
@Column(name = "providername")
private String providerName;

@Basic(optional = true)
@ManyToMany
@JoinTable(
        name = "auth_providers_per_user",
        joinColumns = {
            @JoinColumn(name = "auth_provider_id", referencedColumnName = "id")},
        inverseJoinColumns = {
            @JoinColumn(name = "username", referencedColumnName = "username")})
private Collection<User> users;

@Temporal(javax.persistence.TemporalType.DATE)
@Column(name = "date_created")
private Date dateCreated;

@Temporal(javax.persistence.TemporalType.DATE)
@Column(name = "date_modified")
private Date dateModified;

@Column(name = "enabled")
private Boolean enabled;

public UserAuthProvider() {
    Locale greekLocale = new Locale("el");
    dateCreated = Calendar.getInstance(greekLocale).getTime();
    users = new HashSet<>();
    enabled = true;
}

@Override
public int hashCode() {
    int hash = 5;
    hash = 11 * hash + Objects.hashCode(this.id);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final UserAuthProvider other = (UserAuthProvider) obj;
    if (!Objects.equals(this.id, other.id)) {
        return false;
    }
    return true;
}

}

任何帮助都很有价值,谢谢

1 个答案:

答案 0 :(得分:2)

从所有关联映射中删除@Basic@Basic旨在与原始字段一起使用。来自http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes

  

基本属性是属性类是简单类型的属性,例如String,Number,Date或基元。基本属性的值可以直接映射到数据库中的列值。