Hibernate 系列教程15-一级缓存

时间:2022-09-19 09:04:56

Product

public class Product {
private Long id;
private String name;

Product.hbm.xml

<class name="Product">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
</class>

MainTest

public class MainTest {
@Before
public void save() throws Exception {
Product product = new Product();
product.setName("jege"); Session session = HibernateUtils.INSTANCE.getSession();
session.beginTransaction();
session.save(product);
session.getTransaction().commit();
session.close();
} // 模拟同一个session获取同一个OID对象
// 只发出一条sql:一级缓存命中
@Test
public void get() throws Exception {
Session session = HibernateUtils.getSession();
Product product1 = (Product) session.get(Product.class, 1L);
Product product2 = (Product) session.get(Product.class, 1L);
session.close();
}
}

源码地址

https://github.com/je-ge/hibernate

如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!

Hibernate 系列教程15-一级缓存

Hibernate 系列教程15-一级缓存