Day 6 of Hibernate

Source: Internet
Author: User

1. Place the entire inheritance relationship in a table. To distinguish specific subclass information, an additional column needs to be created in the table.
Create Table paymeny (
.....
Type varchar2 (12)
);
<Class name = "Payment" table = "Payment">
.....
<Discriminitor column = "type" type = "string">
<Sub-class name = "cardpayment" discriminitor-value = "card">
Subclass attributes
</Sub-class>
<Sub-class name = "cashpayment" discriminitor-value = "cash">
Subclass attributes
</Sub-class>
</Class>
High Efficiency and Polymorphism
Redundant waste of space
2. Combine the parent class information with each subclass information to create multiple specific tables.
Create Table cardpayment (
Parent class information
Credit card subclass Information
)
Create Table cashpayment (
Parent class information
Cash subclass Information
)
<Class name = "Payment">
.....

<Union-sub-class name = "cardpayment" table = "cardpayment">
Subclass attributes
</Union-sub-class>
<Union-sub-class name = "cashpayment" table = "cashpayment">
Subclass attributes
</Union-sub-class>
</Class>
Convenient operations, high efficiency, high space utilization
No polymorphism in redundancy
3. Create the corresponding table for each specific object
Create Table payment (
Parent class information
)
Create Table cardpayment (
Credit card subclass Information
Foreign key cardid
)
Create Table cashpayment (
Cash subclass Information
Foreign key cashid
)
<Class name = "Payment" table = "Payment">
.....

<Joined-sub-class name = "cardpayment" table = "cardpayment">
<Key column = "cardid"> </key>
Subclass attributes
</Joined-sub-class>
<Joined-sub-class name = "cashpayment" table = "cashpayment">
<Key column = "cashid"> </key>
Subclass attributes
</Joined-sub-class>
</Class>
High utilization of polymorphism without redundant Space
Inefficient operations

