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;
... ...
}