Introduction to hibernate and first hibernate example.

1. What is Hibernate

  Simply said hibernate is a ORM framework, where ORM stands for Object (Java) - Relational (DB) mapping, 

  Hibernate is developed based on JDBC api, which has greatly reduced the code work when dealing with Databse. 

2. Environment set up and Code example. 

Four steps are concluded as below:

  Step1. Import needed jar files,

  Step 2. set up the "hibernate.cfg.xml" config file.

  Step 3. create the Actor class file. which is the bean we are going to insert into the Database.  

  Step 4. Create the main class

Run output: the data has been successfuly inserted to Database. 

From this example, we could see that Hibernate has helped us reduced greatly on the code lines on dealing with database part

  

Details are described as below for reference

Step1. Import needed jar files, which could be downloaded in www.hibernate.org/downloads

Step 2. set up the "hibernate.cfg.xml" config file. 

  

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
      <!-- Database Connection settings  -->
      <property name ="connection.driver.class">  com.mysql.jdbc.Driver</property>
      <property name = "connection.url"> jdbc:mysql://localhost:3306/sakila</property>
         <property name = "connection.username">root</property>
      <property name="connection.password">3792354</property>
    
    <!-- JDBC Connection pool (use the built-in) -->
     <property name ="connection.pool_size">1</property>
    
    <!-- SQL Dialect -->
    <property name = "dialect">org.hibernate.dialect.MySQLDialect</property>
    
    <!-- Disable the second-level Cache -->
    
    <property name = "cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!-- ECHO all executed SQL to STDOUT -->
    
    <property name ="show_sql" >true</property>
    
    <property name="hbm2ddl.auto">create</property>
    
    <mapping class = "com.yang.bean.Actor"/>
    
    </session-factory>
</hibernate-configuration>

Step 3. create the Actor class file. which is the bean we are going to insert into the Database. 

package com.yang.bean;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;


//tell the hibernate that this class is an entity

//class written to persist or hold data into or from database

@Entity
public class Actor {
	@Id
	private int actor_id;
	private String first_name;
	private String last_name;
	private Date last_update;
	
	public int getActor_id() {
		return actor_id;
	}
	public void setActor_id(int actor_id) {
		this.actor_id = actor_id;
	}
	public String getFirst_name() {
		return first_name;
	}
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
	}
	public String getLast_name() {
		return last_name;
	}
	public void setLast_name(String last_name) {
		this.last_name = last_name;
	}
	public Date getLast_update() {
		return last_update;
	}
	public void setLast_update(Date last_update) {
		this.last_update = last_update;
	}
	
	
	

}

Step 4. Create the main class

package com.yang.bean;


import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {
    
    public static void main(String[] args) {
        
        Date d = new Date();
        Actor a = new Actor();
        
        a.setActor_id(202);
        a.setFirst_name("Yajing");
        a.setLast_name("Hong");
        a.setLast_update(d);
        
         Configuration cfg = new Configuration();
         
         SessionFactory sf = cfg.configure().buildSessionFactory();
         
         Session session = sf.openSession();
         
         session.beginTransaction();
         
         session.save(a);
         
         session.getTransaction().commit();
         
         session.close();
         
         sf.close();
       
    }
    

}

猜你喜欢

转载自www.cnblogs.com/codingyangmao/p/10879022.html