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].?

Foofoosの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;
    }
}

Foo エンティティ:

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(それは であるため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 に答える