Hibernate 持久化上下文

– Start

  1. 其实所谓的 Hibernate 持久化上下文指的是 Session 接口或 JPA 的 EntityManager 接口,我们对数据库进行操作都是通过它来实现的。
  2. 刚创建的实体类对象的转态是 transient
  3. 通过调用 session 的 persist 或 save 方法,实体类对象由 session 管理,它的转态变为了 persistent,此后对该对象的修改操作都会调用 flush 后被同步到数据库中
  4. 我们也可以调用 session 的 detach, evict 或 clear 方法把对象移出 session,它的状态变为了 detached,对 detached 对象的操作不会同步到数据库中
  5. 如果你想把 detached 对象重新加入到 session 中,我们可以调用 lock, update 或 saveOrUpdate
  6. 我们也可以使用 merge 方法把 detached 对象 merge 到 persistent 对象中
  7. 我们也可以使用 contains 判断对象是否已经加入 session 中 (persistent 对象)
  8. 最后调用 session 的 delete 或 remove 后,它的状态变为 removed
  9. flush 方法用来把对象同步到数据库中, refresh 方法用来把数据库的值同步到对象中
package shangbo.hibernate.demo028;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class App {
	public static void main(String[] args) throws Exception {
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure(ClassLoader.getSystemResource("shangbo/hibernate/demo028/hibernate.cfg.xml")).build();
		Metadata metadata = new MetadataSources(registry).getMetadataBuilder().applyPhysicalNamingStrategy(new MyPhysicalNamingStrategy()).build();
		SessionFactory sessionFactory = metadata.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		// 刚创建的实体类对象的转态是 transient
		Customer customer = new Customer("test");

		// 通过调用 session 的 persist 或 save 方法,实体类对象由 session 管理
		// 它的转态变为了 persistent
		// 此后对该对象的修改操作都会调用 flush 后被同步到数据库中
		session.save(customer);
		
		// 我们也可以调用 session 的 detach, evict 或 clear 方法把对象移出 session
		// 它的状态变为了 detached,对 detached 对象的操作不会同步到数据库中
		session.detach(customer);
		
		// 如果你想把 detached 对象重新加入到 session 中
		// 我们可以调用 lock, update 或 saveOrUpdate
		session.update(customer);
		
		// 我们也可以使用 merge 方法把 detached 对象 merge 到 persistent 对象中
		session.detach(customer);
		customer.setCustomerName("test2");
		
		Customer customerPersistent = session.load(Customer.class, 1);
		customerPersistent = (Customer) session.merge(customer);
		
		// 我们也可以使用 contains 判断对象是否已经加入 session 中 (persistent 对象)
		if(session.contains(customerPersistent)) {
			System.out.println("customerPersistent");
		}
		
		// 最后调用 session 的 delete 或 remove 后,它的状态变为 removed
		session.delete(customer);
		
		// flush 方法用来把对象同步到数据库中, refresh 方法用来把数据库的值同步到对象中
		session.flush();
		session.refresh(customerPersistent);
		
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
}

– 更多参见:Hibernate 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-07-04
– Written by ShangBo on 2019-07-04
– End

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值