JPA: Entity Relationships Mapping Example

  • The direction of a relationship can be
    bidirectional–owning side and inverse side
    unidirectional–owning side only
  • Owning side specifies the physical mapping
    CascadeType(ALL, PERSIST, MERGE, REMOVE, REFRESH)
    FetchType(LAZY, EAGER)

bidirectional @one to many

@Entity
@Table(name = "course")
public class Course {
    /** Attributes for Course **/
    @Id
    @Column(name = "courseid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int courseId;
    
    @Basic
    @Column(name = "employeeid")
    private String employeeId;
    
    @Column(name = "coursename")
    private String courseName;
    
    @Temporal(TemporalType.DATE)
    @Column(name = "fromdate")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date fromDate;
    
    @NotNull
    @Column(name = "gstincluded", nullable = false, columnDefinition = "TINYINT", length = 1)
    private boolean gstIncluded;
    
    /** Container for CourseApplicationActions **/
    @OneToMany(mappedBy="course", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    /**Owning side(mappedBy) specifies the physical mapping (CascadeType & FetchType)**/
    private List courseEvent = new ArrayList();
    
    ...
}

CourseEvent

@Entity
public class CourseEvent {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "courseeventid")
    private int courseEventId;
    
    // Reverse Relation
    @ManyToOne
    @JoinColumn(name = "courseid")
    private Course course;
    
    ... 
}

Many to Many

@Entity
public class Author {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name = "full_name", nullable = false)
    private String fullName;

    @ManyToMany(mappedBy = "authors")
    private List books = new ArrayList<>();

}
@Entity
public class Book {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Column(name = "title", nullable = false)
    private String title;

    @ManyToMany
    @JoinTable(name = "Book_Author",
        joinColumns = {
            @JoinColumn(
                name = "book_id", 
                referencedColumnName = "id"
            )
        },
        inverseJoinColumns = {
            @JoinColumn(
                name = "author_id", 
                referencedColumnName = "id"
            )
        }
    )
    private List authors = new ArrayList<>();
}

user

@Entity
@Table(name = "user")
public class User {
    //persistence attribute and Transient
    @Id
    @Column(name = "userid")
    private String userId;
    
    @ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinTable(name = "userrole", 
    joinColumns = {@JoinColumn(name = "userid", referencedColumnName = "userid") }, 
    inverseJoinColumns = {@JoinColumn(name = "roleid", referencedColumnName = "roleid") })
    private List roleSet;
    
    @Transient
    private ArrayList roleIds = new ArrayList();
    
    //constructor
    public User() {
    }

    public User(String userId) {
        this.userId = userId;
    }
    
    //getter & setter
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    
    public List getRoleSet() {
        return roleSet;
    } 
    public void setRoleSet(ArrayList roleSet) {
        this.roleSet = roleSet;
    }

    public ArrayList getRoleIds() {
        ArrayList rList = (ArrayList) this.getRoleSet();
        ArrayList roleIds = new ArrayList();
        for (Role role : rList) {
            roleIds.add(role.getRoleId());
        }
        return roleIds;
    }
    public void setRoleIds(ArrayList roleIds) {
        this.roleIds = roleIds;
    }
}

你可能感兴趣的:(JPA: Entity Relationships Mapping Example)