日期:2014-05-16 浏览次数:20563 次
package com.lwf.hibernate; import java.util.Date; public class Doc { private String id; private String name; private Date createDate; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.lwf.hibernate.Doc"> <id name="id"> <generator class="uuid"></generator> </id> <property name="name"></property> <property name="createDate"></property> </class> <query name="HqlName"> <![CDATA[select id,name from Doc]]> </query> <query name="HqlNameAll"> <![CDATA[from Doc order by name desc]]> </query> <query name="HqlNameAllParam"> <![CDATA[from Doc where name=:name]]> </query> </hibernate-mapping>
package com.lwf.hibernate; import java.util.Date; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class DocSessionTest { public static void main(String[] args) { // testSave(); testGet(); //testLoad(); } public static void commit(Session session){ try { session.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); session.getTransaction().rollback(); } } public static void closeSession(Session session){ if(session.isOpen()){ session.close(); } } public static Session getSession(){ Session session = null; Configuration config = new Configuration().configure(); SessionFactory factory = config.buildSessionFactory(); session = factory.openSession(); return session; } public static void testSave(){ Session session = null; session = getSession(); session.beginTransaction(); Doc doc = new Doc(); session.save(doc); doc.setName("gdgdgd"); doc.setCreateDate(new Date()); session.flush(); commit(session); closeSession(session); } public static void testGet(){ Session session = null; session = getSession(); session.beginTransaction(); Doc doc = (Doc)session.get(Doc.class, "402880f62861314e0128613157060001"); //get会立刻发出查询语句,而load则不会立刻发出查询,直到后面有使用doc如doc.setName才进行查询. //System.out.println(doc.getName()); //doc.setName("t"); session.flush(); commit(session); closeSession(session); } public static void testLoad(){ Session session = null; session = getSession(); session.beginTransaction(); //get会立刻发出查询语句,而load则不会立刻发出查询,直到后面有使用doc如doc.setName才进行查询. //load方法实现了lazy即懒模式 Doc doc =(Doc)session.load(Doc.class, "40288e81280b1db101280b1db61c0001"); //System.out.println(doc.getName()); //doc.setName("t"); session.flush(); commit(session); closeSession(session); } }
package com.lwf.hibernate; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class DocSqlTest { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory fa