0

有什么问题Article.foo导致:Exception Description: The attribute [foos] in entity class [class net.bounceme.dur.usenet.model.Article] has a mappedBy value of [foos] which does not exist in its owning entity class [class net.bounceme.dur.usenet.model.Foo].

Foo没有 foos 的 mappedBy 值,这是正确的读数吗?那是因为Foo有一个 mappedBy 值为: @ManyToMany(mappedBy = "articles")

我怎样才能修复这些实体,使它们是多对多的?

我试图遵循文档

Example 1:

// In Customer class:

@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }

// In PhoneNumber class:

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

为什么注释是方法而不是字段的关系?这似乎让我很困惑,因为我不确定我的代码应该在哪个实体中包含哪些字段。

如上所述尝试后,我按照 IDE 的建议进行了一些更改,最终得到:

堆:

Jul 30, 2012 3:49:47 AM net.bounceme.dur.usenet.driver.Main main
SEVERE: null
Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@18ad9a0
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [USENETPU] failed.
Internal Exception: Exception [EclipseLink-7154] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [foos] in entity class [class net.bounceme.dur.usenet.model.Article] has a mappedBy value of [foos] which does not exist in its owning entity class [class net.bounceme.dur.usenet.model.Foo]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:115)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
    at net.bounceme.dur.usenet.driver.Main.<init>(Main.java:34)
    at net.bounceme.dur.usenet.driver.Main.main(Main.java:24)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [USENETPU] failed.
Internal Exception: Exception [EclipseLink-7154] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [foos] in entity class [class net.bounceme.dur.usenet.model.Article] has a mappedBy value of [foos] which does not exist in its owning entity class [class net.bounceme.dur.usenet.model.Foo]. If the owning entity class is a @MappedSuperclass, this is invalid, and your attribute should reference the correct subclass.
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1385)
    at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:105)
    ... 4 more

文章实体:

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.persistence.*;

@Entity
public class Article implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(Article.class.getName());
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String subject;
    @ManyToMany(mappedBy = "foos")
    private Set<Foo> foos;

    public Article() {
    }

    public Article(Message message) {
        try {
            subject = message.getSubject();
        } catch (MessagingException ex) {
            Logger.getLogger(Article.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

        public Set<Foo> getFoos() {
        return foos;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Article)) {
            return false;
        }
        Article other = (Article) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return subject;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
}

富实体:

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;

@Entity
public class Foo implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToMany(mappedBy = "articles")
    private Set<Article> articles;

    public Foo() {
    }

        public Set<Article> getArticles() {
        return articles;
    }

        public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 Foo)) {
            return false;
        }
        Foo other = (Foo) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "net.bounceme.dur.usenet.model.Foos[ id=" + id + " ]";
    }
}
4

1 回答 1

11

mappedBy = "foos"意思是:去看看这个双向关联的另一边,看看它是如何映射的。另一边是foos目标实体中的属性。

所以,既然你有一个领域

@ManyToMany(mappedBy = "foos")
private Set<Foo> foos;

您的 JPA 引擎在类中查找名为foos(因为 mappedBy = "foos") 的属性Foo(因为它是 a Set<Foo>)。它没有找到任何这样的属性,因此抱怨。

双向关联的一侧(反侧)必须具有该mappedBy属性。没有mappedBy属性的一方是所有者方。因此,如果您希望 Article 成为关联的所有者,您必须具有:

public class Article {
    @ManyToMany
    @JoinTable(...)
    private Set<Foo> foos;
}


public class Foo {
    @ManyToMany(mappedBy = "foos")
    private Set<Article> articles;
}

如果你想让 Foo 成为协会的所有者,你必须有

public class Article {
    @ManyToMany(mappedBy = "articles")
    private Set<Foo> foos;
}


public class Foo {
    @ManyToMany
    @JoinTable(...)
    private Set<Article> articles;
}
于 2012-07-30T11:31:44.090 回答