BE Study 8th

 Let's take a look at JPA through looking into Post.java


@Entity

@Table(name = "posts")

@Entity is an annotation that enables the following class to be mapped with a table of DB. @Table is an annotation that designates which table the entity will be mapped to. It has parameters like 

name: designates the name of the table to which entity will be mapped, uses entity's name if not given.

catalog: map catalog

schema: map schema

uniqueContraints: generate unique constraints if use DDL


public class Post {
defines the class

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_id_generator")
    @SequenceGenerator(name = "post_id_generator", sequenceName = "post_id_seq", allocationSize = 5)
@Id means that the following attribute will be the primary key of the table. @GeneratedValue is the value that uses other strategies to define primary key like

strategy = GenerationType.IDENTITY: this uses 'AUTO_INCREMENT' of mySQL and uses it as PK.
@GeneratedValue(strategy = GenerationType. SEQUENCE, generator = ' '): uses SEQUENCE of Oracle using generator. It Requires @SequenceGenerator to be used.
@GeneratedValue(strategy = GenerationType.TABLE, generator = ' '): uses TABLE using generator. It requires @TableGenerator to be used.

@SequenceGenerator is used to create a generator for @GeneratedValue. It has parameters like
 name: name of the sequence generator
sequenceName: sequence name of the DB
initialValue: used when generating DDL. Defines the first value when generating DDL.
allocation Size: incremented size per a sequence call.
catalog: DB's catalog
schema: DB's schema

    @Column(nullable = false)
    @NotEmpty
    private String title;
@Column means the attribute becomes a column of the table. It has parameters like

name: designates the name of the column to which attribute will be mapped
insertable: saves field when you save the entity
updatable: modifies fiedl when you modify the entity
tabel: designates the name of the table to which attribute will be mapped. Used when you map into more than two tables.
nullable: able to nullify the table
unique: grants unique Constraints to the attribute
columnDefinition: Grants column info to the DB
length, precision, scale: Regulates the field like string length, decimal point...

@NotEmpty means that the attribute cannot be NULL or ""

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cat_id")
    private Category category;
@ManyToOne is an annotation used to state relationship between different tables. In this case, ManyToOne means that many different posts can have same category. fetch = FetchType.LAZY means that it's fetchtype is lazy loading. Let's look into it later.

@JoinColumn means that this used as foreign key for JOIN

    @PrePersist
    public void onCreate() {
        createdAt = LocalDateTime.now();
    }
@PrePersist is an annotation that is called before new entity is created. Thus, onCreate() is called every time when new entity of the 'Post' is created. To be certain, it is called just before when new entity is saved in DB.

    @PreUpdate
    public void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

@PreUpdate is an annotation that is called before any entity is updated. Thus, onUpdate is called every time just before when update of any entity is completed in DB.

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cat_id")
    private Category category;
The above code is very important and critical. So let's take a closer look at it. It means that it category and "cat_id" has a N:1 relationship and thus makes a table that has category as a primary key and "cat_id" as a foreign key. OneToMany annotations isn't used usually because it can cause some problems. Specifically, it sends an extra 'update query' due to dirty checking. Also, the entity can send to table that does not corresponds to itself.

댓글

이 블로그의 인기 게시물

Interface of Java

Data Analytics Overview(OLTP vs OLAP)

Leetcode