JPA-二级缓存

persistence.xml配置如下



    
        
        org.hibernate.ejb.HibernatePersistence

        
        cn.bdqn.entity.Customer
        cn.bdqn.entity.Order
        cn.bdqn.entity.Department
        cn.bdqn.entity.Manager
        cn.bdqn.entity.Category
        cn.bdqn.entity.Item
        
        
        ENABLE_SELECTIVE
        
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

在需要被缓存的实体类加上@Cacheable(true)

@Table(name="customers") 
@Entity
@Cacheable(true)
public class Customer {
@Test

  @Test
    public void SecondLevelCache(){
        Customer customer1 = entityManager.find(Customer.class, 1);
        transaction.commit();
        entityManager.close();
        
        entityManager = entityManagerFactory.createEntityManager();
        transaction = entityManager.getTransaction();
        transaction.begin();
        Customer customer2 = entityManager.find(Customer.class, 1);
    }

只会发送一条SQL语句


你可能感兴趣的:(JPA-二级缓存)