Spring Data JPA @Entity Guide

To use the @Entity annotation in Spring Data JPA, you first need to create an entity class and add the @Entity annotation on the class name. This informs the Spring Data JPA framework that this class is a JPA entity that can be mapped to a database table.

Here is a simple example:

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {
    
    @Id
    private Long id;
    private String username;
    private String email;
    
    // 省略getter和setter方法

}

In the example above, we created an entity class named User and added the @Entity annotation on the class name. Within this class, we used the @Id annotation to specify the primary key field, as well as other regular fields such as username and email.

Once you have defined an entity class and marked the class name with the @Entity annotation, Spring Data JPA will automatically generate the database table structure based on the entity class. You can use this entity class to perform database operations such as saving, updating, and deleting.

Please note that the @Entity annotation also supports additional properties such as specifying table names, schemas, etc. You can configure these properties as needed.

bannerAds