Home  >  Article  >  Java  >  How do annotations in Java Persistence API (JPA) map to object-relational mapping?

How do annotations in Java Persistence API (JPA) map to object-relational mapping?

王林
王林Original
2024-05-06 16:33:01500browse

JPA annotation mapping maps Java objects to relational database tables, simplifying interaction with the database. Entity annotations mark persistent objects (@Entity), field annotations specify database column mappings (@Column, @Lob), and relationship mapping annotations represent relationships between entities (@OneToOne, @OneToMany, @ManyToMany). For example, in the Employee and Department entities, @OneToOne represents a one-to-one relationship, and @OneToMany represents a one-to-many relationship.

Java Persistence API(JPA)中的注解如何映射对象-关系映射?

Annotation mapping in Java Persistence API (JPA): Object-relational mapping

Java Persistence API (JPA) provides A way to map Java objects to relational database tables using annotations. This mapping is called object-relational mapping (ORM), and it simplifies the process of interacting with the database.

Entity annotations

Entity annotations are used to mark a Java class to indicate that it is a persistent object. Commonly used entity annotations include:

  • @Entity: Indicates that a class is a JPA entity.
  • @Id: Indicates that a persistent field is the primary key of the entity.

Field annotations

Field annotations are used to specify the database column mapping of the field. Commonly used field annotations include:

  • @Column: Specify the database column name, type and other attributes of the field.
  • @Lob: Indicates that the field is a large object (LOB), such as text or an image.

Relationship mapping annotations

Relationship mapping annotations are used to represent the relationship between two entities. Commonly used relationship mapping annotations include:

  • @OneToOne: one-to-one relationship.
  • @OneToMany: One-to-many relationship.
  • @ManyToMany: Many-to-many relationship.

Practical case

Suppose we have an Employee entity and a Department entity, they have the following relationship : Each department can have multiple employees, and each employee can only belong to one department.

@Entity
public class Employee {
    @Id
    private Long id;
    private String name;

    @OneToOne
    private Department department;
}

@Entity
public class Department {
    @Id
    private Long id;
    private String name;
    
    @OneToMany(mappedBy = "department")
    private Set<Employee> employees;
}

In the previous code, the @OneToOne annotation indicates that there is a one-to-one relationship between Employee and Department, one employee corresponds to one department. The @OneToMany annotation indicates that there is a one-to-many relationship between Department and Employee, where one department corresponds to multiple employees.

The above is the detailed content of How do annotations in Java Persistence API (JPA) map to object-relational mapping?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn