Three paradigms Affairs Jpql statements optimistic locking Pessimistic locking database concurrency jpa experience

1 JPQL

1.1 What is jpql

jpql: jpa is a query language provided, similar to sql.

1.2 jpql and differentiated sql

(1) jpql inside the operation target and the object attributes sql operating tables and table columns inside the case-sensitive

(2) can not appear in jpql inside *, table name can not appear

(3) jpql sql and they are the same keywords, keywords are case insensitive

1.3 jpql syntax

sql syntax:

​ select *

from table

where the filter conditions

group by grouping condition

avg after having filtered packet (score)> 98

order by ordering

jpql similar

select o.name, o (object attribute)

from the class O the Join O. inside class attribute name p

where o. attribute name

group by o. attribute name

order by

1.4 jpql which join

sql: query link

External connection: the connection is divided into left outer (left table and the intersection of all the data must contain the right part of the table.) And a right outer connection (and vice versa) left join right join

En: data on the intersection portion join

JPQL:

select o from Employee o join o.department d

1.5 aggregate functions - and the same sql

sql: find the following values ​​for a set of data

count the number of records seeking seeking maximum sum sum max min avg averaging for the Minimum

1 5 6 8 2 -->count 5 max 8 min 1 sum 22 avg 22/5

With the use of group by group

1.6 sub-queries - and as sql

Subquery:

As the result of a query or a query further condition table used

Subquery: a nested query

select p from Employee p where p.salary > (select avg(o.salary) from Employee o)

1.7 jpql use collection

There may be used a value determined size

select o from Project o where o.employees.size = 0

1.8 Paging

sql syntax:

​ select * from table where limit beginIndex,pageSize

currentPage: Current Page

pageSize: the number of page

beginIndex: starting position beginIndex = (currentPage-1) * pageSize

pageSize: the number of page

​ select * from table where limit 0,5

​ select * from table where limit 5,5

2 SQL support

To sql

Using this method

createNativeQuery

3 transaction concurrency

3.1 What is a transaction

transaction

Services: a set of logical operations either all succeed or fail (with the total death)

3.2 What are the characteristics of the transaction (ACID)

Atomicity: A

This operation is a transaction which has a minimum unit, can not be split again

Consistency: C

The same amount of data consistency, before the operation, the total amount of 10,000, and after completing the operation, the total amount of 10 000

Isolation today :() I

Reflect more than two affairs operations, should have isolation between transactions, your transaction can not operate my business, if you operate my business transactions occur, it will bring the issue of concurrent transactions

Persistence: D

When we commit a transaction submitted data on the permanent presence down

3.3 Transaction Concurrency

Transaction concurrency:

A plurality of transaction (or two or more of the transaction),SimultaneouslyIn operationThe same dataTime, this phenomenon is called concurrent transactions

Transaction concurrency will bring some problems: - solved through database isolation mechanism

The first update appears lost - handles

The second category is missing Update - handles

Dirty read - database isolation mechanism

Waste (Magic degrees) - Database isolation mechanism

Non-repeatable read - database isolation mechanism

3.4 database isolation mechanism

Four kinds

READ UNCOMMITTED (uncommitted read) Read Fantasy (dummy read), not repeat the reading and allow dirty reads.

READ COMMITTED allow fantasy to read, non-repeatable read, do not allow dirty read

REPEATABLE READ allowed to read fantasy, do not allow dirty reads and non-repeatable read

SERIALIZABLE fantasy reads, non-repeatable reads and dirty reads are not allowed - performance certainly become low

3.5 The first and second categories lost updates

Lock mechanism

Pessimistic locking: very pessimistic locks need to wait for a lock release, in order to manipulate data, performance is not high

Optimistic locking: no need to wait, after the version number of the underlying need to control the first modification, the version number change, the second person can not operate on this product

4JPA usage rules or experience

. \ 1 bidirectional many association, not a one-way-to-many - a little high efficiency

. \ 2 flexible one-way one association - foreign key parties to maintain high efficiency point

\ 3. Not one to one, with many-to-replace (do not use a shared one primary key, foreign key with a unique one to one)

\ 4. The secondary cache configuration object (configuration Cacheable), query cache (query JPQL),

No query only use the query cache (a hit if the conditions are very low)

Query cache:

EntityManagerFactory different entityManager same same same transmission condition values ​​jpql

\ 5 combining relationship set the list (sequence repeat) - The second project document modules, many-to-use collection set

\ 6. Design table, the table field less, do not be afraid table associated with secondary cache support, design table as far as possible to reach the third paradigm (foreign key)

Database paradigm: database rules

1NF: design time, which try to ensure that the table can not store a plurality of column values, a minimum unit column, can not be divided - body atomic Series

2NF: inside each row of data table should have a unique value can be distinguished, it is recommended you create a primary key

3NF: Table should not be stored inside another non-primary key information in a table - I suggest you a foreign key

Published 23 original articles · won praise 2 · Views 938

Guess you like

Origin blog.csdn.net/metjoyful/article/details/101197870