[JAVA] JPA Basics 2

Entity class

What is Entity class?

The Entity class is a class that corresponds to a database table. You can think of it as a class that represents an "entity" in a relational data model.

Important) For the table managed by JPA, the corresponding Entity class must be created.

Creating Entity class

Class declaration

The Entity class must be created as a POJO (JavaBeans) class with the "@ javax.persistence.Entity" annotation. It also usually implements the java.io.Serializable interface.

Declaration of Entity class when class name and table name are the same


@Entity
public class ClassName implements Serializable {
    // ...
}

If the class name is exactly the same as the table name, it will be automatically mapped to the corresponding table, but if the table name and class name are different, use the "@ javax.persistence.Table" annotation to name You must explicitly specify the table name in the attribute.

Declaration of Entity class when class name and table name are different


@Entity
@Table(name="table_name")
public class ClassName implements Serializable {
    // ...
} 
Field declaration

In the Entity class, you need to declare the field corresponding to the table's primary key column, and you must specify "@ javax.persistence.Id" for that primary key field.

The possible data types in the declaration of the primary key field are as follows.

Mapping is optional for columns other than the primary key column. Similar to the class and table name rules, if the field and column names are exactly the same, they will be automatically mapped (default mapping rule).

As an example, the Entity class "Author" (corresponding to the Author table) that represents the author of the book is as follows.

Author.java


@Entity
public class Author implements Serializable {
    
    @Id
    private Integer id;

    private String name;
}

If the field name and column name are different, they will not be automatically mapped, so in order to map, you need to annotate the field with "@ javax.persistence.Column" and specify the table name in the name attribute. For example, in order to set the column name of the table to "author_name" and the field name of the Entity class to "name", it is necessary to describe as follows.

Author.java


@Entity
public class Author implements Serializable {
    
    @Id
    private Integer id;

    @Column(name="author_name")
    private String name;
}

Column annotation

Date / time type mapping
Enum mapping

Use the "@ javax.persistence.Enumerated" annotation to map enum fields.

Automatic primary key generation

Recommended Posts

JPA Basics 1
JPA Basics 2
Rails basics
Ruby basics
Ruby basics
Fragment basics
Docker basics
ViewPager basics
Java basics
Java basics
RSpec Basics
JavaScript basics
Hash basics
Java basics
Ruby basics
RecyclerView Basics
Rails CSV basics
Rails Routing Basics
Rails database basics
Rails Logger Basics
java programming basics
Basics of Ruby
JPA w / PostgreSQL
Object-oriented (Java) basics
Regular expression basics
Java concurrency basics
Rspec Basics [Rails]
Stream API basics