get()和load()

Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		News news=(News)session.get(News.class, new Integer(1));
		News news1=(News)session.get(News.class, new Integer(1));
		transaction.commit();
		System.out.println(news.getTitle()+"======"+news.getContent());
		System.out.println(news1.getTitle()+"======"+news1.getContent());

 

Hibernate: 
    select
        news0_.id as id0_0_,
        news0_.title as title0_0_,
        news0_.content as content0_0_ 
    from
        mw_news news0_ 
    where
        news0_.id=?
dkfhk======dsjfkh
dkfhk======dsjfkh

 

	public static void main(String[] args) {
		Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		News news=(News)session.load(News.class, new Integer(1));
		News news1=(News)session.load(News.class, new Integer(1));
		transaction.commit();
		System.out.println(news.getTitle()+"======"+news.getContent());
		System.out.println(news1.getTitle()+"======"+news1.getContent());
		
	}

 

Hibernate: 
    select
        news0_.id as id0_0_,
        news0_.title as title0_0_,
        news0_.content as content0_0_ 
    from
        mw_news news0_ 
    where
        news0_.id=?
dkfhk======dsjfkh
dkfhk======dsjfkh

 方法load()和方法get()都可以根据标示符属性值查询获取一个持久化对象,但是在未找到符合条件的持久化对象时,load()方法抛出异常,get()方法返回null.

get()方法先从hibernate一级缓存中查询符合条件的对象,找不到则直接到数据库中查找记录,而方法load会先从hibernate中的一级缓存中查找符合条件的对象,找不到还会在hibernate的二级缓存对象中查找对象,仍未找到则才到数据库中去查找

你可能感兴趣的:(load)