jpa--mapping persisted objects (Entity)

Source: Internet
Author: User

an ordinary Pojo class can be mapped into a class that is persisted by @entity;

Class JavaBean Style:
• The class attribute must be private;
• There are getter and setter methods;

Mapped entities: @Entity
# @Entity Entity must have an argument-free construction method;
# Implement Serializable interface, it is recommended that each entity implements the interface;
# where, in entity, the name attribute represents the entity names, such as:
@Entity (name=contacts)
public class contactseo{
...
}
When JPA executes JPQL, it needs to use contacts as the entity name instead of Contactseo.
String JPQL = "SELECT * from Contacts C";
# If Name is not configured, the entity name defaults to the class name.

inheritable:
# entities can inherit, non-entity classes can inherit from entity classes, and entity classes can also inherit from non-entity classes;
# Abstract classes can also be labeled as entity classes;

Callout PRIMARY Key:
# An entity class must have at least one primary key (Primary key);
# Use @id to label as entity primary key;

default entity mappings:
# A class is labeled with @entity's persisted class, and if no other annotations are annotated, the properties and methods of the class are automatically mapped to the default tables and fields in the database. Such as:
@Entity
public class Contacteo Implement serializable{
Public Contacteo () {}
Private Integer ID;
private String name;
Getter and Setter methods slightly
}
The default database table name for the entity is: Contacteo, field: int id; varchar name;

mapping tables and fields:
# @Table Note To define a mapped table;
# @Column annotations can be defined to map to fields;

mapping Table @table:
@Target ({TYPE}) @Retention (RUNTIME)
Public @interface table{
String name () default "";
String catalog () default "";
String schema () defalut "";
Uniqueconstraint[] Uniqueconstraints () default {};
}
# @Table must be marked in front of the class name;
the # Name property represents the name of the table that the entity corresponds to;
# The catalog and schema properties represent the entity's specified point directory name or database name;
# The Uniqueconstraints property represents the unique constraint that the entity is associated with, an entity can have more than one unique constraint, and no constraint by default;
# If you use the uniqueconstraints tag, you need to match the tag uniqueconstraint tag to use;
Example:
@Entity
@Table (name= "Contact", schama= "jpadb", uniqueconstraints={
@UniqueConstraint (
ColumnNames = {"Name", "email"}
)},
@UniqueConstraint (
ColumnNames = {"Other_col_1", "other_col_2"}
)}
)
# Description: In comments, property values are case-insensitive.


Mapping methods and properties (@Column)
The @Column tag represents the fields in the table that the persisted property is mapped to.
@Target ({METHOD, FIELD}) @Retention (RUNTIME)
Public @interface column{
String name () default "";
Boolean unique () default false;
Boolean nullable () default false;
Boolean insertable () default false;
Boolean updateable () default false;
String columndefinition () default "";
String table () Defalut "";
int lenght () default 255;
int precision () default 0;
int scale () default 0;
}
# tags can be labeled before getter methods or before attributes;
# name is the field name;
# Unique is a unique identifier for the field, and the default is false;
# Nullable if the field can be a null value, the default is true;
# insertable If you need to insert the value of the field when inserting data using the Insert script;
# Updateable whether the value of the field needs to be updated when updating data using the "Update" script;
More than two are used for read-only properties, such as primary or foreign keys, and these fields are usually generated automatically.
# columndefinition represents the SQL statement created by this field when the table is created, typically used when generating table definitions through entity;
The # Table property represents the table name of the table in the specified table when multiple tables are mapped, with the default value being the primary table;
# Lenght indicates the length of the field;
# Both precision and scale represent precision;
Example:
@Column (name= "Contact_Name", Nullable=false, length=200)
@Column (name= "Contact_Name", Nullable=false, columndefinition= "Clob not NULL")

Mapping Optimizations:
· Basic Type VS Package type
# when NULL, if the corresponding property of the entity is of type int, converting a null to int must produce a conversion exception, but if the Entity property type is integer, it is an object and the value of the object can be null, there is no problem at this time ;
# It is recommended that the attributes of the callout entity use the Java Basic Type wrapper class (which may sacrifice some conversion efficiency);

· @Basic Setting the Load mode
@Target ({METHOD, FIELD}) @Retention (RUNTIME)
Public @interface Basic {
Fetchtype fetch () default EAGER;
Boolean optional () default true;
}
# The default entity properties are loaded in real time (EAGER);
# Two modes of loading:
Lazy lazily loading
Eager instant load (default)
Example: @Basic (Fetch=fetchtype.eager)

primary Key mappings:
# Primary Key identification @id
# @GeneratedValue PRIMARY key generation strategy: TABLE, SEQUENCE, IDENTITY, AUTO.
@GeneratedValue (strategy = generationtype.sequence-oracle
@GeneratedValue (strategy = generationtype.identity-sql Server
# Build policy: Self-increment primary key, table Builder (@TableGenerator), Sequence Generator (@Sequence), identity generator, composite primary key (@IdClass), and embedded primary key (@Embeddedld).
# JPA definable build primary key policy comparison:
· Sequence,identity is mainly for some special database, do not determine the type of database to be supported by the system, it is best not to use.
· Auto is used to compare simple primary keys with less requirement for primary key generation policy
· Table generation strategy is to persist the value of the primary key in the database table, because as long as the relational database, you can create a table, specifically to save the generated values, thus eliminating the incompatibility between the database, can not only guarantee the support of a variety of databases, but also a certain degree of flexibility, recommended to use.
• If the above method does not meet the requirements, you can set the value of the primary key by certain rules, you can use the UUID to automatically generate it in the program, and then map to the entity's primary key, not through the JPA primary key generation policy.

Example:
@Id
@GeneratedValue (Strategy=generationtype.auto)

Mapping Special types:
· Mapping blobs and CLOB types (@Lob)
# blobs and CLOB types can be labeled with the @lob property (text Type field can also be labeled with this property);
# Clob (Character Large Object) type is a long string type, mapped to a type in the entity can be char[], charater[], or string type;
# Blob (Binary Large Object) type is a byte type, which is mapped to a type in the entity can be byte[], byte[], or a class that implements the serializable interface;
# Because the above two types of data generally occupy large memory, so lazy loading is usually used;

· Mapping time (temporal) type (@Temporal)
# time and date type, need to use @temporal to mark;
# Temporaltype Enumeration type is defined as: DATE, time, TIMESTAMP; default is Temporaltype.timestamp;

· Map enumeration (enumerated) type
# By @enumerated class callout enumeration type;
# You need to be aware when using @enumerated:
An enumeration type has two properties: Name and value. Defined by Enumtype: ORDINAL, STRING
A ordinal that represents the persisted value of the enumerated type, and a string that represents the name of the persisted enumeration type;
# Ordinal types are recommended for persisting enumeration types;
# The defined position of the enum type (inside or outside the entity) depends on the case.

· Mapping non-persisted types (@Transient)
# If there is an attribute in the entity or the Getter method does not need to be persisted, it needs to be annotated with @transient.

Source: http://blog.sina.com.cn/s/blog_49fd52cf0100rzjn.html

jpa--mapping persisted objects (Entity)

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.