For Java Development Advanced, Hibernate is one of the three main Java framework, enough to see its importance, then how much do you know about hibernate? From today on I will lead you together to explore hibernate knowledge, hibernate for our development of Mobile Application Association database is very convenient, hibernate for the database operation is very convenient, save a lot of previous development inconvenience.
Hibernate is an open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases at will using object programming thinking. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence.
There are 6 core and class interfaces for hibernate: Session, Sessionfactory, Transaction, Query, criteria, and configuration. These 6 core and class interfaces will be used in any development. Through these interfaces, you can not only access persistent objects, but also enable transaction control. For details see:
Let's start with a brief introduction to the content of this film:
First step: Install the Java EE Development environment
JAVA EE Development also use Ecipse, here I provide a: http://www.newasp.net/soft/71687.html=, interested children shoes can go to download, the installation process is super simple, we think about it.
Step Two: Create a new project
method is consistent with our creation of Web projects, right-click on the left, select New project, project name can be set by themselves, as long as the Java naming convention.
Step three: Import a jar package that we may use
The small part here is not completely done, the follow-up will be updated, please look forward to!
Fourth Step: Configure Hibernate environment variables
A, hibernate.properties file:
######################### Query Language ########################### define query Language constants/function Nameshibernate.query.substitutions Yes'Y', no'N'## SelectThe Classic query Parser#hibernate.query.factory_class org.hibernate.hql.Internal. Classic. classicquerytranslatorfactory#################### Platforms ###################### MySQL#hibernate.dialect Org.hibernate.dialect.MySQLDialecthibernate.dialect Org.hibernate.dialect.mysqlinnodbdialect#hibernate.dialect Org.hibernate.dialect.MySQLMyISAMDialecthibernate.connection.driver_class Com.mysql.jdbc.Driverhibernate.connection.url Jdbc:mysql://127.0.0.1:3306/testhibernate.connection.username roothibernate.connection.password root## MS SQL server#hibernate.dialect Org.hibernate.dialect.sqlserverdialect#hibernate.connection.username Sa#hibernate.connection.password sa######## ############################ Hibernate Connection Pool #################################### Hibernate.connection.pool_size1############################## c3p0 Connection pool############################# #hibernate. C3p0.max_size2hibernate.c3p0.min_size2Hibernate.c3p0.timeout thehibernate.c3p0.max_statements -Hibernate.c3p0.idle_test_period thehibernate.c3p0.acquire_increment2hibernate.c3p0.validatefalse################################# Miscellaneous Settings ################################### Print all generated SQL to the Console#hibernate.show_sqltrue# # Format SQLinchLog and Consolehibernate.format_sqltrue# # Add comments to the generated sql#hibernate.use_sql_commentstrue# # Generate Statistics#hibernate.generate_statisticstrue# # Auto Schema Export#hibernate.hbm2ddl.auto Create-drop#hibernate.hbm2ddl.auto createhibernate.hbm2ddl.auto Update#hibernate.hbm2ddl.auto Validate
Note: notefor c3p0 Connection pool see: Http://baike.baidu.com/link?url=37RuZ_7cnA1F4S6iCZ3PP6f63H0SRBcUN3j1QSJ6EDmT1k_ NFSQKP9PZJNVB7GYKIDNYROOL8MAQ_K9OLCQQBK
hibernate.format_sql true annotations are described in: http://blog.csdn.net/zhengqiqiqinqin/article/details/8450875
Hibernate.hbm2ddl.auto Update notes See: Http://www.linuxidc.com/Linux/2011-12/49206.htm
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect: The MySQL database version statement we installed
Fifth step: Create our User object class:
Hibernate data is manipulated in the form of objects, so we need to create an object class for our subsequent operations.
Public classUser {Private intID; PrivateString name; PrivateString Pass; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getpass () {returnPass; } Public voidSetPass (String pass) { This. pass =Pass; } }
Sixth step: Create User.hbm.xml:
With the object, we need to use this file to send the object information to our database, for our next design to pave the groundwork.
<?xml version= "1.0"? ><! DOCTYPE hibernate-Mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" Package = "Po "> <class name=" User "> <id name=" id "column=" c_id "> class=" Native " ></generator><!--Setting the primary key-- </id> <property name= "name" column= "C_name" length= "20" /> <property name= "pass" column= c_pass "length="/> </ class></ Hibernate-mapping>
This file needs to be placed in the same folder as our corresponding class, and the file's filename suffix must not be changed.
Seventh Step: Configure the Hibernate.cfg.xml file:
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Www.hibernate.org/dtd/hibernate-configuration-3.0.dtd ">true</property> < Mapping resource= "Po/user.hbm.xml"/> </session-factory>
Here our development before the configuration work has been completed, for his use, I will be in the next article for you to introduce.
Environment configuration for the JAVA EE Hibernate Foundation