Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OneToMany mappedBy maps to _____

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;     } } 
like image 655
Thufir Avatar asked Jul 30 '12 09:07

Thufir


People also ask

What is mappedBy used for?

The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.

What is the use of mappedBy in JPA?

The purpose of the MappedBy parameter is to instruct JPA: Do NOT create another join table as the relationship is already being mapped by the opposite entity of this relationship.

What does mappedBy mean in Hibernate?

mappedBy tells Hibernate how to create instances of your entities and load the data into them. It should refer to the field name in the class that you are annotating, PersonDetail in this instance, where the relationship is defined.

What is @ONE-to-many mapping in JPA?

The One-To-Many mapping comes into the category of collection-valued association where an entity is associated with a collection of other entities. Hence, in this type of association the instance of one entity can be mapped with any number of instances of another entity.


1 Answers

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.

like image 121
Mikko Maunu Avatar answered Oct 05 '22 01:10

Mikko Maunu