Hibernate framework to generate learning strategies and persistence

Primary key generation strategy

1. Primary Key Categories

Natural primary key

Itself is a primary key field of the table, the properties of a particular entity, unique properties of the object itself. To say we have a person ID number, name, sex and other attributes, ID number is only possible as a natural primary key.

Surrogate primary key

Primary key table itself is not a required field , we use an automatic system growth, or the like using a random string method additionally provided in the main key. Development which we generally use a surrogate primary key, so it can help us to modify user fields in the future. For example: For example, we use natural primary key (student number), if we enter student data when two students will learn numbers recorded backwards, so he wanted us to go to modify student information will be very troublesome, because we are a table two identical primary keys can not appear.

2. The primary key generation strategy

In the process of using a surrogate primary key, try to do auto-generated primary key can not allow the user to manually set the primary key. General database to automatically grow, let the program generates a unique identifier.

In hibernate them, in order to reduce the preparation process, offers a variety of internal primary key generation strategy:

  • increment

    Strategy : automatic increase in long, short, int
    Note : Using a single thread, do not use multiple threads, because the current way is to find the largest id number table and then on the basis of +1 for the next record assignment. So when two threads simultaneously id field among the database queries when the two threads is the same one found in the two thread id inserted record id necessarily the same error!

  • identity
    strategies : automatic growth
    principle: the use of a database underlying growth strategies for automatic database growth mechanism. Mysql automatic growth, Oracle is not automatically increase.

  • sequence
    strategies : automatic growth
    principle : by way of the sequence, the sequence must have to support Oracle support sequences, Mysql does not support serialization

  • uuid
    policy : For the primary key type string
    principle : using randomly generated strings hibernate primary key

  • native
    policy : local policy
    principle : to automatically switch identity and sequence

  • assigned
    strategy : hibernate will not help you manage the primary key
    principle : to manually call or come by the program to generate the primary key

In the development of which we generally use native, uuid both.

Endurance of

1. What is the persistent persistent classes &

What is persistence

A memory object persistence in the process (stored) in the database. hibernate persistence framework is a framework for persistent object will automatically update the data, as long as the state of the object to be persistent, do not call the update will automatically update the data

What is the persistent class

A Java class mapping relationship established with the database. Java class mapping file +

2. persistent class to write rules

  1. Persistent class for a constructor with no arguments
    underlying objects will be created by reflection if no constructor with no arguments, the object reflector can be created
  2. Internal private fields to provide get and set methods
    , then do not provide. hibernate will not be able to get the value of the object
  3. Object persistence class provides a OID of the database table among the primary key corresponds to
    the Java distinguished by an address object is the same object
    database using a primary key to distinguish whether it is the same record
    in Hibernate distinguished by persistent class OID attribute is It is the same object
  4. Persistent class attributes make use of package type
    package type default value NULL
    basic type digital default value
  5. Persistent classes do not use the final modified
    to load a relationship with the delay, delay means Hibernate load is optimized return a proxy object. Dynamic proxies, a lower layer of bytecode enhancement technology, this class inherits proxy. If a final, can not be inherited, can not be inherited, can not create proxy object delay load is invalid

3. division of persistent classes

Hibernate persistence framework by persistent class ORM operation is completed, in order to better manage Hibernate persistent class, a persistent class object into three states:

State name status distinguish
Instantaneous state There is no single OID
are not session management
Just a new object has not been set id, it has not been managed by session
Persistent state We have a unique OID
there is session management
Id have
to call session method, the object to the session, the session was only management
Of free / hosting state / off-line state We have a unique OID
is not session management
When the session is closed off when the close
object processing free form

State transition diagram:

Cache

What is the cache, it means an optimized way, the data stored in the cache (memory) and thus do not have to be a data source (database) to fetch data and improve the efficiency of our inquiry. And there hibernate partition cache and level two cache, wherein the cache is a session-level, consistent with the Session life cycle, and is a collection of a series of Java Session configuration. A built-in cache is not uninstalled . Secondary cache is SessionFactory-level cache, to make their own configuration is enabled by default, in the enterprise generally do not, and now redis

1. The characteristics of a cache

  • When an application using the Save Session interface (), when the update (), saveOrUpdate (), if there is no corresponding session object cache, automatically queries the appropriate information from the database, which is written to the cache .
  • When you call the Session interface load, get () method, and a list iterator method Query interface, will determine whether the object exists in the cache, it is returned, it does not query the database , if the object is not to query the cache, and then to among the corresponding database query object, and added to a cache.
  • When you call session.close method, the cache will be cleared

Let's look at an example:

 @Test
    public void testSession(){
        Session session = HibernateUtil.openSession();
        Transaction transaction = session.beginTransaction();
        //第一次获取缓存中不存在需要发送sql到数据库中查询
        Customer customer1 = session.get(Customer.class, 2L);
        System.out.println(customer1);
        //第二次获取相同的数据,缓存中已经存在不需要去数据库获取
        Customer customer2 = session.get(Customer.class, 2L);
        System.out.println(customer2);
        System.out.println(customer1 == customer2);

        transaction.commit();
        session.close();
    }

The results of the above code is run:

You can see that we send a request for the same record twice, hibernate only help us send a sql statement, it is the second reason may be taken directly in the cache. And we can see two out of the same object!

2 cache internal structure

Among them, a cache area: Snapshots District

When we use the id to query the database, the result will hibernate query cache was left to the session, and it will copy the data to the session to place a snapshot .
When we use tr.commit () when the session will also clean up the cache (flush), clean the cache when the session level, Hibernate will use the OID of a cache objects and snapshots of objects for comparison If two objects (the object and a cache object snapshot) of the property changes, the update statement, update the database , this time a snapshot of the data area is updated to a data cache, if the two objects property does not change, then do not execute update statement. The goal is to ensure that consistent data cache and the database.

This will also explain our above-mentioned object persistence state why the data will automatically update !

Guess you like

Origin www.cnblogs.com/ThinMoon/p/12634981.html