Build a hibernate Environment

Source: Internet
Author: User

Build a hibernate Environment

Persistence concept:

Persistence means to synchronously save data (such as objects in memory) to a database or some storage devices (such as disk files and XML data files.
In the hierarchical architecture of software, the persistence layer is the logic layer that deals with databases.

JDBC-problems:

Tedious code:

Example of inserting an object

public void addAccount(final Account account) throws DAOException,AccountAlreadyExistException{final Connection conn=getConnection();PreparedStatement pstmt=conn.prepareStatement(“insert into account values(?,?,?)”);pstmt.setString(1.account.getUserName));pstmt.setInt(2,account.getPassWord));pstmt.setString(3.account.getSex);pstmt.execute();conn.close();

If the account table has hundreds of fields, the pstmt. setXX () statement needs to be written hundreds of times, which is obviously inappropriate.


Non-object-oriented

Database operations are not object-oriented, but relational databases


ORM Introduction

ORM (object relational mapping) is the ing between objects and relations.

It converts operations directly performed on a table into direct operations on attributes and methods of persistence classes.

ORM is the persistent layer in the layered system.

The ORM technology can greatly improve the development efficiency and time, and the Development quality is more easily guaranteed.


Hibernate core interface

Session, SessionFactory, Transaction, Query, and Configuration


Session:

Session is responsible for executing the CRUD operation on the Persistent Object (the CRUD task is to complete the communication with the database, including many common SQL statements.

However, it should be noted that the Session object is non-thread-safe. At the same time, the Hibernate session is different from the HttpSession In the JSP application.

Here, when we use the term session, it actually refers to the session in Hibernate, and later we will call the httsemi sion object a user session.


SessionFactory

SessionFactroy initializes Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects.

The factory model is used here. Note that SessionFactory is not lightweight, because in general,

A project usually requires only one SessionFactory. When you need to operate multiple databases, you can specify one SessionFactory for each database.


Configuration

Configuration is responsible for configuring and starting Hibernate to create the SessionFactory object.

During Hibernate startup, the Configuration class instance first locates the ing document location, reads the Configuration, and then creates the SessionFactory object.


Transaction

Transaction is responsible for Transaction-related operations.

It is optional, and developers can also design and write their own underlying transaction processing code.


Query:

Query is responsible for executing various database queries. It can be expressed in HQL or SQL statements.


Environment setup:

Dependency package:

Pom. xml

   
    
     
4.0.0
    
    
     
Com. demo
    My-hibernate
    
     
0.0.1-SNAPSHOT
    
    
     
      
       
Org. hibernate
      Hibernate-core
      
       
3.6.10.Final
      
     
     
      
       
Org. hibernate. javax. persistence
      Hibernate-jpa-2.0-api
      
       
1.0.0.Final
      
     
     
      
       
Log4j
      Log4j
      
       
1.2.17
      
     
     
      
       
Org. slf4j
      Slf4j-log4j12
      
       
1.7.7
      
     
     
      
       
Org. javassist
      Javassist
      
       
3.18.2-GA
      
     
     
      
       
Junit
      Junit
      
       
4.10
      
     
     
      
      
       
Oracle
      Ojdbc
      
       
14
      
     
    
   

Note: ojdbc14.jar must be manually added to the maven local repository to customize coordinates.

Hibernate. cfg. xml:

  
   
    
    
     
True
    
    
     
Oracle. jdbc. driver. OracleDriver
    
    
     
Jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl
    
    
     
Diankun
    
    
     
Diankun
    
    
    
     
Org. hibernate. dialect. Oracle10gDialect
    
    
    
     
Create
    
    
    
   
  

Student:

package com.demo.model;public class Student {private int studentId ;private String studentName ;private int age;public int getStudentId() {return studentId;}public void setStudentId(int studentId) {this.studentId = studentId;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}

Student. hbm. xml

  
      
           
                
             
            
            
        
   
  

Test

package com.demo;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;public class StudentTest {SessionFactory sessionFactory ;@Beforepublic void init(){sessionFactory=new Configuration().configure().buildSessionFactory();}@Testpublic void createTest(){}}


Run mvn test and the database automatically generates the table structure.





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.