Simple mapping
In recent years ORM (Object-relational Mapping, Object-relational mapping, that is, the mapping of entity objects and database tables) technology market is very lively, a variety of persistent framework came into being, the most influential is the Hibernate and TopLink. On the basis of fully absorbing the existing excellent ORM, especially the Hibernate framework design idea, Sun company has developed a new JPA (Java persistence API) specification, which brings a standard to the current chaotic and persistent market, and has a great momentum of unifying the persistent market. JPA is a mapping relationship between an object-relational table by JDK5.0 annotations or XML, and a Run-time entity object is persisted to the database. The leader of the JPA spec group is Hibernate's inventor Gavin KING,JPA the specification of the Hibernate, so if a person who is familiar with the Hibernate is used, JPA will be adept, proficient, and And it feels simpler, mainly thanks to the annotations (annotation) introduced in the JDK5.
The following is an introduction to JPA usage using annotations.
Let's start with a little example of how to map a single Java class to a database.
Listing 1. Employee entity
@Entity public
class Employee implements Serializable {
private static final long serialversionuid = 1L;
@Id
private Long Id;
private String name;
private int age;
Private String Addree;
Getters and Setters
}
If there are no @javax. Persistence.entity and @javax. Persistence.id These two annotations, it is a typical POJO Java class, now add these two annotations, Can be used as an entity class in relation to a table in the database. His corresponding table in the database is:
Figure 1. ER diagram corresponding to Employee table
Mapping rules:
1. Entity classes must be annotated with @javax. persistence.entity;
2. You must use @javax. Persistence.id to annotate a primary key;
3. Entity classes must have a public or protected parameterless constructor, and an entity class can also have other constructors;
4. Entity classes must be top-level classes (top-level Class). An enumeration (enum) or an interface (interface) cannot be annotated as an entity;
5. Entity classes cannot be final and cannot have final-type methods;
6. If an instance of an entity class needs to be called by means of a value (for example, a remote call), the entity class must implement the (implements) Java.io.Serializable interface.
Mapping a POJO Java class to a table in a database is as simple as this thanks to the idea of the Configuration by Exception introduced by Java EE, the core of which is that the container or vendor provides a default rule under which the program is To run correctly, if the developer has special needs and needs to change the default rule, it is an exception (Exception) to the default rule.
As the example above shows: The default mapping rule is that the name of the database table and the corresponding Java class have the same name, and the name of the column in the table is the same as the name of the corresponding field in the Java class.
Now we can change this default rule:
Listing 2. Modifying mapping rules using @Table and @Column annotations
@Entity
@Table (name= "workers") public
class Employee implements Serializable {
private static final long Serialversionuid = 1L;
@Id
@GeneratedValue
private Long Id;
@Column (name= "Emp_name", length=30)
private String name;
@Column (name= "Emp_age", Nullable=false)
private int age;
@Column (name= "emp_address", Nullable=false, Unique=true)
private String addree;
Getters and Setters
}