persistent entity

JPA is actually the persistence layer solution of the JavaEE platform. It uses an object-relational mapping technology to automatically persist Java objects, including three parts: entity, entity manager and Java persistence query language.

 

@Entity: This annotation is used to indicate that the class it is in is an entity type.

@Table: This annotation is used to set the table name to which the entity is to be mapped.

@Id: This annotation is used to set a property as the primary key or unique identifier of the entity.

@GeneratedValue: This annotation is used to specify how the primary key value is generated. This annotation is used in conjunction with the @Id annotation on the primary key attribute.

public @interface GeneratedValue
{
    GenerationType strategy() default AUTO;
    String generator() default "";
}

 

The generator() property defines the name of the primary key value generator. If the primary key value generation strategy of the entity is not GenerationType.AUTO or GenerationType.IDENTITY, you need to provide the corresponding SequenceGenerator or TableGenerator annotation, and then set the generator() attribute value to the name attribute value of the annotation.

 

The strategy() attribute specifies the field value generation strategy. Its attribute value is an enumeration type with four optional values: TABLE, SEQUENCE, IDENTITY, AUTO.

(1) GenerationType.TABLE: Use this value to create a table to generate a data type serial number and use the serial number as the primary key value. Database porting is easier using this strategy.

(2) GenerationType.SEQUENCE: Use the SEQUENCE (sequence) of the database to generate the primary key value.

(3) GenerationType.IDENTITY: Use the database ID self-increasing method to generate the primary key value.

(4) GenerationType.AUTO: A suitable generation method is selected by the container according to the database type.

 

@Column: This annotation specifies which field in the data table the member attribute of the entity is mapped to and some structural information of the field. This annotation needs to be marked on the getter method of the member attribute.

 

Member property mapping

 

@Transient: By default, all fields mapped by entity member properties become persistent fields. If you don't need some member properties to be persistent fields, you can use this annotation.

@Enumerated: You can use this annotation if you need to map enumeration type member properties to the database.

@Lob: This annotation is specially used to map large data types. When the type of the entity member attribute is byte[], Byte[] or java.io.Serializable, the annotation will be mapped to the Blob type of the database. When the type is char[], Character[] or java.lang.String, the annotation will be mapped to the database's Clob type.

@Basic: For large data types annotated with @Lob, in order to avoid occupying a lot of memory each time an entity is loaded, it is necessary to delay loading this property, and this annotation can be used at this time. (The FetchType() property specifies whether to delay loading, the default is immediate loading. The optional() property specifies whether the field is allowed to be null when generating the database structure.)

@Temporal: This annotation indicates which type is mapped to the database data, time and timestamp.

 

It is recommended to overload the equals and hashcode methods

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326752443&siteId=291194637