1JPA persistent state of the object
1.1 Temporary state (transient) : transient state
Just a new statement creates, did not entityManager a relationship
Not persistent, not in entityManager in. The object becomes temporary object
1.2 persistent state (persistent) : Managed status
And entityManager a relationship
Has been persisted, added to entityManager level cache ( the persist Remove Merge ) .
@Test public void testUpdateid () throws Exception { the EntityManager entityManager = JpaUtil.getEntityManager (); entityManager.getTransaction () the begin ();. OIDDomain the Domain = EntityManager.find (OIDDomain. Class , 1L); // persistent state Domain.setName ( "little 17" ); // cache // EntityManager.persist (Domain); // persistent state // commit the transaction entityManager.getTransaction () the commit ();. entityManager.close (); // free state }
The state is the object of persistent objects.
1.3 free state (detached) : detached state
It has been persistent, but not in entityManager in.
This state is the object of the free object.
1.4. Delete status (removed) : From JPA began some state
Only calls entityManager.remove (domain objects ) method
Objects associated ID , and entityManager under management,
But the plan has been deleted, the transaction commits really been deleted.
1.5. Dirty data update
A persistent state of objects within a transaction management , if you change the original data ( non-key ) , appears dirty data , automatically issued when the transaction is committed update statements to modify.
@Test public void testUpdateid () throws Exception { . = JpaUtil the EntityManager entityManager getEntityManager () ; entityManager.getTransaction () the begin (). ; OIDDomain the Domain = EntityManager.find (OIDDomain. Class, 1L) ; // persistent state Domain.setName ( "little 17") ; // cache //entityManager.persist(Domain);// persistent state // commit the transaction entityManager.getTransaction () the commit (). ; entityManager.close () ; // free state }
1.5 execution flow analysis
JPAUtil get entityManager, open affairs
By entityManager get an object, the object is now persistent objects
This object will be placed in a cache inside
JPA will be to prepare a snapshot of the current object ( this object is backed up )
The third step: to commit the transaction
It will snapshots and compare your data for this object now
If the same, no need to modify not sent SQL ( performance is high )
When the time is not the same, JPA will think this data is dirty data
It will be dirty at the time of the transaction commits, it synchronizes database ( Send update SQL statement )
Hibernate: update t_iod set name=? where id=?
The relationship between objects 2'domain
2.1. Dependencies
Interdependence among injecting Spring managed objects
Controller performance level depends on the Service business layer, Service relies on Dao persistence layer
2.2. Relationship
According to association multiplicity can be divided into one to one, one to many, and many-to-many. Mainly refers to the database ( class ) relations
According navigational classified as unidirectional and bidirectional associations association.
Like a person, but he did not know unrequited love
A couple I have you you have me, bidirectional
2.3. Aggregation relationship (or essentially bidirectional many , many)
It represents the whole part of the relationship, and the whole part can be separated alone.
A corresponding number of man woman, this woman more each person has their own circle, maybe there is this gay gay;
Paolu a brothel pimps, men can pick up my sister alone, the Cowherd and can continue next
2.4. Combining relationship ( in essence, is an aggregation relationship , bidirectional many, many )
An order
2.5 Many-code analysis
@Entity
@Table(name = "t_product")
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;
private String address;
@ManyToOne//FetchType.EAGER迫切加载
@JoinColumn(name ="dir_id")
private ProductDir dir;
Correct wording: first save the party, then save the multi-party
HE a secondary cache 3
Cache is at the expense of memory for time
一级缓存就是同一个entitymanagerfactory 同一个entitymanager 同oid(默认的)
3.2.1. 添加二级缓存jar文件
pml配置这个导报
hibernate-release-4.3.8.Final\lib\optional\ehcache\3个jar文件
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.8.Final</version>
</dependency>
3.2.2. 搜索配置信息Cache
添加persistence.xml配置信息
<!-- 启用二级缓存 -->
<property name="hibernate.cache.use_second_level_cache" value="true" />
<!-- 二级缓存的实现类,文档里面的包名有错的 -->
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
<!-- 启用查询缓存 -->
<property name="hibernate.cache.use_query_cache" value="true" />
1.2. 在<properties>上面添加配置二级缓存扫描的策略
<!-- ALL:所有的实体类都被缓存 -->
<!-- NONE:所有的实体类都不被缓存. -->
<!-- ENABLE_SELECTIVE:标识 @Cacheable(true) 注解的实体类将被缓存 -->
<!-- DISABLE_SELECTIVE:缓存除标识 @Cacheable(false) 以外的所有实体类 -->
<!-- UNSPECIFIED:默认值,JPA 产品默认值将被使用 -->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
1.3. domain类的二级缓存
二级缓存类配置:命中条件:同一个SessionFactory,不同的entityManager,OID相同
// @Entity
// @Cacheable(true)
// public class Department