2012-07-30 76 views
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].实体类中的属性具有一个mappedBy值,该值存在于其拥有的实体中

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

如何修复这些实体以使它们成为多对多?

我试图按照docs

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: [email protected] 
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 + " ]"; 
    } 
} 

回答

9

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

所以,既然你有一个字段

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

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

双向关联的唯一一面(反面)必须具有mappedBy属性。没有mappedBy属性的一侧是所有者一方。所以,如果你想第二十成为该协会的所有者,你必须拥有:

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


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

如果你想美孚成为该协会的老板,你必须有

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


public class Foo { 
    @ManyToMany 
    @JoinTable(...) 
    private Set<Article> articles; 
} 
相关问题