Hibernate query:
1 native SQL
Programmers are proficient in SQL
Control SQL efficiency by yourself
The queried information can only be encapsulated by yourself.
2 hql student1 ---- 1 Address
Based on the SQL framework, object-oriented
From student;
Select S. name from student s;
From student s where S. age> 20;
From student s inner join address a where a. Name = ...;
3 criteria
Standard Object-Oriented Query
Session. createcriteria (student. Class );
Session. createcriteria (student. Class)
. Add (restrictions. gt ("Age", 20 ));
Session. createcriteria (student. Class)
. Createcriteria ("Address ")
. Add (restrictions. eq ("name ","..."));
4 named Query
Extract SQL statements from source code to the ing File
*. HBM. xml
<Hibernate-mapping>
<Class>
.....
</Class>
<Query name = "SQL variable name">
<! [CDATA [
Hql statement ...? /: Name
]>
</Query>
</Hibernate-mapping>

Session. getnamedquery ("SQL variable name ")

Hibernate supports data sources:
1 hiebrnate built-in data source, used for testing only
<Property name = "connection. username"> Scott </property>
<Property name = "connection. url">
JDBC: oracle: thin: @ localhost: 1521: sinojava
</Property>
<Property name = "dialect">
Org. hibernate. dialect. oracle9dialect
</Property>
<Property name = "connection. Password"> tiger </property>
<Property name = "connection. driver_class">
Oracle. JDBC. Driver. oracledriver
</Property>

2 ******* integrate c3p0 with hibernate *******
<Property name = "connection. username"> Scott </property>
<Property name = "connection. url">
JDBC: oracle: thin: @ localhost: 1521: sinojava
</Property>
<Property name = "dialect">
Org. hibernate. dialect. oracle9dialect
</Property>
<Property name = "connection. Password"> tiger </property>
<Property name = "connection. driver_class">
Oracle. JDBC. Driver. oracledriver
</Property>

Add the following information:
<Property name = "connection. provider_class">
Org. hibernate. Connection. c3p0connectionprovider
</Property>
<Property name = "c3p0. max_size"> 5 </property>
<Property name = "c3p0. Timeout"> 2000 </property>
..........

3. JNDI
<Property name = "connection. username"> Scott </property>
<Property name = "connection. url">
JDBC: oracle: thin: @ localhost: 1521: sinojava
</Property>
<Property name = "dialect">
Org. hibernate. dialect. oracle9dialect
</Property>
<Property name = "connection. Password"> tiger </property>
<Property name = "connection. driver_class">
Oracle. JDBC. Driver. oracledriver
</Property>

Add the following information:
<Property name = "JNDI"> JAVA: COMP/ENV/userds </property>

The program must manually obtain the connection and pass it to hibernate:
Getsession (){
Context Env = new intialcontext ();
Datasource DS = (datasource) ENV. Lookup ("Java: COMP/ENV/userds ");
Conenction con = Ds. getconnection ();
...
Sessionfactory. opensession (conn );
....
}

Hibernate supports paging:
Query. setfirstresult (start position );
Query. setmaxresults (number of queries );
Query. List ();

Lock:
Read/write locks
Table-Level Lock/column-Level Lock

Concurrency Control in JDBC:
1 Database
Isolation level
Transaction_none 0
Transaction_uncommited_read 1
Transaction_commied_read 2
Transaction_repeatable_read 4
Transantion_serializable 8
Connection. settransactionisolation (...)

Select ***** [for update ];
2 applications
Synchronized
* ***** Hibernate controls multi-thread concurrency:
1 Database
Isolation level
Transaction_none 0
Transaction_uncommited_read 1
Transaction_commied_read 2
Transaction_repeatable_read 4
Transantion_serializable 8

Hibernate. cfg. xml
<Property name = "connection. Isolation"> 2 </property>


Select ***** [for update ];
2 applications
Pessimistic lock:
It is assumed that two threads will access one data simultaneously.
The operation must be locked:
Query q = session. createquery ("from student s ")
Q. setlockmode ("S", lokemode. Write );
Lockmode:
None
Read lock
Write lock
Update table-Level Lock <==> select *** for update;
Optimistic lock:
It is considered that a thread may not necessarily be concurrently accessed.

Thread 1 thread 2
------------------------------------------------------
Balance = 1000
1000 1000
1000-600 commit
400 1000-200 commit
800

Solution: Use a flag to Control Data Validity

Balance identity thread 1 thread 2
-----------------------------------------------------------------
1000 1
1000 1 1000 1
1000-600 1 + 1 commit
400 if (2> 1) Submit
1000-200 1 + 1 commit
If (2> 2) cannot be submitted

Flag space:
Timestamp:

** Version:

Hibernate cache mechanism:
Level 1 cache: Session Object
Session. Get (class, serializble)
Session. Load (class, serializble)

GET/load commonalities:
The SQL statement structure sent by get and load is the same.
Session ---> sessionfactory ---> DB
Difference between get and load:
Get:
1. If you call the get method, you can directly send an SQL query.
2. Get directly searches for information. If no information is found, null is returned.

Load:
1. The load method is called and no SQL statement is sent by default.
SQL query is not sent until this object is actually used.
Object returned by the load method: If no information is found, the proxy object is returned.
If the information is used, the actual object 2 load method cannot be found, and an exception org. hibernate. objectnotfoundexception is reported.

Level 2 Cache: sessionfactory
1. Add attributes to hibernate. cfg. xml.
-- Set the second-level cache class
<Property name = "cache. provider_class">
Org. hibernate. cache. ehcacheprovider
</Property> Level 2
-- Sets the cache for query operations. Otherwise, the cache takes effect for get/load.
<Property name = "cache. use_query_cache"> true </property>

2. Set the secondary cache in the HBM ing File
<Class name = "concur" table = "concur_time">
<Cache Usage = "Read-Only | read-write"/>
...
</Class>

3. If you use a query statement to query and want to use a second-level cache
Query. setcacheable (true );
Query. list ....

Hibernate object status and session methods:
Transient temporary object
Detached free object
Persist persistence object

Configuration CFG = new configuration (). Configure ();
Sessionfactory Sf = cfg. buildsessionfactory ();
Session session = SF. opensession ();
Transaction Tx = session. begintransaction (); Memory session DB

Concur c = new concur ("hehe"); there is no === transient
Bytes ----------------------------------------------------------------------------------------------
Session. Save (C );

TX. Commit (); has a === persist
Bytes ----------------------------------------------------------------------------------------------
Session. Close (); no === detched
Bytes ----------------------------------------------------------------------------------------------

1. Build a hibernate Environment
2. hibernate operation steps
3 1-1 1-m
4. inheritance relationship processing: 3 methods
5 optimistic lock
6. HBM configuration file
7. Common Methods of session
8 hql/criteria
9 Data Source-c3p0
10 Id generation policy-native (sequence, identity, HiLo)-UUID. HEX

 

 

 

 

 

 

 

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.