Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Entity

Shulou Source: shulou.com Published: 2022-06-02 01:28:09 09月30日 Update

This article mainly introduces the relevant knowledge of how to use Entity, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use Entity. Let's take a look at it.

Entity is based on the JPA specification. Please refer to the JPA or Hibernate documentation for more detailed technical details.

Configuration file

Com.jspxcms.plug.ContextConfig 's @ EntityScan ({"com.jspxcms.plug.domain"}) automatically scans for classes that contain @ Entity annotations under the package.

Database table

Instead of using the primary key self-increment policy, use JPA's TABLE primary key generation strategy to place the primary key in a table in the database that defaults to Hibernate_sequences in Hibernate. So don't use the primary key to augment when building a table.

Create table plug_resume (f_resume_id int not null, f_site_id int not null, f_name varchar) not null comment 'name', f_post varchar (100) not null comment 'job application', f_creation_date datetime not null comment 'delivery date', f_gender char (1) not null default 'M'comment' gender' F_birth_date datetime comment 'date of birth', f_mobile varchar 'comment' mobile phone', f_email varchar 'comment' email', f_expected_salary int comment 'expected salary', f_education_experience longtext comment 'educational experience', f_work_experience longtext comment 'work experience', f_remark longtext comment 'remarks' Primary key (f_resume_id)) engine = innodb Alter table plug_resume comment 'resume'; alter table plug_resume add constraint fk_plug_resume_site foreign key (f_site_id) references cms_site (f_site_id) on delete restrict on update restrict; entity class

Use JPA's TABLE primary key generation policy.

Note the following three values: name = "tg_plug_resume", pkColumnValue = "plug_resume" generator = "tg_plug_resume", where plug_resume is the table name, and if the table name is abc, the three values are name = "tg_abc" and pkColumnValue = "abc" generator = "tg_abc".

InitialValue = 1 means that the primary key starts at 1. AllocationSize = 10 means that hibernate gets 10 primary key values at a time. If the system is rebooted before running out, there will be discontiguous primary keys in the database. However, because getting the primary key value requires querying and modifying the database, it is a great overhead for tables that insert data frequently, so this value can be adjusted appropriately according to the situation.

If you use MySQL's primary key self-increment, in addition to adding the primary key self-increment attribute to the table primary key, the ID annotation in Entity should also be changed to @ GeneratedValue (generation = IDENTITY) or @ GeneratedValue (generation = AUTO).

Package com.jspxcms.plug.domain;@Entity@Table (name = "plug_resume") public class Resume implements java.io.Serializable {private Integer id;. @ Id @ Column (name = "f_resume_id", unique = true, nullable = false) @ TableGenerator (name = "tg_plug_resume", pkColumnValue = "plug_resume", initialValue = 1, allocationSize = 10) @ GeneratedValue (strategy = GenerationType.TABLE, generator = "tg_plug_resume") public Integer getId () {return this.id;} public void setId (Integer id) {this.id = id;}. } this is the end of the article on "how to use Entity". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Entity". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Data database knowledge policy three representation content situation date comment article generation large appropriate frequent value comment name entity attribute Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Xiaomi Apple macOS