Hibernate study notes (b)

Hibernate Overview:

What is Hibernate: a framework ORM persistence layer
what ORM:
ORM: object-relational mapping, refers to establish a mapping relationship of a java object relational database table to the operation target can be completed related to the operation of the database

Why learn Hibernate
simplify the development of JDBC
performance well

Hibernate's API
the configuration: load profile
SessionFactory: internal connection pool maintenance. C3p0 connection pool configuration, custom tools.

Session:
maintaining a cache, database interaction and bridges.
Commonly used methods:
the Save (Object obj)
GET () / the Load ()
Update ()
the Delete ()
saveOrUpdate ()

Transaction:
commit();
rollback();


##### persistent class written rules

Overview of the class of durable

[] What is persistence? The memory of a process object persistence to the database. Hibernate persistence framework is used to frame

[] What is the persistent class? The establishment of a java object mapping relationship with a database table, then this class in Hibernate persistent class is called
[] persistent class = java + class mapping file


persistent class written rules

[] Provides a free parameter of persistent class constructor Hibernate underlayer requires reflection generate instances

[] attribute requires private, that provides a public get and set function to the private attribute method Hibernate acquired, the value set object

[] persistent class provides a unique identifier OID of a database key corresponding to whether the java through the object address to distinguish the same object in the database is determined by the primary key is the same record,
in Hiberbate by attribute distinguishing persistent class OID whether the same object

[] persistent class attributes to make use of the package because the type of basic data types like default is 0, then 0 will be a lot of ambiguity. Packaging Type Default is null

[] persistent classes do not use the final modified


##### primary key generation strategy

 

 

 

###### three states persistent classes

 


###### Hibernate's cache

 

 

// distinguish three states 
    public  void the demo1 () { 
            the Session the session = HibernateUtils.GetSession ();
            // manual opening transaction 
           the Transaction Transaction = session.beginTransaction (); 
           HibernateDemo1 hibernateDemo1 = new new HibernateDemo1 (); // transient state Object: No unique identification OID, not session management 
           hibernateDemo1.setCust_name ( " XXXX " ); 
            were saved using Session.save (hibernateDemo1); // persistent state objects: a unique identifier OID, managed session 
           transaction.commit (); // submitted event 
           session .close (); //Session close 
          
           // Session.clear (); // Clear All
            // Session.evict (hibernateDemo1); // empty object. This is a cache management process 
           
           // detached state of the object: the object has a unique identity OID, Session management is not 
          the System. OUT .println ( " Customer Name: " + hibernateDemo1.getCust_name ()); 
    }

 

 


Looking back on affairs #####

[] Transaction: A transaction is a set of operations on the logical form of respective logical units set of operations either all succeed or all fail
[] transaction: A transaction is a set of operations on the logic composition set of operations each logical unit either all succeed or all fail

#### transaction characteristics:

[] Atomicity: a transaction on behalf of indivisible

[consistency]: before and after the transactions executed on behalf of, the integrity of the data consistent

[] Isolation: one transaction represents the process of execution, should not be subject to interference from other transactions

[] Persistence: after the completion of transactions executed on behalf of, the data persistence to the database

#### If you do not consider the problem of isolation, will lead to the following security issues:

[] Read issue

# Dirty read: A transaction reads data from another uncommitted transaction

# non-repeatable read: A transaction update data read another transaction has been submitted, leading to inconsistent results of a previous transaction multiple queries

# imaginary read: a transaction insert data read another transaction has been submitted, leading to the results of a previous transaction multiple queries inconsistent

[] Written questions (to understand)

# loss caused by two types of update

[] to solve the reading problem

# Set the transaction isolation level

#Read uncommitted above all problems will occur

#Read committed to resolve dirty reads, but non-repeatable reads and phantom reads can occur

# Repeatable read resolved dirty reads and non-repeatable reads, but phantom reads can occur

#Serializable solve all problems reading

under normal circumstances, the database is often used second or third way isolation, the default is to use the second oracle, and The third is to use mysql

Related issues regarding the configuration of isolation in Hibernate - photo

Setting the isolation level <property name = "hibernate.connection.isolation"> 4 </ property>

 

 

#### sevice affairs:

Other ##### Hibernate API

【】Query

public  void QueryTest () { 
          the Session the session = HibernateUtils.GetSession ();
           // manual opening transaction 
          the Transaction Transaction = session.beginTransaction (); 
          
          // get the Query interface by the Session 
          String HQL = " from HibernateDemo1 " ; // query in HIbernateDemo1 the correspondence table may be provided WHERE 
          of org.hibernate.Query Query = session.createQuery (HQL);
           // set the condition
           // query.setParameter (0, ". 1");
           // set page 
          query.setFirstResult ( 0 ); // initial data
          query.setMaxResults ( 2 ); // page number display 
          List <HibernateDemo1> List = Query.list (); 
          transaction.commit (); // Submit event 
          Session.close (); // session close 
    }

 

 

【】Criteria

 

public  void CriteriaTest () { 
          the Session the session = HibernateUtils.GetSession ();
           // manual opening transaction 
          the Transaction Transaction = session.beginTransaction (); 
          
          // objects Criteria obtained by the session 
         / * Criteria Criteria = session.createCriteria (HibernateDemo1.class) ; 
          List <HibernateDemo1> = criteria.list List (); // get the data of all the information table * / 
          
          // condition inquiry 
          Criteria Criteria = session.createCriteria (HibernateDemo1. class ); 
          criteria.add (Restrictions.like ( " CUST_NAME " , " Song% " ));
          List <HibernateDemo1> = criteria.list List (); // get the information query 
          
          // set page 
          criteria.setFirstResult ( 0 ); 
          criteria.setMaxResults ( 3 ); 
          
          transaction.commit (); // submitted event 
          session. Close (); // session close 
    }

 

 

【】SQLQuery

 

 

 

 Information categories:

public class HibernateDemo1 {
    
    private long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_phone;
    private String cust_mobile;
    public long getCust_id() {
        return cust_id;
    }
    public void setCust_id(long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    @Override
    public String toString() {
        return "HibernateDemo1 [cust_id=" + cust_id + ", cust_industry="
                + cust_industry + ", cust_level=" + cust_level
                + ", cust_mobile=" + cust_mobile + ", cust_name=" + cust_name
                + ", cust_phone=" + cust_phone + ", cust_source=" + cust_source
                + "]";
    }
    
    
}

 

 

Tools:

 

public  class HibernateUtils {
     // main reuse this package is to place tool class
     // The tools are tools of Hibernate 
    
    public  static Final the Configuration CFG;
     public  static Final SessionFactory SF; // Create a SessionFactory: similar JDBC connection pool 
    
    static {
         // load Hibernate core profile 
        CFG = new new the configuration () configure ();. 
        SF = cfg.buildSessionFactory (); // here corresponds to create plants 
    }
     // External interface provided 
    public  static the Session GetSession () {
         //SessionFactory obtained by the Session object, similar to the Connection JDBC 
        return sf.openSession (); 
    } 
}

Guess you like

Origin www.cnblogs.com/byczyz/p/11427494.html