Mapping shows how two entities are related to each other. In many to many mapping many entities of class A can only have multiple entities in class B. Like for example suppose the two classes are Student and Courses. So if these two classes have many to many relationship then it means that each Student can have multiple Courses .And each Course can have multiple students

We will explain this with the help of an example:

We make two class Student(We named the class Stu3 but we will reference it as Student to make it more easy to understand) and Course.

Listing 1: Course class

package model;
 
 import java.io.Serializable;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 
 /**
 *
 * @author Anurag
 */
 @Entity
 public class Course implements Serializable {
 
 @Id
 @GeneratedValue
 @Column(name = "C_ID")
 private Long id;
 private String name;
 private int duration;
 
 public Course() {
 }
 
 public Long getId() {
 return id;
 }
 
 public void setId(Long id) {
 this.id = id;
 }
 
 public Course(String n, int dur) {
 name = n;
 duration = dur;
 }
 
 public int getDuration() {
 return duration;
 }
 
 public void setDuration(int duration) {
 this.duration = duration;
 }
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 }
 

文章標籤

saravanagumar 發表在 痞客邦 留言(0) 人氣(8)