Resolve the persisted classes and mapping files in the Java Hibernate framework _java

Source: Internet
Author: User
Tags generator unique id

Persistence class
the whole concept of hibernate is to take the value from the Java class attribute and will persist to the database table. A mapping file Hibernate help determine how to pull the values from the class and map them to the table and related fields.

Java classes whose objects or instances are stored in a database table are called persistent classes in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the normal Java object (POJO) programming model. There are primary rules for the following persisted classes, but these rules are not required.

A default constructor is required for all of the persisted Java classes.

All classes should contain IDs that are easy to identify hibernate and databases within an object. This property maps to the primary key column of a database table.

All attributes that will be persisted should be declared private and have been defined in the JavaBean style by the getxxx and Setxxx methods.

The key function of Hibernate, the proxy, depends on the implementation of the interface of the persistent class or of a non final, or declared, public method.

All classes that do not extend or implement the EJB framework require some specialized classes and interfaces.

The Pojo name is used to emphasize that a given object is a normal Java object, rather than a special object, better than Enterprise JavaBean.

A simple example of Pojo:
Based on some of the rules mentioned above, we can define a Pojo class as follows:

public class Employee {
  private int id;
  Private String firstName; 
  Private String lastName;  
  private int salary; 

  Public employee () {} public
  employee (string fname, string lname, int salary) {
   this.firstname = fname;
   This.lastname = lname;
   This.salary = salary;
  }
  public int getId () {return
   ID;
  }
  public void setId (int id) {
   this.id = ID;
  }
  Public String Getfirstname () {return
   firstName;
  }
  public void Setfirstname (String first_name) {
   this.firstname = first_name;
  }
  Public String Getlastname () {return
   lastName;
  }
  public void Setlastname (String last_name) {
   this.lastname = last_name;
  }
  public int getsalary () {return
   salary;
  }
  public void setsalary (int salary) {
   this.salary = salary;
  }
}


Mapping files
XML documents for object/relationship mapping are usually defined. This mapping file indicates how one or more classes defined by Hibernate are mapped to database tables.

Although many hibernate users choose to write XML manually, there are tools that can be used to generate mapping documents. Includes users for advanced hibernate, such as Xdoclet,middlegen and Andromda.

Let's consider that our objects will adhere to the Pojo class defined earlier in the table defined in the next section.

public class Employee {
  private int id;
  Private String firstName; 
  Private String lastName;  
  private int salary; 

  Public employee () {} public
  employee (string fname, string lname, int salary) {
   this.firstname = fname;
   This.lastname = lname;
   This.salary = salary;
  }
  public int getId () {return
   ID;
  }
  public void setId (int id) {
   this.id = ID;
  }
  Public String Getfirstname () {return
   firstName;
  }
  public void Setfirstname (String first_name) {
   this.firstname = first_name;
  }
  Public String Getlastname () {return
   lastName;
  }
  public void Setlastname (String last_name) {
   this.lastname = last_name;
  }
  public int getsalary () {return
   salary;
  }
  public void setsalary (int salary) {
   this.salary = salary;
  }
}

There will be a table that corresponds to each of the objects you are willing to provide persistence. Consider the above purpose to store and retrieve the following RDBMS tables:

CREATE TABLE EMPLOYEE (
  ID INT not NULL auto_increment,
  first_name VARCHAR () default null,
  last_name varcha R () default NULL,
  salary   INT default null,
  PRIMARY KEY (ID)
);

Based on the above two entities, we can define a mapping file that indicates how one or more classes defined by Hibernate are mapped to the following database tables.

<?xml version= "1.0" encoding= "Utf-8"?> <!
DOCTYPE hibernate-mapping public 
 "-//hibernate/hibernate mapping dtd//en"
 "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "> 

 
 

The format in the saved mapping file: <classname>.hbm.xml. We save the file Employee.hbm.xml in the mapping file. Take a look at the small details of the mapping elements used in the mapping file:

The mapping document is an XML document that has the

The <class> element is used to define database tables from a Java class-specific mapping. The Java class name specifies that the name property of the class element is used and the Table property database table name is used.

The <meta> element is an optional element that you can use to create a description of the class.

The <id> element maps the unique ID attribute in the class to the primary key of the database table. The Name property of the ID element refers to the columns of the property's class and column property that are in the database table. The Type property holds the Hibernate mapping type, which is converted from Java to SQL data type.

The primary key value that the <generator> element within the ID element is used to automatically generate. Setting the class attribute of the build element to native lets hibernate pick up algorithms in either identity,sequence or Hilo to create a primary key based on the support capabilities of the underlying database.

The <property> element is used to map the properties of a Java class to columns in a database table. The Name property of an element refers to the columns of the property's class and column property that are in the database table. The Type property holds the Hibernate mapping type, which is converted from Java to SQL data type.

Also, this will be used in the mapping file, and then try to cover as many other attributes and available elements as possible for other Hibernate related topics.

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.