SpringBoot JPA annotation details

1.@OneToOne
2.@OneToMany
targetEntity: Default associated entity type. If a specific type is specified in the collection class, you do not need to use targetEntity. Otherwise, targetEntity needs to be specified
Cascade: Cascade Operations
CascadeType.PERSIST Cascade persistence (save) operations
CascadeType. MERGE cascade update (merge) operation
CascadeType. REFRESH Cascade refresh operation, only query get operation
CascadeType. REMOVE Cascade delete operation
CascadeType. ALL Cascade all the above operations
Fetch: Whether the fetch is lazy loaded or not. By default, the first side is to load FetchType.EAGER immediately, and the more side is to delay loading of FetchType.LAZY
mappedBy: relationship maintenance
3.@ManyToOne
4. @ManyToMany
5.@JoinColumn
6. Default @Basic without annotation
@Basic represents a simple mapping of attributes to fields in a database table. For getXxxx() methods without any annotations, the default is @Basic
fetch: Indicates the read strategy of the attribute, there are two types: EAGER and LAZY, which represent main branch fetching and lazy loading respectively. The default is EAGER.
optional: indicates whether the property is allowed to be null, the default is true
Set POJO to entity @Entity
@Entity
public class Demo {
}
Set the table name @Table
@Table(name = "demo", schema = "sd")
public class Demo {
}
Set primary key @Id
@Id
private String id;
Set field type @Column
name: field name
unique: is it unique
nullable: whether it can be null
inserttable: whether it can be inserted
updateable: whether it can be updated
columnDefinition: Define the DDL that creates this column when creating the table
secondaryTable: Secondary table name. If this column is not built on the master table (by default it is built on the master table), this attribute defines the name of the slave table where the column is located.
@Column(name = "user_code", nullable = false, length=32)//Set the field corresponding to the attribute userCode to user_code, the length is 32, not empty
private String userCode;
@Column(name = "user_wages", nullable = true, precision=12, scale=2)//Set the field corresponding to the attribute wages to user_wages, 12 digits can retain two decimal places, can be empty
private double wages;
@Temporal(TemporalType.DATE)//Set to time type
private Date joinDate;  
Field sorting, you can specify the order for it when loading data @OrderBy
@OrderBy("group_name ASC, name DESC")
private List<Demo> books = new ArrayList<Demo>();
7. Primary key generation strategy @GeneratedValue
TABLE: Use a specific database table to hold the primary key.
SEQUENCE: The primary key is generated based on the sequence of the underlying database, provided that the database supports sequences.
IDENTITY: The primary key is automatically generated by the database (mainly automatic growth type)
AUTO: The primary key is controlled by the program (also the default, when specifying the primary key, if the primary key generation strategy is not specified, the default is AUTO)
The database supports the four strategies of GeneratorValue as follows:
MYSQL
GenerationType.TABLE
GenerationType.AUTO
GenerationType.IDENTITY
GenerationType.SEQUENCE is not supported
ORACLE
strategy=GenerationType.AUTO
GenerationType.SEQUENCE
GenerationType.TABLE
GenerationType.IDENTITY is not supported
GenerationType.TABLE
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator="payablemoney_gen")
@TableGenerator(name = "pk_gen", table="tb_generator",
        pkColumnName="gen_name",  valueColumnName="gen_value",
        pkColumnValue="PAYABLEMOENY_PK", allocationSize=1)
private String id;
GenerationType.SEQUENCE
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq")
@SequenceGenerator(name="payablemoney_seq", sequenceName="seq_payment")
private String id;
GenerationType.IDENTITY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
GenerationType.AUTO
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
8. Primary key generation strategy @GenericGenerator
The @GenericGenerator annotation is a custom primary key generation strategy generator provided by hibernate, and the multi-defined strategy is implemented by @GenericGenerator. Therefore, it should be used together with @GeneratedValue, and the "generator" attribute in the @GeneratedValue annotation should be consistent with the name attribute in the @GenericGenerator annotation, and the strategy attribute indicates the primary key generation strategy of hibernate.
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq")
@GenericGenerator(name = "payablemoney_seq", strategy = "uuid")
private String id;
@GenericGenerator supports 13 strategies, namely:
    GENERATORS.put("uuid", UUIDHexGenerator.class);  
	GENERATORS.put("hilo", TableHiLoGenerator.class);  
	GENERATORS.put("assigned", Assigned.class);  
	GENERATORS.put("identity", IdentityGenerator.class);  
	GENERATORS.put("select", SelectGenerator.class);  
	GENERATORS.put("sequence", SequenceGenerator.class);  
	GENERATORS.put("seqhilo", SequenceHiLoGenerator.class);  
	GENERATORS.put("increment", IncrementGenerator.class);  
	GENERATORS.put("foreign", ForeignGenerator.class);  
	GENERATORS.put("guid", GUIDGenerator.class);  
	GENERATORS.put("uuid.hex", UUIDHexGenerator.class); //uuid.hex is deprecated  
	GENERATORS.put("sequence-identity", SequenceIdentityGenerator.class);   
9. Big Field @Lob
@Lob // Corresponds to Blob field type
@Column(name = "PHOTO")
private Serializable photo;
@Lob // Corresponding to Clob field type
@Column(name = "DESCRIPTION")
private String description;
10. Transient Field @Transient
@Transient // There is no need to map fields with the database, and there is no need to save the database when saving
private Date testDate;
11. Format time according to time zone @JsonFormat
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08")
private Date createTime;
12.@NotFound
action: used to specify what to do if the referenced foreign key does not exist
NotFoundAction.EXCEPTION (default) throws an exception
NotFoundAction.IGNORE ignored
13.@Where
Clause: data is too much, just write filter conditions
@OneToMany(mappedBy = "cost", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true)
@Where(clause = "del_flag = 0")
public List<CostVasItem> getItems() {
    return items;
}
14. Allow Cross-Origin Requests @CrossOrigin
@CrossOrigin(origins = "*")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325754667&siteId=291194637