Jpa basics (5): JPA ing for field types such as date_enumeration

Source: Internet
Author: User

Enumeration class:

 
1 Public EnumGender {
2Man, woman
3}

Person class:

 1   Import Java. util. date;
2
3 Import Javax. Persistence. column;
4 Import Javax. Persistence. entity;
5 Import Javax. Persistence. enumtype;
6 Import Javax. Persistence. enumerated;
7 Import Javax. Persistence. generatedvalue;
8 Import Javax. Persistence. ID;
9 Import Javax. Persistence. Table;
10 Import Javax. Persistence. Temporal;
11 Import Javax. Persistence. temporaltype;
12
13 @ Entity
14 @ Table (name = "persontable ")
15 Public Class Person {
16 @ ID
17 @ Generatedvalue
18 Private Integer ID;
19 @ Column (length = 10, nullable = False , Name = "personname ")
20 Private String name;
21 @ Temporal (temporaltype. Date)
22 Private Date birthday;
23 @ Enumerated (enumtype. String)
24 @ Column (length = 5, nullable = False )
25 Private Gender gender = gender. Man;
26 Public Person (){}
27 Public Person (string name ){
28 This . Name = Name;
29 }
30 Public Integer GETID (){
31 Return ID;
32 }
33 Public Void Setid (integer ID ){
34 This . ID = ID;
35 }
36 Public String getname (){
37 Return Name;
38 }
39 Public Void Setname (string name ){
40 This . Name = Name;
41 }
42 Public Date getbirthday (){
43 Return Birthday;
44 }
45 Public Void Setbirthday (date birthday ){
46 This . Birthday = birthday;
47 }
48 Public Gender getgender (){
49 Return Gender;
50 }
51 Public Void Setgender (gender Gender ){
52 This . Gender = gender;
53 }
54 }
    • @ Table (name = "persontable") // change the ing table name in the database
    • Private gender Gender = gender. Man; // you can set the default value here. Gender is an enumeration type.
    • @ Enumerated (enumtype. String) // It indicates that this attribute is an enumeration type. The enumerated strings in the brackets indicate the enumerated strings stored in the database rather than the enumerated indexes.
    • @ Temporal (temporaltype. Date) // indicates that this property is mapped to a database as a date type, and the brackets contain the date format.

The object is created for us by hibernate. When we get an object by ID, this object is returned to us. The object is created by using the reflection technology inside hibernate, and the default constructor is used for reflection, therefore, a public non-argument constructor must be provided.

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.