摘要:
无论load还是get,都会首先查找缓存(一级缓存),如果没有,才会去数据库查找,调用clear()方法可以强制清除session缓存flush()强制让缓存的内容与数据库的内容做同步SchemaExport:在程序里面控制生成建表语句。实际工作中可以不用hbm2ddl.auto这个配置,手动建表 @Test public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);; }第一个true表示是否打印出... 阅读全文
摘要:
讲update不是讲这个方法,而是讲什么时候会更新数据库里面的内容。什么情况下用更新?1.可以更新一个detachd状态对象,把它变成persistent,同时数据库里进行更新2.更新transient状态的对象会报错,但是更新自己设定好的ID的transient对象可以(前提是数据库里有对应的这条数据记录)有一个问题,它会将所有字段都进行更新,效率不高。是否可以做到只更新改过的字段呢?有三种方法1.修改注解,在model里面加@Column( updatable=false)。这种方式不好,因为它不灵活2.在xml配置文件上写一个dynamic-update="true" 阅读全文
摘要:
在测update方法之前,我们看读取:get和load。load就是从数据库里取一条记录,取到内存里,把一条记录转化成对应的对象。测试代码:package hjj.lch.hibernate.model;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;publi 阅读全文