What is hibernate?

Source: Internet
Author: User

Main Features











Hibernate is an Object relational mapping solution and a persistent management solution or persistence layer. Such an explanation may not make a person who learns hibernate understand.













You might imagine that your application has a lot of functionality (business logic), and you want to save the data in a database. When all of your business logic is implemented with Java objects, the database table is not an object.













Hibernate provides a workaround for mapping database tables to Java class. It replicates database data to object and also supports storing object data to the database. In this way, you can make object convert one or more database tables.













Saving data to a storage device is called persistence. Copying database table data to object and saving data from object into a database table is called Object Relational mapping.













Why use the Object relational mapping?























Better system Architecture























When you have all the processing and database access written into the page, so that your program has a lot of shortcomings.













It is difficult to reuse. You write repetitive code in a number of places. Makes it hard to find all the changes you need to make.













When you divide a page into parts such as business logic and persistent logic, you can easily be more part of it without affecting other parts.













Reduce the time to write standard database operations























Many database queries are simple "insert,update,delete" statements. There is absolutely no need to develop these tedious statements. Hibernate help you save time.













Loading data from a database to a class is similar to the following code

Query query = session.createquery ("Select B from Bug as B");











for (Iterator iter = Query.iterate (); Iter.hasnext ();) {











Bugs.add ((Bug) Iter.next ());











}











return bugs;













Saving the bug class is similar to













Session.update (bug);













It's hard to implement some advanced features























The caching scheme provided by Hibernate (Caching solutions), transaction processing (transactions) and other functions are not easy to implement. There is absolutely no reason to develop something that already exists, you just need to use hibernate to achieve the above function.













How does hibernate work?











My opinion is that it is much friendlier than entity beans (entity beans), and hibernate only starts with some simple Java classes (Pojo=plain old Java Objects).













To use hibernate you first write a simple Java class:

public class Bug











Implements Serializable











{











private int hashvalue = 0;











Private Java.lang.Integer ID;











Private java.lang.String title;











Public Bug ()











{











}











Public Bug (Java.lang.Integer FID)











{











This.setid (FID);











}











Public Java.lang.Integer getId () {











return ID;











}











public void SetId (Java.lang.Integer id) {











This.id = ID;











}











Public java.lang.String GetTitle () {











return title;











}











public void Settitle (java.lang.String title) {











This.title = title;











}











.............













Then create a mapping file. The mapping file describes how Hibernate maps the fields of a class to a database field.












<class name= "Bugs" table= "Bugs" >











<id name= "id" column= "FID" type= "Java.lang.Integer" >











<generator class= "Sequence" >











<param name= "sequence" >public.bug_fid_seq</param>











</generator>











</id>











<property name= "title" column= "Ftitle" type= "java.lang.String"/>











</class>


































You can now use the Hibernate class.

For example, create a new database entity:

try {











Bug bug = new Bug ();











Bug.settitle ("Title");











BUG.SETTUSERFK (Tuser);











Transaction tx = Session.begintransaction ();











Session.save (bug);











Tx.commit ();











catch (Hibernateexception e) {











E.printstacktrace ();











}























The description file that you need to create can be automatically generated with some tools, such as MyEclipse. MyEclipse provides the ability to create classes and create a description file directly from a database table.













Hibernate includes the following tools:

L Generate Java classes from a description file

L Generate a description from an existing datasheet

L Generate database tables from the description file


























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.