ORM Object Relational mapping has become a hot technology

Source: Internet
Author: User
Keywords Object Relational Mappings
Tags basic class default example framework it is java java class

Simple mapping is one of the simplest and most basic of object-relational mapping, so learning simple mapping is the prerequisite of mastering inheritance mapping and relational 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. Based on the full absorption of the existing excellent ORM, especially the Hibernate framework http://www.aliyun.com/zixun/aggregation/8511.html "", Sun has developed a new JPA (Java Persistence API) specification, for the current chaotic clusters of persistent market to bring a standard, a great unity of the persistence of the market momentum. 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 process of making a lot of reference to the Hibernate content, so if a person familiar with the Hibernate, the use of JPA will be proficient and adept, And it feels simpler, mainly thanks to the annotations (annotation) introduced in the JDK5.

Use annotations to introduce the use of JPA below.

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 an entity class that corresponds to a table in a 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 or 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 revisit by Exception introduced by Java EE, the core of which is that the container or vendor provides a default rule, Under this rule, the program works correctly, and 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.

Related Article

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.