Set up a maven web project in Intellij with Spring and JPA - Part Three

Here we are for the third part of these tutorial series, we will now cover how to map create our JPA mappings.
This tutorial will be using a simple blog example from now on, using a simple mysql database.

Ok let's start by introducing our first entity "Post":
package blog.domain;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;


@Entity
public class Post {
    @Id
    @GeneratedValue
    @Column(name = "POST_ID")
    private long postId;

    @Column(name = "TITLE")
    private String title;

    @Column(name = "TEXT")
    private String text;

    @Column(name="POST_DATE")
    private Date postDate;

    @OneToMany(targetEntity = Comment.class, mappedBy = "post",cascade = CascadeType.REMOVE)
    private Collection<comment> comments;

    public void setPostId(long postId) {
        this.postId = postId;
    }

    public long getPostId() {
        return postId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Date getPostDate() {
        return postDate;
    }

    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    public Collection<comment> getComments() {
        return comments;
    }

    public void setComments(Collection<comment> comments) {
        this.comments = comments;
    }
}
If you are new to ORM (Object Relational Mapping) this might need some explanation:

  • @Entity: this annotation indicates that this class represents a table in the database;
  • @Id: indicates that this is our Primary Key (PK);
  • @GeneratedValue: let's us autoincrement the PK value;
  • @Column: Relational databases usually have different names for tables that are not really usable for java classes therefor we can utilize this annotation to map our field to the matching column in the database;
  • @OneToMany: indicates a one to many relationship between Post and Comment tables. The targetEntity attribute is pretty straight forward it indicates the targetted Entity of the relationship. mappedBy indicates the owning field of the relationship and is required unless the relationship is bidirectional. Cascade indicates what to do with the comment rows associated with a certain post, CascadeType is a enum that has the following values: ALL, MERGE, PERSIST, REFRESH, REMOVE.
Further things to notice:

  • the @Entity annotation can also take a name attribute to match the real table name;
  • getters will be used in our jsp's;
  • setters will be used in our backend to modify the entity object.


To keep it simple we will only be using 2 entities: post and comments. Therefore let's have a look at the mapping of the Comment entity.

package blog.domain;

import javax.persistence.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


@Entity
public class Comment {

    @Id
    @GeneratedValue
    @Column(name = "COMMENT_ID")
    private long commentId;

    @Column(name="COMMENTOR_NAME")
    private String commentorName;

    @Column(name="COMMENT_DATE")
    private Date commentDate;

    @Column(name="COMMENT_TEXT")
    private String commentText;

    @ManyToOne
    @JoinColumn(name = "POST_ID")
    private Post post;


    public long getCommentId() {
        return commentId;
    }

    public String getCommentorName() {
        return commentorName;
    }

    public void setCommentorName(String commentorName) {
        this.commentorName = commentorName;
    }

    public Date getCommentDate() {
        DateFormat dateFormat = new SimpleDateFormat(" yy/MM/dd HH:mm");
        dateFormat.format(commentDate);
        return commentDate;
    }

    public void setCommentDate(Date commentDate) {
        this.commentDate = commentDate;
    }

    public String getCommentText() {
        return commentText;
    }

    public void setCommentText(String commentText) {
        this.commentText = commentText;
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }
}


That's it! We have mapped our classes to database tables, tune in to the next tutorial where we will continue this tutorial series.

More about this tutorial series can be found here:
Set up a maven web project in Intellij with Spring and JPA - Part One (Project setup)
Set up a maven web project in Intellij with Spring and JPA - Part Two (Maven setup)
Set up a maven web project in Intellij with Spring and JPA - Part Four (Spring Configuration)
Set up a maven web project in Intellij with Spring and JPA - Part Five (DAO)
Set up a maven web project in Intellij with Spring and JPA - Part Six (Configuring Spring MVC)
Set up a maven web project in Intellij with Spring and JPA - Part Seven (Implementing Spring MVC)