introduction
In Java enterprise-level development, data persistence is an extremely critical link. Developers often need to store Java objects into a database, or read data from the database and convert them into Java objects. JPA (Java Persistence API) is a standard object-relational mapping (ORM) solution provided by the Java EE platform, greatly simplifying this process. in,@Entity
Annotations occupy a core position in JPA, and it establishes a mapping relationship between Java entity classes and database tables. In-depth understanding@Entity
Annotations and their association with database tables are crucial to efficiently carry out data persistent development.
1. Overview of JPA and Entity Mapping
JPA is a standard for the Java platform that defines a set of APIs and specifications for object-relational mapping. With JPA, developers can operate databases in an object-oriented manner without writing a large number of SQL statements. Entity mapping is one of the core functions of JPA. It associates Java classes with database tables and maps the properties of Java objects to columns of database tables. In this way, developers can implement the addition, deletion, modification and search of the database by operating Java objects.@Entity
Annotations are annotations used in JPA to identify entity classes. The classes marked by it will be regarded as entities by JPA and then participate in the process of object-relationship mapping.
2. Basic use of @Entity annotation
@Entity
Annotations are used to mark a Java class as a JPA entity class. This class needs to meet certain conditions, such as having a parameterless constructor, usually there is a primary key attribute. Here is a simple example:
import ; import ; @Entity public class Product { @Id private Long id; private String name; private double price; public Product() { } public Product(Long id, String name, double price) { = id; = name; = price; } // Getters and Setters public Long getId() { return id; } public void setId(Long id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public double getPrice() { return price; } public void setPrice(double price) { = price; } }
In this example,Product
Class@Entity
Annotations are marked as entity classes.@Id
Annotation specifiedid
Attributes are the primary key. JPA will default to using the class name as the database table name and the attribute name as the column name of the table.
3. Specify the database table name
By default, JPA uses the name of the entity class as the name of the database table. However, developers can use@Table
Annotation to specify different table names. Examples are as follows:
import ; import ; import ; @Entity @Table(name = "products") public class Product { @Id private Long id; private String name; private double price; public Product() { } public Product(Long id, String name, double price) { = id; = name; = price; } // Getters and Setters public Long getId() { return id; } public void setId(Long id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public double getPrice() { return price; } public void setPrice(double price) { = price; } }
In the above code,@Table(name = "products")
The database table name is specifiedproducts
, not the default oneProduct
。
4. Map the columns of the database table
The properties of the entity class will be mapped to columns with the same name in the database table by default. But developers can use@Column
Annotation to specify different column names, and can also set other attributes of the column, such as whether to allow nullity, length, etc. Examples are as follows:
import ; import ; import ; import ; @Entity @Table(name = "products") public class Product { @Id private Long id; @Column(name = "product_name", nullable = false, length = 100) private String name; @Column(name = "product_price") private double price; public Product() { } public Product(Long id, String name, double price) { = id; = name; = price; } // Getters and Setters public Long getId() { return id; } public void setId(Long id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public double getPrice() { return price; } public void setPrice(double price) { = price; } }
In this example,@Column(name = "product_name", nullable = false, length = 100)
Willname
Properties map toproduct_name
Column, and set that column is not allowed to be empty, with a length of 100.
5. Inheritance and table mapping of entity classes
In Java, there may be an inheritance relationship in entity classes. JPA provides a variety of strategies to handle the mapping of entity class inheritance and database tables, such as single table inheritance, one table per concrete class, and join table inheritance. Here is an example of single table inheritance:
import ; import ; import ; import ; import ; import ; @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "product_type", discriminatorType = ) public abstract class Product { @Id private Long id; private String name; public Product() { } public Product(Long id, String name) { = id; = name; } // Getters and Setters public Long getId() { return id; } public void setId(Long id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } } @Entity public class ElectronicProduct extends Product { private String brand; public ElectronicProduct() { } public ElectronicProduct(Long id, String name, String brand) { super(id, name); = brand; } // Getters and Setters public String getBrand() { return brand; } public void setBrand(String brand) { = brand; } }
In the above code,@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
A single table inheritance policy is specified.@DiscriminatorColumn
Used to distinguish different types of entities.
Summarize
@Entity
Annotations are the basis for implementing the mapping of Java entity classes and database tables in JPA. By using@Entity
、@Table
、@Column
With the explanations, developers can flexibly control the mapping relationship between entity classes and database tables, including table names, column names, and column attributes. At the same time, JPA also provides a variety of strategies to handle inheritance and table mapping of entity classes. Understanding and mastering this knowledge will help developers to develop data persistence more efficiently and improve the maintainability and scalability of their code.
The above is a detailed content for a deep understanding of Java @Entity annotation and its association with database tables. For more information about Java @Entity annotation, please follow my other related articles!