JPA basic object ing Learning

Source: Internet
Author: User

An object class must follow the following requirements:

1. The class must be annotated with javax. Persistence. entity.

2. The class must have a public or protected non-parameter constructor. There can be other constructors.

3. The class cannot be declared as final.

4. The object class can inherit the object class or non-entity class. Non-entity classes can inherit entity classes.

 

Fields that can be persisted in JPA:

    • Original Java type
    • Java. Lang. String
    • Serializable type
    • Wrappersof Java primitive types
    • Java. Math. biginteger
    • Java. Math. bigdecimal
    • Java. util. Date
    • Java. util. Calendar
    • Java. SQL. Date
    • Java. SQL. Time
    • Java. SQL. Timestamp
    • User-definedserializable types
    • Byte []
    • Byte []
    • Char []
    • Character []
    • Enumerated types
    • Other entity or entity set
    • Embedded class

 

Basic Object ing

@ Entity

@ Secondarytable (name = "salary ")

Public class employee implements serializable {

@ ID

@ Column (name = "emp_id ")

@ Generatedvalue (Strategy = generationtype.Sequence)

Private long ID;

 

@ Basic

@ Version

Private long version;

 

@ Basic

@ Column (name = "f_name ")

Private string firstname;

 

@ Basic

@ Column (name = "l_name ")

Private string lastname;

 

@ Basic

@ Column (name = "gender ")

@ Enumerated (enumtype.String)

Private gender Gender = gender.Male;

 

@ Column (Table = "salary ")

Private double salary;

......

}

 

 

 

Abstract entity Annotation

@ Entity

Public abstract class employee {

@ ID

Protectedinteger employeeid;

...

}

@ Entity

Public class fulltimeemployee extends employee {

Protectedinteger salary;

...

}

@ Entity

Public class parttimeemployee extends employee {

Protected floathourlywage;

}

 

 

Super class ing

 

 
@ Mappedsuperclass
Public class employee {
 
@ ID
 
Protected integer employeeid;
 
...
 
}
 
@ Entity
 
Public class fulltimeemployee extends employee {
 
Protected integer salary;
...
 
}
 
@ Entity
 
Public class parttimeemployee extends employee {
 
Protected float hourlywage;
 
...
 
}

 

Embedded entity class ing

@ Embeddable

// Embed the content into other entities and share the identity with them

Public class employmentperiod {

// @ Temporal must be a field of the Java. util. Date and Java. util. Calendar type,

// And it can only be for such type fields or attention encoding.

@ Temporal (temporaltype.Date)

Private calendar startdate;

@ Temporal (temporaltype.Date)

Private calendar enddate;

... ...

}

Public classemployee implements serializable {

... ...

@ Embedded

@ Attributeoverrides ({

@ Attributeoverride (name = "startdate", column = @ column (name = "start_date ")),

@ Attributeoverride (name = "enddate", column = @ column (name = "end_date "))})

Private employmentperiod period;

... ...

}

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.