Entity Mapping Basics

Source: Internet
Author: User
Tags time and date

An ordinary Pojo class can be mapped to a persisted class by labeling @entity, and a persisted class can correspond to the data in the database. The mapping becomes an entity class to be dependent on some specific rules.

1. Mapping entities (@Entity)
The class labeled @entity annotation indicates that the class is a persisted class, and when in the container, the server will first load all the entity classes annotated with the @entity annotation, for example:

?
123456 @EntitypublicclassContaceEO {   ...   ContactEO() {...}   ...}

The properties for the definition of @Entity annotation are as follows:

?
1234 @Target(TYPE) @Retention(RUNTIME)public@interface Entity {  String name() default"";}

Where the name attribute represents the name of the entity, and if not set, the default is the name of the callout entity class.
Note: An entity class labeled @entity must have at least one non-parametric construction method.

2. Callout primary KEY (@Id)
Used to label the primary key of an associated database in an entity class

3. Mapping table (@Table), Mapping methods and properties (@Column)
As the following table structure:

?
1234 createtable contact (  id int(20) not null,  name varchar(50) defaultnull,)

Change the entity class code to:

?
1234567891011 @Entity@Table(name="contact")public class ContactEO implements java.io.Serializable {  @Id  private Integer id;  @Column(name="name")  private String name;  //省略get、set方法}

The properties of the @Table are defined as follows:

?
1234567 @Target({TYPE}) @Retention(RUNTIME)public @interface Table {  String name() default "";  String catalog() default "";  String schema() default "";  UniqueConstraint[] uniqueConstraints() default {};}

Here are a few questions to consider:
A, the tag to precede the class
B, the Name property represents the names of the tables that correspond to the entities
The C, catalog, and Shema properties represent the directory name or database name specified by the entity
D, the Uniqueconstraints Tag property represents the unique constraint associated with the entity, and an entity can have multiple unique constraints

?
1234 @Entity@Table(name="contact", uniqueConstraints = {  @UniqueConstraint(columnNames = {"name", "email"})})

Indicates that the name and email fields of the specified table are unique, that is, there cannot be a record of exactly the same name and email value, equivalent to SQL:

?
12 ...uniquekeyname_email (name,email)

The properties of the @Column tag are defined as follows:

?
12345678910111213 @Target({METHOD,FIELD}) @Retention(RUNTIME)public @interface Column {  String name() default "";  boolean unique() default false;  boolean nullable() default true;  boolean insertable() default true;  boolean updateable() default true;  String columnDefinition() default "";  String table() default "";  int length() default 255;  int precision() default 0;  int scale() default 0;}

There are several issues to note when using @column tags:
A, this tag can be labeled before the Getter method or before the property
b, the unique attribute indicates whether the field is a unique identity, and the default is False
The C, ColumnDefinition property represents the SQL statement created by this field when the table is created, typically used when generating table definitions through entity.
The D, Table property represents a field in a table that specifies a table when multiple tables are mapped.
The E, precision, and scale properties represent precision, and when the field type is double, precision represents the total length of the numeric value, and the scale represents the number of digits in the decimal point.
For example: Customize the SQL statement for the CLOB type field, with the following code:

?
12 @Column(name="constact_name", columnDefinition="clob not null")privateString name;

The resulting SQL statement is as follows:

?
12345 createtable contact (  id int not null;  contact_name clob(200) not null;  primary key(id))


4. Basic data types that can be persisted
(1) Java basic type: byte, int, short, long, Boolean, char, float, double
(2) wrapper classes corresponding to the Java underlying data type: Byte, Integer, short, Long, Boolean, Character, Float, Double
(3) byte-and-character array: byte[], byte[], char[], character[]
(4) Large value type: Java.math.BigInteger, Java.math.BigDecimal
(5) String type: string
(6) Time and date type: Java.util.Date, Java.util.Calendar, Java.sql.Date, Java.sql.Time, Java.sql.TimeStamp
(7) Enum type: User-defined enumeration type
(8) Entity type: class labeled @entity
(9) contains a collection of entity types Collection class: Java.util.Collection, Java.util.List, Java.util.Map
(10) embedded (embeddable) class

5. @Basic Set Load Mode
It is defined as follows:

?
12345 @Target({METHOD, FIELD}) @Retention(RUNTIME)public@interface Basic {  FetchType fetch() default EAGER;  boolean optional() defaulttrue;}

Entity Mapping Basics

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.