What does mappedBy
map to? Or, rather, what should it map to?
The headers
field below maps to @Entity
Foo
as per @OneToMany
docs? And then Foo
would be a wrapper for javax.mail.Header
?
package net.bounceme.dur.usenet.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.persistence.*;
@Entity
public class Articles implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(Articles.class.getName());
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String subject;
@OneToMany(mappedBy="foo") //a wrapper for Header is needed?
private List<Header> headers = new ArrayList<>();
public Articles() {
}
public Articles(Message message) {
try {
subject = message.getSubject();
} catch (MessagingException ex) {
Logger.getLogger(Articles.class.getName()).log(Level.SEVERE, null, ex);
}
}
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 Articles)) {
return false;
}
Articles other = (Articles) 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;
}
}
Yes wrapper for javax.mail.Header is needed, in general you cannot persist directly arbitrary classes, especially not the ones that are not Serializable. Also they cannot be elements of list that designs relationship between entities.
Value of mappedBy is name of the field that is owning side of bidirectional relationship. For a sake of example, let's assume that Article entity does have one-to-many relationship to Foo entity:
@OneToMany(mappedBy="article")
private List<Foo> headers;
Now we know that there must be other end of this relationship, and it is attribute, which is located to Foo entity, does have Article as a type and is named article:
@Entity
public class Foo {
...
@ManyToOne
Article article;
}
Owning side (in this case article in Foo) is used when bidirectional relationship is persisted to the database. In JPA 2.0 specification this is told with following words:
Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change. In the case of unidirectional one-to-one and one-to-many relationships, it is the developer’s responsibility to insure that the semantics of the relationships are adhered to.