SpringDataJpa common entity class notes

Recently the company in the use of SpringDataJpa, we need to create an entity class, to create the database table structure by entity type, generate a database table.

Here we look at some commonly used when creating the entity classes of notes it! ! !

 

1. The entity class used annotations

@Entity

This identifies the entity class is a JPA entity, remember to tell JPA generated entity class corresponding to the table when the program is running

@Table (name = "custom table")

Customizations entity class corresponding to a database table name, the default is the entity class name. Note: Mysql keyword not set to an entity class, create a table unsuccessful problem occurs. For example: leave

@Id

This variable inside the class where the primary key Id

@GeneratedValue

Set the primary key generation strategy, in this way depending on the particular database, if the database does not support the increment primary key, then the type is not used

@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "repair_seq"): is a front key generation strategy sequence (may be Auto, IDENTITY, native, etc., represents Auto switch between a plurality of databases), the name of the specified sequence is repair_seq.

E.g:

 

 

 

@Basic

Attribute represents a simple mapping database table field, marked for no getXXXX () method, that is, the default @Basic fetch: this attribute indicates the read policy, LAZY and EAGER there are two, namely a front support grip take and lazy loading, the default is EAGER.

@Column

@Column an ​​explanatory column, if the column names the same as the field name, may be omitted. @Column annotation property details

(1) name attributes: fields in the database table is marked as a corresponding name field;
(2) length attribute: indicates the length of the field, when the field of type varchar, this property will be effective, the default is 255 characters ;
(. 3) Nullable properties: indicates whether the field can be null value, the default is true.
(4) unique properties: This field indicates whether the unique identifier, the default fasle.
(5) precision and scale attributes: precision scale properties and attributes representing the precision when the field type is double, precision of the total length value, scale the number of bits occupied by the decimal point.

@Transient

Indicates that the property is not mapped to a field in a database table, ORM framework will ignore the property. If the property is not a field mapping database tables, be sure to mark it as @Transient, otherwise, ORM framework annotated as its default @Basic

@Temporal

@Temporal notes indicate the date and time format. There are three default is @Temporal (TemporalType.TIMESTAMP). As follows:
(. 1) @Temporal (TemporalType.DATE): date, page retrieval result format such as: 2016-08-05
(2) @Temporal (TemporalType.TIME): Time, page retrieval result is: 13:46:25
(3) @Temporal (TemporalType.TIMESTAMP): time and date, the page is to get results: 2016-08-05 13:46:25

There are two ways Notes: 

Written in the field:

    @Temporal(TemporalType.TIMESTAMP)
    private Date birthday;

Written on getXxx method:

    @Temporal(TemporalType.DATE)
    @Column(name = "birthday", length = 10)
    public Date getBirthday() {
        return this.birthday;
    }

@Enumerated ( "need to define the type of deposit database")

Use @Enumerated enumeration mapping field, I've established a gender enumeration type as sex, and keep up the rear is stored in the database as a String type deposit.

Example entity class (Customer):

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhukf/p/11813556.html
Recommended