From getting started to getting started with Hibernate (1), getting started with hibernate
-
Hibernate (orm framework) (open source object link ing Framework)
Hibernate is an open-source object relationship ing framework that encapsulates JDBC very lightweight objects. It establishes ing between POJO and database tables and is a fully automated orm framework, hibernate can automatically generate and execute SQL statements, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.
Orm: Object relational ing, Object ing, used for the Association and ing framework between objects and relational databases.
Demo1: steps for building a pure java project:
Step 1: Create the database table t_user and create the java object USer. java
Step 2: Add a jar package
Step 3: Create a ing File
Step 4: Create the hibernate core configuration file hibernate. cfg. xml
Step 5: Test
The detailed steps are as follows:
Project Structure
Step 1:Create Database Table t_user, create java object USer. java
# Database Table t_user
Create database hibernate_demo CHARSET 'utf8'; create table t_use (id INT (10) primary key AUTO_INCREMENT, uname VARCHAR (20) not null );
//Create a java object User. java
Public class User {private Integer id; private String uname; // getter and setter omit ......}
Step 2:Add jar package
Step 3:Create a ing FileUser. hbm. xml
<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <! -- Package: java object package path -->
Step 4:Hibernate. cfg. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> Step 5: Test
1 package com. jonychen. test; 2 3 import org. hibernate. session; 4 import org. hibernate. sessionFactory; 5 import org. hibernate. transaction; 6 import org. hibernate. boot. registry. standardServiceRegistryBuilder; 7 import org. hibernate. cfg. configuration; 8 import org. hibernate. service. serviceRegistry; 9 10 import com. jonychen. model. user; 11 12 public class HibernateTest {13 14 public static void main (Stri Ng [] args) {15 // TODO Auto-generated method stub16 // 1. Load the Configuration file 17 Configuration cfg = new Configuration (); 18 cfg. configure ("hibernate. cfg. xml "); 19 20 21 // 2. Create sessionFactory 22 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder (). applySettings (cfg. getProperties ()). build (); 23 SessionFactory sessionFactory = cfg. buildSessionFactory (serviceRegistry); 24 25 // 3. Open the new session26 Session = SessionFactory. openSession (); 27 28 try {29 // Transaction 30 Transaction ts = session. beginTransaction (); 31 32 User user = new User (); 33 user. setUname ("jonychen"); 34 // Add data 35 session. save (user); 36 // submit transaction 37 ts. commit (); 38} catch (Exception e) {39 // TODO Auto-generated catch block40 e. printStackTrace (); 41} finally {42 // close connection 43 if (session! = Null) {44 session. close (); 45} 46 if (sessionFactory! = Null) {47 sessionFactory. close (); 48} 49 50} 51} 52}
Add a plug-in Eclipse to develop Hibernate