1.get()andload()Java代码 session.get(Clazz,id); session.load(Clazz,id); session.get(Clazz,id); session.load(Clazz,id); 说明:load()与get()的区别 请注意如果没有匹配的数据库记录,load()方法可能抛出无法恢复的异常(unrecoverableexception)。如果类的映射使用了代理(proxy),load()方法会返回一个未初始化的代理,直到你调用该代理的某方法时才会去访问数据库。若你希望在某对象中创建一个指向另一个对象的关联,又不想在从数据库中装载该...
http://www.jb51.net//article/16685.htm
以下程序直接通过HibernateAPI批量更新CUSTOMERS表中年龄大于零的所有记录的AGE字段:tx=session.beginTransaction();Iteratorcustomers=session.find("fromCustomercwherec.age>0").iterator();while(customers.hasNext()){Customercustomer=(Customer)customers.next();customer.setAge(customer.getAge()+1);}tx.commit();session.close();如果CUSTO...
http://www.jb51.net//article/16483.htm
[code]Usertableuser=null;Sessionsession=HibernateSessionFactory.getSession();Stringsql="fromUsertableasuserwhereuser.username=?";Queryq=session.createQuery(sql);q.setString(0,username);Listl=q.list();Iteratorite=l.iterator();if(ite.hasNext()){user=(Usertable)ite.next();}returnuser;[/code]查询完以后才能更新用S...
http://www.jb51.net//article/16480.htm
一个PO有三种状态:1、未被持久化的VO此时就是一个内存对象VO,由JVM管理生命周期2、已被持久化的PO,并且在Session生命周期内此时映射数据库数据,由数据库管理生命周期3、曾被持久化过,但现在和Session已经detached了,以VO的身份在运行这种和Session已经detached的PO还能够进入另一个Session,继续进行PO状态管理,此时它就成为PO的第二种状态了。这种PO实际上是跨了Session进行了状态维护的。在传统的JDO1.x中,PO只有前面两种状态,一个PO一旦脱离PM,就丧失了状态了,不再和数据库数据关联,成为一个纯粹的内存VO,它即使进入一个新的PM,也...
http://www.jb51.net//article/16479.htm
例如:[code]HibernateTest.javaimportonlyfun.caterpillar.*;importnet.sf.hibernate.*;importnet.sf.hibernate.cfg.*;importjava.util.*;publicclassHibernateTest{publicstaticvoidmain(String[]args)throwsHibernateException{SessionFactorysessionFactory=newConfiguration().configure().buildSessionFactory();Session...
http://www.jb51.net//article/16478.htm
insert方法[code]publicvoidinsert(Objecto){Sessionsession=HibernateSessionFactory.currentSession();Transactiont=session.beginTransaction();session.save(o);t.commit();HibernateSessionFactory.clossSession();}[/code]delete方法[code]publicvoiddelete(Objecto,Serializableid){Sessionsession=HibernateSessionFact...
http://www.jb51.net//article/16477.htm
1、Configuration/SessionFactory/SessionConfiguration实例代表了一个应用程序中Java类型到SQL数据库映射的完整集合.Configuration被用来构建一个(不可变的(immutable))SessionFactory.SessionFactory是线程安全的,创建代价很高。Session是非线程安全的,轻量级的。一个Session对应一个JDBC连接,Session的connection()会获取Session与之对应的数据库连接Connection对象。Session的功能就是操作对象的,这些对象和数据库表有映射关系。Session操作的...
http://www.jb51.net//article/16476.htm
数据的保存,更新和删除:1、Session.save()方法:Session.save()方法用于实体对象的持久化保存,也就是说当执行session.save()方法时会生成对应的insertSQL语句,完成数据的保存。如下面的代码:Useruser=newUser();user.setName(“zx”);Transactiontx=session.beginTransaction();session.save(user);tx.commit();当执行到session.save()方法时,Hibernate并不会马上生成insertSQL语句来进行数据的保存,而是当稍后清理session的...
http://www.jb51.net//article/16475.htm
1<propertyname="hibernateProperties">2<props>3<propkey="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>4<propkey="hibernate.show_sql">false</prop>5<!--Create/updatethedatabasetablesautomaticallywhentheJVMstartsup6<propkey="hibernate.hbm2ddl.auto">update</prop>-->7<!--Turnbatchingofffo...
http://www.jb51.net//article/16040.htm
示例数据表:team(班级)、certificate(身份证)、student(学生)Team.hbm.xml<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- MappingfileautogeneratedbyMyEclipsePer...
http://www.jb51.net//article/16039.htm
dao层接口:Java代码[code]packagecom.last999.im.news.dao;importjava.util.*;importcom.last999.im.news.entity.KindEntity;importcom.last999.im.news.web.PageTool;publicinterfaceKindEntityDao{publicKindEntityget(Stringuuid);publicvoidsave(KindEntitykindEntity);publicvoidupdate(KindEntitykindEntity);publicvoidde...
http://www.jb51.net//article/15976.htm
