Thu*_*fir 0 java entity entity-relationship jpa jpql
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没有一个mappedBy值的foos,那是一个正确的读数吗?那是因为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; }
Run Code Online (Sandbox Code Playgroud)
为什么是关于方法而不是关于字段的关系的注释?这似乎让我感到困惑,因为我不确定哪些字段应该在我的代码的哪个实体中.
按照上面的尝试后,我按照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
Run Code Online (Sandbox Code Playgroud)
文章实体:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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 + " ]";
}
}
Run Code Online (Sandbox Code Playgroud)
mappedBy = "foos"意思是:去看看这个双向关联的另一面,看看它是如何映射的.另一方是foos目标实体中的属性.
所以,既然你有一个领域
@ManyToMany(mappedBy = "foos")
private Set<Foo> foos;
Run Code Online (Sandbox Code Playgroud)
你的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;
}
Run Code Online (Sandbox Code Playgroud)
如果您希望Foo成为协会的所有者,您必须拥有
public class Article {
@ManyToMany(mappedBy = "articles")
private Set<Foo> foos;
}
public class Foo {
@ManyToMany
@JoinTable(...)
private Set<Article> articles;
}
Run Code Online (Sandbox Code Playgroud)