JPA learning --- Section 5: JPA ing of field types such as date and enumeration

Source: Internet
Author: User

1. In the previous section, we can see in the database that the created tables and fields are created through Entity bean. What are the rules for creating table names and field names?

The code is as follows:

Package learn. jpa. bean; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; @ Entitypublic class Person {@ Id @ GeneratedValue private Integer id; private String name; public Person () {} public Person (String name) {this. name = name;} 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 ;}}

Table name: it is named based on the Entity name.

Field: It is named based on the attribute name.

Private Integer id;
Private String name;

The above IDs and names are called fields, and the IDs in getId () and setId () are called attributes.

2. Change the table name and field name or length, as shown in the following code:

Package learn. jpa. bean; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. generatedValue; import javax. persistence. id; import javax. persistence. table; @ Entity @ Table (name = "t_person") public class Person {@ Id @ GeneratedValue private Integer id; @ Column (length = 64, name = "personName ", nullable = false) private String name; public Person () {} public Person (String name) {this. name = name;} 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 ;}}

3. Date and enumeration field Ing. The code is as follows:

Entity class:

Package learn. jpa. bean; import java. util. date; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. enumType; import javax. persistence. enumerated; import javax. persistence. generatedValue; import javax. persistence. id; import javax. persistence. table; import javax. persistence. temporal; import javax. persistence. temporalType; @ Entity @ Table (name = "t_person") public class Person {@ Id // @ Id is used to mark the attribute's primary key @ GeneratedValue private Integer id; @ Column (length = 64, name = "personName", nullable = false) private String name; // TemporalType. DATE indicates the DATE format, for example, 2014-10-01 // TemporalType. TIME indicates the TIME format, for example, 12: 21: 34 // TemporalType. TIMESTAMP indicates the date plus time format example: 12:21:34 @ Temporal (TemporalType. DATE) private Date brithday; // @ Enumerated annotation enumeration type // EnumType. STRING indicates that the value saved to the database is MAN or WOMEN // EnumType. ORDINAL indicates that the value saved to the database is the index value set by MAN and WOMEN @ Enumerated (EnumType. STRING) @ Column (length = 5, nullable = false) private Gender gender; public Person () {} public Person (String name) {this. name = name;} 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 Date getBrithday () {return brithday;} public void setBrithday (Date brithday) {this. brithday = brithday;} public Gender getGender () {return gender;} public void setGender (Gender gender) {this. gender = gender ;}}

Enumeration class:

Package learn. jpa. bean;/***** @ doc Gender enumeration type **/public enum Gender {MAN, WOMEN ;}

Note:

1. TemporalType. DATE indicates the DATE format, for example
2. TemporalType. TIME indicates the TIME format. Example: 12: 21: 34
3. TemporalType. TIMESTAMP indicates the date plus time format. Example: 12:21:34

@ Enumerated annotation enumeration type
1. EnumType. STRING indicates that the value saved to the database is MAN or WOMEN.
2. EnumType. ORDINAL indicates that the value saved to the database is the index value set by MAN and WOMEN.

JPA learning --- Section 5: JPA ing of field types such as date and enumeration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.