The second SSH Hibernate framework

1.1 persistent class writing rules
         1.1.1 What is the persistent class? 
            Persistent classes: Table established entity class mappings, you can call a persistent class. 
            Persistent class = Java class + mapping file.
        
         1.1 .2 persistent class writing rules 
            ( 1 ): provided no argument constructor 
            ( 2 ): class members are private private 
            ( . 3): providing get / private attributes of the set method 
            ( 4 ): to make use of properties packaging types: different from the default, default values packaging null, the default data type base 0.5 
            ( 5 ): do not use persistent class for final modifications: use final modification, the delay loading failure, immediately becomes loaded. 
            ( 6 ) : class attributes required to provide identification (OID), mapping relationship between the primary key in the table: Hibernate determines whether the object is in accordance with the same ID attribute.
            
            Persistent class is a special javabean 
        
            identifier attribute Hibernate entity classes: Object Identifie referred OID attribute 
                    is the configuration file corresponding to the entity class attributes of the primary key. 
                    
                    Effect: Let Hibernate to distinguish whether the object is the same. 
                            If multiple objects identity attribute the same value, hibernate you will think the same object, stored data will complain. 
                            
            OID properties are handed over to hibernate to maintain: 
                the benefits: not to repeat the same with the OID property. 
                    do not have to write code to ensure non-empty unique.
            
     1.2 Hibernate primary key generation strategy:
         1.2.1 distinguish between natural primary key and a surrogate primary key 
            natural primary key: create table when using the entity's own property as the table's primary key. 
                For example: create a person table, you can use the ID number autogenous (only unrepeatable) as the primary key. 
                
            surrogate primary key: when creating the table, not used by the entity in its own attributes as the primary key, create a field that is not associated as the primary key. 
                For example: Create a person table, without the use of autogenous iD number (unique unrepeatable) as the primary key, with another to create an id The primary key.
                
            Try to use a surrogate primary key. If you use natural primary key when the primary key required to participate in the business logic, there may need to change a lot of design and code.
            
         1.2.2 primary key generation strategy for 
            the primary key should not be maintained by the users themselves, should be unified management, Hibernate it provides a way primary key unified management: primary key generation strategy. 
            
            INCREMENT: automatic growth is the hibernate provide an automatic growth is not automatic increase in the database (for Short,. int , Long type of the primary key) 
                            the first to use select max (cust_id) from cust_customer; this maximum value + 1 as the primary key in the next record 
                            for concurrent access to the database is not applicable. 
                            
            Identity: automatic growth for Short, int , Long types of master key used for database automatically growth mechanism not all databases support automatic growth, such as Oracle does not automatically increase. 
            
            sequence: sequence for Short, int ., Long types of primary keys, using the sequence is accomplished generated primary key must be a database .Oracle support sequence mode automatically growth. 
                
            native        : local policy, based on the underlying database is different from Automatically selected using identity or sequence.
            
            uuid: random string for string type of the primary key. 
            
            Assigned: the Hibernate not maintain primary key, the developer needs to manually set the primary key. 
            
            Summary: after If it is for a numeric type, selecting native, if type varchar, can choose uuid 
                but can not hibernate maintenance, manually set their own unique non-null (assigned) in the program
                            
     1.3 persistent class three states
         1.3.1 Hibernate persistent classes in the three states. 
            Hibernate persistence framework for better management class, a persistent class into three states. 
                instantaneous state: no persistent identity the OID, the operation is not session management 
                in a consistent state: the OID has a persistent identity, session operation has been managed. 
                detached state (off state): there are persistent identity OID, the operation is not managed session.
            
         1.3.2 distinguish three states persistent class
             public  void the demo1 () { 
                the session session = HibernateUtils.openSession ();
                Transaction the Transaction = session.beginTransaction (); 
                
                // transient state: no persistent identity OID, session management is not 
                the Customer = Customer new new the Customer (); 
                customer.setCust_name ( "HUANG Tian-hua" ); 
                
                // in a consistent state: YES persistence identity OID, session management has been 
                the Serializable ID = Session.save (Customer); 
                System.out.println (ID); 
                
                transaction.commit (); 
                Session.close (); 
                
                // detached state: YES persistent identity OID, not session management. 
                System.out.println (customer.getCust_name ()); 
                
            }
            
         1.3.3 converting three states (interview questions) 
            transient state: no persistent identity OID, not session management 
                obtained:
                Customer the Customer = new new the Customer (); 
                state transition: 
                    Instantaneous -> Persistence: save, saveOrUpdate 
                    instantaneous -> Hosting: customer.setCust_id (1L ); 
                    
            persistent state: YES persistent identity OID, session management has been 
                obtained: As long as relates directly to the query methods are persistent state objects. 
                the Customer Customer = Session.get (the Customer. class , 1L ); 
                the state transition 
                lasting -> Hosting: close 
                persistent -> instantaneous: delete 
             
            detached state: YES persistent identity OID, session management is not
                obtain 
                the Customer Customer = new new the Customer (); 
                customer.setCust_id ( 1L ); 
                state transitions: 
                Managed -> Persistence: save, update, saveOrUpdate 
                hosted -> Instantaneous: customer.setCust_id ( null ) 
                
            in a consistent state: As far as the query returns persistent objects are in a consistent state to state class. 
                features: If the persistent state object is finished modifying the properties necessary to perform the update operation, the database will automatically update data
            
         1.3.4 persistent state of the database can be automatically updated 
            
            public  void demo2 () { 
                the Session the session = HibernateUtils.openSession (); 
                the Transaction Transaction =session.beginTransaction ();
                 // get persistent state objects 
                the Customer the Customer = Session.get (the Customer. class , 1L ); 
                customer.setCust_name ( "Wang Baoqiang" );
                 // Session.update (the Customer); // no manual update method is called, but also can update the database 
                transaction.commit (); 
                Session.close (); 
            }

     1.4 cache of Hibernate
         1.4.1 Overview cache
             1.4.2 an overview of Hibernate: 
            Hibernate frame itself, the performance is not very good, but Hibernate provides many performance optimization manually, for example, cache 
            
            Hibernate framework provides two levels of cache:
                 
            cache: Session-level cache is called a cache because the life cycle in line with the built-in session can not be uninstalled (by the Session in a series... Java collections constituted)
             
            The secondary cache: called SessionFactory . level cache to cache configuration required plug-ins by default does not open .ehche. == Redis 
            
            PS:. in addition to delete, add and change search database kick dealings, will be to set aside a cache 
                but as long as the session is closed, all cache data cleared. 
                
            the reason: there is a cache of 

            principle: 
                Session.get (. the Customer class , 1L)   // to the database query on a cache after the investigation is over 
                                        a cache 
                                            store 
                                                put a 
                                            snapshot zone 
                                                backup copy

                ct.setCust_name ( "rose1234");    // to a buffer memory area modifies the content, but does not modify Snapshot district 
                                        level cache 
                                            memory area 
                                                modifies the 
                                            snapshot area 
                                                before 


                tx.commit ();       // go to a cache, the data storage area and snapshot area are out to compare 
                            the same: do nothing 
                            inconsistent: do data sent automatically update statement to update data
                
             1.4.3 prove the existence of a cache:
                 public  void demo1 () { 
                    the Session the session = HibernateUtils.ipenSession (); 
                    Thransaction Transaction= session.beginTransaction();
                    
                    Customer customer = new Customer();
                    customer.setCust_name("好强");
                    
                    Serializable id = session.save(customer);
                    session.get(Customer.classmid);//没有发烧SQL语句.
                    
                    Customer customer1 = session.get(Customer.class,1L);//发送SQL语句去查询
                    System.out.println(customer1);
                    Customer customer2 = session.get(Customer.class, 1L); // not send SQL statements 
                    System.out.println (Customer2); 
                    
                    transaction.commit (); 
                    Session.close (); 
                    
                } 

        which have a buffer cache, and the snapshot area, later when the data is reset , update the cache area, were submitted to commit, will contrast are the same inside a buffer zone and a buffer zone snapshots, 
        if consistent, no database updates; if not, the area restored to the snapshot, and update the database
    
         1.4.5 empty the cache of data: method of managing a cache of 
            clear, relevant way to evict, close cache management. 
            the Clear cache can be emptied of all data. 
            data objects evict one can empty the cache .
    
     1.5 the session binding (ThreadLocal)
         1.5.1 set Hibernate with the current thread-bound session using
         <! - setting session with the current thread binding ->
        <Property name = "hibernate.current_session_context_class"> the Thread </ Property> 
        
    1.6      other API Hibernate of
        in hibernate, the query batch data way: 
                Query: If the way to do this is to use the inquiry, also known as HQL query. 
                Criteria: If is with this way of doing the query, the query has been referred to as QBC.
        
         1.6.1 query: HQL query 
            receives a HQL query: HQL - . Hibernate query language Hibernate query language, SQL language syntax is very similar to the one object-oriented language .Query interfaces, HQL query is a query object-oriented objects.. 
                @Test 
            / ** 
             * query Interface 
             * HQL query is a query object-oriented objects. 
             * / 
            public  void the demo1 () { 
                the Session the session = HibernateUtils.getCurrentSession (); 
                the Transaction Transaction = session.beginTransaction (); 
                
                // query all customer information: 
                / *Query query = session.createQuery("from Customer");
                List<Customer> list = query.list();
                
                for (Customer customer : list) {
                    System.out.println(customer);
                }*/
                
                // 条件查询
                /*Query query = session.createQuery("from Customer where cust_name like ?");
                // 设置参数
                query.setParameter(0, "郝%");
                List<Customer> list = query.list();
                for (Customer customer : list) {
                    System.out.println (Customer); 
                } * / 
                
                // paging query: 
                Query Query = session.createQuery ( "from the Customer" );
                 // where to start 
                query.setFirstResult (. 3 );
                 // page number of records 
                query.setMaxResults (. 3 ); 
                
                List <the Customer> List = Query.list ();
                 for (Customer the Customer: List) { 
                    System.out.println (Customer); 
                } 
                transaction.commit (); 
            }
            
         1.6.2 Criteria: the QBC Inquire 
            the QBC - query By Criteria query conditions more object-oriented query language.. 
            @Test 
            / ** 
             * the QBC query: query By Criteria 
             * / 
            public  void demo2 () { 
                the Session the session = HibernateUtils.getCurrentSession (); 
                the Transaction Transaction = session.beginTransaction (); 
                
                // query all: 
            / *     Criteria Criteria = session.createCriteria (Customer.class); 
                List <the Customer> = criteria.list List (); 
                for (Customer the Customer: List) { 
                    System.out.println (Customer) ; 
                } * / 
                
                // 条件查询
                /**
                 * ==     eq
                 * >        gt                 
        * >=    ge        
                 * <        lt        
                 * <=    le        
                 * <>    ne        
                 * in   in
                 * like like
                 * 
                 */


                /*Criteria criteria = session.createCriteria(Customer.class);
                // criteria.add(Restrictions.eq("cust_name", "郝天一"));
                criteria.add(Restrictions.like("cust_name", "郝%"));
                List<Customer> list = criteria.list();
                for (Customer customer : list) {
                    System.out.println(customer);
                }*/
                
                // 分页查询
                Criteria criteria = session.createCriteria(Customer.class);
                criteria.setFirstResult(3);
                criteria.setMaxResults(3);
                List<Customer> list = criteria.list();
                for (Customer customer : list) {
                    System.out.println(customer);
                }
                
                transaction.commit();
            }

 

Guess you like

Origin www.cnblogs.com/haizai/p/11484960.html