Development

JPA의 주석 개요

sonpro 2023. 2. 26. 15:11
반응형

Annotation

Overview of Annotation in JPA

Java Persistence API (JPA) is an open-source library used to manage the persistence of data in Java applications. It provides a powerful and flexible way to store and retrieve data from a database. Annotation is one of the key features of JPA that allows developers to define the mapping between Java classes and database tables. In this blog post, we will discuss the different types of annotations used in JPA and how they are used to create a database schema. We will also look at some examples of how to use annotations in JPA.

What is Annotation?

Annotation is a form of metadata that can be used to provide additional information about a class, method, or field. It is used to define the mapping between Java classes and database tables. Annotations can be used to define the table name, column names, primary key, and foreign key relationships. Annotations can also be used to define the data types of the columns and the relationships between the tables.

Types of Annotations in JPA

JPA supports a wide range of annotations that can be used to define the mapping between Java classes and database tables. The following are some of the most commonly used annotations in JPA:

@Entity

The @Entity annotation is used to mark a Java class as an entity. This annotation is used to define the table name, primary key, and other properties of the entity.

@Table

The @Table annotation is used to define the table name for the entity. This annotation can also be used to define the schema name and the catalog name for the table.

@Column

The @Column annotation is used to define the column name and the data type of the column. This annotation can also be used to define the length, precision, and scale of the column.

@Id

The @Id annotation is used to mark a field as the primary key of the entity. This annotation is used to define the primary key column name and the data type of the primary key.

@ManyToOne

The @ManyToOne annotation is used to define a many-to-one relationship between two entities. This annotation is used to define the foreign key column name and the data type of the foreign key.

@OneToMany

The @OneToMany annotation is used to define a one-to-many relationship between two entities. This annotation is used to define the foreign key column name and the data type of the foreign key.

Examples of Annotation in JPA

The following are some examples of how to use annotations in JPA:

Defining an Entity

The following example shows how to define an entity using the @Entity annotation:

@Entity
public class Employee {
    // fields
}

Defining a Table

The following example shows how to define a table using the @Table annotation:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
    // fields
}

Defining a Column

The following example shows how to define a column using the @Column annotation:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
    @Column(name="FIRST_NAME", length=50)
    private String firstName;
}

Defining a Primary Key

The following example shows how to define a primary key using the @Id annotation:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
    @Id
    @Column(name="EMPLOYEE_ID")
    private Long id;
}

Defining a Many-to-One Relationship

The following example shows how to define a many-to-one relationship using the @ManyToOne annotation:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
    @ManyToOne
    @JoinColumn(name="DEPARTMENT_ID")
    private Department department;
}

Defining a One-to-Many Relationship

The following example shows how to define a one-to-many relationship using the @OneToMany annotation:

@Entity
@Table(name="DEPARTMENT")
public class Department {
    @OneToMany(mappedBy="department")
    private List<Employee> employees;
}

Conclusion

Annotations are an important part of JPA and are used to define the mapping between Java classes and database tables. Annotations can be used to define the table name, column names, primary key, foreign key relationships, and data types. In this blog post, we discussed the different types of annotations used in JPA and looked at some examples of how to use annotations in JPA.

반응형