Java bean:
package com.my.bean; import java.util.Date; public class WorkPack { private String uWorkPackID; private String BaselineID; private String SourceID; private String uProjectID; private String sPackName; private String sDescription; private Date dPackStartDate; private Date dPackEndDate; private short iStatus; private String uParentID; private java.math.BigDecimal fPMPlanWorkload; private short iPriority; private String sBizID; private String sDeadwayMemo; private short IsIteration; private short ProgressRate; private String Owner; private short ScheduleStatus; private String sCreator; private Date dCreateTime; private String sModifiedUser; private Date sModifiedTime; private String remark; private String KeyIn; private String KeyOut; private java.math.BigDecimal WorkLineBase; private String UpdateType; private String Predecessors; public String getuWorkPackID() { return uWorkPackID; } public void setuWorkPackID(String uWorkPackID) { this.uWorkPackID = uWorkPackID; } public String getBaselineID() { return BaselineID; } public void setBaselineID(String baselineID) { BaselineID = baselineID; } public String getSourceID() { return SourceID; } public void setSourceID(String sourceID) { SourceID = sourceID; } public String getuProjectID() { return uProjectID; } public void setuProjectID(String uProjectID) { this.uProjectID = uProjectID; } public String getsPackName() { return sPackName; } public void setsPackName(String sPackName) { this.sPackName = sPackName; } public String getsDescription() { return sDescription; } public void setsDescription(String sDescription) { this.sDescription = sDescription; } public Date getdPackStartDate() { return dPackStartDate; } public void setdPackStartDate(Date dPackStartDate) { this.dPackStartDate = dPackStartDate; } public Date getdPackEndDate() { return dPackEndDate; } public void setdPackEndDate(Date dPackEndDate) { this.dPackEndDate = dPackEndDate; } public short getiStatus() { return iStatus; } public void setiStatus(short iStatus) { this.iStatus = iStatus; } public String getuParentID() { return uParentID; } public void setuParentID(String uParentID) { this.uParentID = uParentID; } public java.math.BigDecimal getfPMPlanWorkload() { return fPMPlanWorkload; } public void setfPMPlanWorkload(java.math.BigDecimal fPMPlanWorkload) { this.fPMPlanWorkload = fPMPlanWorkload; } public short getiPriority() { return iPriority; } public void setiPriority(short iPriority) { this.iPriority = iPriority; } public String getsBizID() { return sBizID; } public void setsBizID(String sBizID) { this.sBizID = sBizID; } public String getsDeadwayMemo() { return sDeadwayMemo; } public void setsDeadwayMemo(String sDeadwayMemo) { this.sDeadwayMemo = sDeadwayMemo; } public short getIsIteration() { return IsIteration; } public void setIsIteration(short isIteration) { IsIteration = isIteration; } public short getProgressRate() { return ProgressRate; } public void setProgressRate(short progressRate) { ProgressRate = progressRate; } public String getOwner() { return Owner; } public void setOwner(String owner) { Owner = owner; } public short getScheduleStatus() { return ScheduleStatus; } public void setScheduleStatus(short scheduleStatus) { ScheduleStatus = scheduleStatus; } public String getsCreator() { return sCreator; } public void setsCreator(String sCreator) { this.sCreator = sCreator; } public Date getdCreateTime() { return dCreateTime; } public void setdCreateTime(Date dCreateTime) { this.dCreateTime = dCreateTime; } public String getsModifiedUser() { return sModifiedUser; } public void setsModifiedUser(String sModifiedUser) { this.sModifiedUser = sModifiedUser; } public Date getsModifiedTime() { return sModifiedTime; } public void setsModifiedTime(Date sModifiedTime) { this.sModifiedTime = sModifiedTime; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getKeyIn() { return KeyIn; } public void setKeyIn(String keyIn) { KeyIn = keyIn; } public String getKeyOut() { return KeyOut; } public void setKeyOut(String keyOut) { KeyOut = keyOut; } public java.math.BigDecimal getWorkLineBase() { return WorkLineBase; } public void setWorkLineBase(java.math.BigDecimal workLineBase) { WorkLineBase = workLineBase; } public String getUpdateType() { return UpdateType; } public void setUpdateType(String updateType) { UpdateType = updateType; } public String getPredecessors() { return Predecessors; } public void setPredecessors(String predecessors) { Predecessors = predecessors; } }
Hibernate的mapping xml:
<?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.my.bean.WorkPack" table="PPM_WorkPack"> <id name="uWorkPackID" type="java.lang.String" length="37"> <column name="uWorkPackID" /> <generator class="assigned" /> </id> <property name="BaselineID" type="java.lang.String" length="37"> <column name="BaselineID" /> </property> <property name="SourceID" type="java.lang.String" length="37"> <column name="SourceID" /> </property> <property name="uProjectID" type="java.lang.String" length="37"> <column name="uProjectID" /> </property> <property name="sPackName" type="java.lang.String" length="200"> <column name="sPackName" /> </property> <property name="sDescription" type="java.lang.String" length="4000"> <column name="sDescription" /> </property> <property name="dPackStartDate" type="java.util.Date"> <column name="dPackStartDate" /> </property> <property name="dPackEndDate" type="java.util.Date"> <column name="dPackEndDate" /> </property> <property name="iStatus" type="short"> <column name="iStatus" /> </property> <property name="uParentID" type="java.lang.String" length="37"> <column name="uParentID" /> </property> <property name="fPMPlanWorkload" type="java.math.BigDecimal"> <column name="fPMPlanWorkload" /> </property> <property name="iPriority" type="short"> <column name="iPriority" /> </property> <property name="sBizID" type="java.lang.String" length="60"> <column name="sBizID" /> </property> <property name="sDeadwayMemo" type="java.lang.String" length="4000"> <column name="sDeadwayMemo" /> </property> <property name="IsIteration" type="short"> <column name="IsIteration" /> </property> <property name="ProgressRate" type="short"> <column name="ProgressRate" /> </property> <property name="Owner" type="java.lang.String" length="255"> <column name="Owner" /> </property> <property name="ScheduleStatus" type="short"> <column name="ScheduleStatus" /> </property> <property name="sCreator" type="java.lang.String" length="255"> <column name="sCreator" /> </property> <property name="dCreateTime" type="java.util.Date"> <column name="dCreateTime" /> </property> <property name="sModifiedUser" type="java.lang.String" length="255"> <column name="sModifiedUser" /> </property> <property name="sModifiedTime" type="java.util.Date"> <column name="sModifiedTime" /> </property> <property name="remark" type="java.lang.String" length="4000"> <column name="remark" /> </property> <property name="KeyIn" type="java.lang.String" length="100"> <column name="KeyIn" /> </property> <property name="KeyOut" type="java.lang.String" length="100"> <column name="KeyOut" /> </property> <property name="WorkLineBase" type="java.math.BigDecimal"> <column name="WorkLineBase" /> </property> <property name="UpdateType" type="java.lang.String" length="25"> <column name="UpdateType" /> </property> <property name="Predecessors" type="java.lang.String" length="800"> <column name="Predecessors" /> </property> </class> </hibernate-mapping>
测试例子:
package com.my.test; import java.util.Date; import java.util.List; import java.util.UUID; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.my.bean.WorkPack; public class TestHibernate { @SuppressWarnings("unchecked") public static void main(String[] args) { Configuration cfg = new Configuration(); @SuppressWarnings("deprecation") SessionFactory factory = cfg.configure().buildSessionFactory(); Session session = factory.openSession(); org.hibernate.Transaction trans = session.beginTransaction(); //------------------------------------------------------------- // Select //------------------------------------------------------------- String hqlSelect = "from WorkPack where uProjectID=:uProjectID"; Query querySelect = session.createQuery(hqlSelect); querySelect.setParameter("uProjectID", "b84570f4-81de-4b18-abb3-48c431e398c4"); List<WorkPack> list = querySelect.list(); //------------------------------------------------------------- // Update //------------------------------------------------------------- String hqlUpdate = "update WorkPack set sPackName=:sPackName where uWorkPackID=:uWorkPackID"; Query queryUpdate = session.createQuery(hqlUpdate); queryUpdate.setParameter("sPackName", "hi"); queryUpdate.setParameter("uWorkPackID", "A1514770-2D6F-43A5-AF83-DD26068F0DF9"); int resultUpdate = queryUpdate.executeUpdate(); //------------------------------------------------------------- // Insert //------------------------------------------------------------- WorkPack wp = new WorkPack(); wp.setuWorkPackID(UUID.randomUUID().toString()); wp.setBaselineID("00000000-0000-0000-0000-000000000001"); wp.setSourceID(null); wp.setuProjectID("B84570F4-81DE-4B18-ABB3-48C431E398C4"); wp.setsPackName("insert"); wp.setsDescription(null); wp.setdPackStartDate(null); wp.setdPackEndDate(null); wp.setiStatus((short)1); wp.setuParentID(null); wp.setfPMPlanWorkload(new java.math.BigDecimal(0)); wp.setiPriority((short)3); wp.setsBizID(null); wp.setsDeadwayMemo(null); wp.setIsIteration((short)0); wp.setProgressRate((short)20); wp.setOwner("zhangxuebin WX208751"); wp.setScheduleStatus((short)0); wp.setsCreator("zhangxuebin WX208751"); wp.setdCreateTime(new Date()); wp.setsModifiedUser("zhangxuebin WX208751"); wp.setsModifiedTime(new Date()); wp.setRemark(null); wp.setKeyIn(null); wp.setKeyOut(null); wp.setWorkLineBase(new java.math.BigDecimal(0)); wp.setUpdateType(null); wp.setPredecessors(null); session.save(wp); //------------------------------------------------------------- // Delete //------------------------------------------------------------- String hqlDelete = "delete WorkPack where uWorkPackID=:uWorkPackID"; Query queryDelete = session.createQuery(hqlDelete); queryDelete.setParameter("uWorkPackID", wp.getuWorkPackID()); int resultDelete = queryDelete.executeUpdate(); trans.commit(); session.close(); // Print select results for (WorkPack item : list) { System.out.println(item.getsPackName()); } // Print update results System.out.println(resultUpdate); // Print delete results System.out.println(resultDelete); } }
事实上,insert的方法也可以使用Query的HQL直接操作。
参考引用:
http://www.cnblogs.com/wucg/archive/2012/06/04/2533537.html