Spring Boot Spring Data JPA創建實體類

  • 在前面我們提到過ORM(Object Relational Mapping),這裏面的O對應的是架構中應用層的對象實體。在JAP中,我們需要創建一個類對應這個實體,而這個實體一般對應着數據庫中的一張表。接下來我們好好講一下,如何創建一個實體類,以及相關的註解。

  • 示例代碼:

import javax.persistence.*;

@Entity(name = "user")
@Table("t_user")
public class user {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;
    @Column(name = "address")
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

當我們創建好了這個實體類,運行項目,框架會掃描到該類的註解,並且查看數據庫中是否有這麼一張對應的表。如果存在該表,再檢查對應的屬性是否相同。如果不同,框架會根據實體類更新數據庫的表(前提是我們配好了"ddl-auto: update",數據定義自動更新)。下面,我們來一一介紹在實體類中使用到的註解。

  • @Entity:
  1. The @Entity annotation is used to specify that the currently annotate class represents an entity type. Unlike basic and embeddable types, entity types have an identity and their state is managed by the underlying Persistence Context.
    @Entity註解用於指定當前被註解的類爲一個實體類;與其他類型不同,實體類含有一個標識(主鍵),其狀態被當前持久化上下文管理。

  2. The @Entity annotation defines just one attribute name which is used to give a specific entity name for use in JPQL queries. By default, the entity name represents the unqualified name of the entity class itself.
    打開源碼我們可以發現@Entity註解可以設置name屬性值,該屬性值默認爲空。如果爲空,框架默認類名爲其值;該值用於生成查詢語句。

  • @Table:
    1 The @Table annotation is used to specify additional information to a JPA @Table annotation, like custom INSERT, UPDATE or DELETE statements or a specific FetchMode.
    2 該註解有比較多的屬性可以設置,具體如下:
    String catalog
    (Optional) The catalog of the table.
    Index[] indexes
    (Optional) Indexes for the table.
    String name
    (Optional) The name of the table.
    String schema
    (Optional) The schema of the table.
    UniqueConstraint[] uniqueConstraints
    (Optional) Unique constraints that are to be placed on the table.

  • @Id:

  1. Specifies the primary key of an entity. The field or property to which the Id annotation is applied should be one of the following types: any Java primitive type; any primitive wrapper type; String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.
    @Id註解指定一個實體的主鍵,該註解可以用於Java的所有原始類型,元素類型對應的包裝類和以上所示類。
    2 The mapped column for the primary key of the entity is assumed to be the primary key of the primary table. If no Column annotation is specified, the primary key column name is assumed to be the name of the primary key property or field.
    註解的屬性被認爲是對應表的主鍵,如果實體類中沒有屬性被@Id註解,那麼實體類的主鍵名被默認爲數據庫表的主鍵名。
  • @GeneratedValue:
  1. Provides for the specification of generation strategies for the values of primary keys.
    爲主鍵值的生成指定策略。
  2. The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.
    該主鍵配合@Id註解使用,策略如下(需要根據數據庫的支持,選擇使用對應的生成策略):
    AUTO
    Indicates that the persistence provider should pick an appropriate strategy for the particular database.
    IDENTITY
    Indicates that the persistence provider must assign primary keys for the entity using a database identity column.
    SEQUENCE
    Indicates that the persistence provider must assign primary keys for the entity using a database sequence.
    TABLE
    Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.
  • @Column:
  1. Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.
    指定實體類屬性與數據庫表中對應的屬性名。
  2. 該註解有比較多的屬性可以設置,具體如下:
    String columnDefinition
    (Optional) The SQL fragment that is used when generating the DDL for the column.
    boolean insertable
    (Optional) Whether the column is included in SQL INSERT statements generated by the persistence provider.
    int length
    (Optional) The column length.
    String name
    (Optional) The name of the column.
    boolean nullable
    (Optional) Whether the database column is nullable.
    int precision
    (Optional) The precision for a decimal (exact numeric) column.
    int scale
    (Optional) The scale for a decimal (exact numeric) column.
    String table
    (Optional) The name of the table that contains the column.
    boolean unique
    (Optional) Whether the column is a unique key.
    boolean updatable
    (Optional) Whether the column is included in SQL UPDATE statements generated by the persistence provider.
  • 除了這些註解,其實還有其他註解沒有列出。這裏提供一個鏈接,提供參考:hibernate使用說明
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章