锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 涓撴敞浜庡垎浜獼ava鍚庣鐩稿叧鎶鏈佽佸徃鏈哄疄鎴樺共璐э紝涓嶉檺浜嶫VM銆佸茍鍙戙佽璁℃ā寮忋佹ц兘浼樺寲銆佸垎甯冨紡&寰湇鍔°佷簯鍘熺敓銆佸ぇ鏁版嵁鐩稿叧涓婚銆?/span> 杞歡緋葷粺鐨勫茍鍙戞帶鍒朵竴鑸槸閫氳繃鍔犻攣錛堟湁涔愯閿佸拰鎮茶閿佷袱縐嶏級鏉ュ疄鐜幫紝涔愯閿佹槸涓縐嶄簨鍚庤ˉ鏁戞帾鏂斤紝鏄氳繃紼嬪簭鐨勯昏緫鎺у埗鐗堟湰鏉ュ疄鐜扮殑錛岃屾偛瑙傞攣鏄簨鍓嶇殑涓縐嶉闃叉帾鏂斤紝瀹冨埄鐢ㄦ暟鎹簱鐨勯攣鏈哄埗鏉ュ疄鐜般?/p>
铏界劧涔愯閿佽兘澶熸彁楂樼郴緇熺殑鎬ц兘錛屼絾瀹冩槸瀵瑰彂鐢熷啿紿佺殑璁塊棶榪涜浜嬪悗鐨勮ˉ鏁戯紝搴旂敤鍦ㄧ敤鎴瘋緭鍏ユ暟鎹噺寰堝皯鐨勫満鍚堟瘮杈冮傚悎錛屼絾濡傛灉鍦ㄤ紒涓?a class="infotextkey" target="_blank">ERP錛岀敤鎴蜂笌緋葷粺浜や簰娑夊強澶ч噺鏁版嵁鍦ㄩ〉闈㈣〃鍗曚笂褰曞叆錛屽鏋滀簨鍚庢彁浜ゅけ璐ュ悗鎵嶆彁紺虹敤鎴瘋閲嶆柊褰曞叆鏄緢涓嶇幇瀹炵殑錛屾墍浠ユ湁蹇呰榪涜浜嬪墠鎺у埗錛岃繖灝辮閲囩敤鎮茶閿併?br />
涓涓吀鍨嬬殑鍊氳禆鏁版嵁搴撶殑鎮茶閿佽皟鐢細 榪欐潯 sql 璇彞閿佸畾浜?account 琛ㄤ腑鎵鏈夌鍚堟绱㈡潯浠訛紙name=”Erica”錛夌殑璁板綍銆傛湰嬈′簨鍔℃彁浜や箣鍓嶏紙浜嬪姟鎻愪氦鏃朵細閲婃斁浜嬪姟榪囩▼涓殑閿侊級錛屽鐣屾棤娉曚慨鏀硅繖浜涜褰曘侶ibernate 鐨勬偛瑙傞攣錛屼篃鏄熀浜庢暟鎹簱鐨勯攣鏈哄埗瀹炵幇銆?br />
涓嬮潰鐨勪唬鐮佸疄鐜頒簡瀵規煡璇㈣褰曠殑鍔犻攣錛?br />
String hqlStr ="from TUser as user where user.name='John'";
Java鐖卞ソ鑰呯ぞ鍖?/strong>
鍏紬鍙峰唴瀹圭畝浠嬶細
甯屾湜鍏蟲敞鐨勪綘鍋滀笅鑴氭錛屽畾鏈夋墍鏀惰幏銆?/span>
浠ュ墠鐨勪釜浜哄崥瀹㈠唴瀹逛笉濂借縼縐伙紝鎵浠ユ垜鎵撶畻鏍規嵁鑷繁鐨勫騫寸粡楠岋紝涓嶆柇鏁寸悊杈撳嚭鏈変環鍊肩殑鍐呭銆?br />鐩墠鍏紬鍙峰唴瀹規湁鍏充簬SpringCloud錛堝井鏈嶅姟妗嗘灦錛夈丼kywalking錛圓PM鐩戞帶璋冪敤閾撅級銆丣VM錛圙C鍒嗘瀽銆佸唴瀛樻硠婕忓垎鏋愶級銆佸茍鍙戠紪紼?/span>鐩稿叧鍘熷垱瀹炴垬鏂囩珷宸插嚭鐐夈?br />
鏈榪戝垰寮濮嬪凡緇忔湁涓嶅皯灝忎紮浼村叧娉ㄤ簡錛屾湡寰呰兘鏈夊垢鎼滅儲鍒版湰鍗氬鐨勫悓瀛︼紝鍙互鎵爜鍏蟲敞涓涓嬶紝涓嶈儨鎰熸縺銆?br />澶у鏈変換浣曟妧鏈佽亴鍦恒侀潰璇曚笂鐨勯棶棰橀兘鍙互涓庢垜浜ゆ祦銆?br />
鏂瑰紡涓錛?/strong>鎵爜浠ヤ笅鍏紬鍙蜂簩緇寸爜錛?/strong>
鏂瑰紡浜岋細鍦ㄥ井淇′笂鐩存帴鎼滅儲錛?nbsp;javatech_cbo
鎰熻阿鍚勪綅灝忎紮浼寸殑鏀寔錛屽悗緇細鍦ㄨ鍏紬鍙蜂笂杈撳嚭澶ч噺鐨勬湁浠峰肩殑瀹炴垬騫茶揣 錛屾湡寰呬笌浣犱竴鍚岃繘姝ヤ笌鎴愰暱銆?br />
]]>
闇姹傛槸鏌ヨ緗戠粶淇℃伅錛岀綉緇滀俊鎭腑鍏寵仈浜嗗垎鍖恒佺綉緇淚P(1涓綉緇滐紞錛嶏籍涓綉緇滐緝錛?錛岀綉妗ヤ俊鎭?
HQL錛?br />
StringBuffer hql = new StringBuffer();
hql.append("select new Map(vn.id as id, vn.vnName as vnName, b.name as vnType, ");
hql.append("vn.vnIptype as vnIptype, vn.vnIp as vnIp,vn.vnNat as vnNat, vn.vnFlag as vnFlag, ");
hql.append("vn.vnetFlag as vnetFlag, (case when sum(vmvn.state)='0' then 0 else 1 end) as vnState, ");
hql.append("vn.useType as useType, vn.createTime as createTime, ");
hql .append("zone.oneName as oneName, zone.oneHypervisor as oneHypervisor, zone.oneSeq as oneSeq) ");
hql.append("from VnetTable vn, ZoneTable zone, BridgeTable b ");
hql.append("left join fetch vn.VnTables vmvn where zone.id = vn.zoneId and vn.vnType = b.id ");
... ...
hql.append(" group by vn.id");
if (vnState != null && !"".equals(vnState)) {
if ("0".equals(vnState)) {
hql.append(" having sum(vmvn.state)=0");
} else if ("1".equals(vnState)) {
hql.append(" having sum(vmvn.state)!=0");
}
}
}
hbm閰嶇疆鏂囦歡錛?br />
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.base.mapping.VnetTable" table="vnet_table" >
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="uuid.hex" />
</id>
<property name="vnId" type="java.lang.Long">
<column name="VN_ID">
<comment>鍒涘緩铏氭嫙緗戠粶鏃跺緱鍒扮殑ID</comment>
</column>
</property>
...
<property name="zoneId" type="java.lang.String">
<column name="ZONE_ID" length="32">
<comment>鍒嗗尯ID</comment>
</column>
</property>
<property name="vlanId" type="java.lang.Integer">
<column name="VLAN_ID">
<comment>VLAN_ID</comment>
</column>
</property>
<set name="vnTables" lazy="false" order-by="id asc" inverse="false" fetch="join" cascade="all">
<key column="VN_NID"/>
<one-to-many class="com.base.mapping.VnTable"/>
</set>
</class>
</hibernate-mapping>
鐞嗚Вinverse鍜宑ascade錛屽彲浠ュ弬鑰冿細http://blog.csdn.net/wkcgy/article/details/6271321
鎬葷粨錛?nbsp;
<one-to-many>涓紝寤鴻inverse=”true”錛岀敱“many”鏂規潵榪涜鍏寵仈鍏崇郴鐨勭淮鎶?nbsp;
<many-to-many>涓紝鍙緗叾涓竴鏂筰nverse=”false”錛屾垨鍙屾柟閮戒笉璁劇疆
Cascade錛岄氬父鎯呭喌涓嬮兘涓嶄細浣跨敤銆傜壒鍒槸鍒犻櫎錛屼竴瀹氳鎱庨噸銆?/span>
鎿嶄綔寤鴻
涓鑸many-to-one鍜宮any-to-many涓嶈緗駭鑱旓紝榪欒鐪嬩笟鍔¢昏緫鐨勯渶瑕?瀵筼ne-to-one鍜宱ne-to-many璁劇疆綰ц仈銆?/span>
many-to-many鍏寵仈鍏崇郴涓紝涓绔緗甶nverse=”false”錛屽彟涓绔緗負inverse=”true”銆傚湪one-to-many鍏寵仈鍏崇郴涓紝璁劇疆inverse=”true”,鐢卞绔潵緇存姢鍏崇郴琛?/span>
]]>
]]>
鍦ㄥ涓鎴風鍙兘璇誨彇鍚屼竴絎旀暟鎹垨鍚屾椂鏇存柊涓絎旀暟鎹殑鎯呭喌涓嬶紝闃叉鍚屼竴涓暟鎹淇敼鑰岄犳垚娣蜂貢錛屾渶綆鍗曠殑鎵嬫灝辨槸鍦ㄨ鍙栨椂瀵規暟鎹繘琛岄攣瀹氾紝鍏跺畠瀹㈡埛绔笉鑳藉鍚屼竴絎旀暟鎹繘琛屾洿鏂扮殑璇誨彇鍔ㄤ綔銆?/p>
select * from account where name=”John” for update
Query query = session.createQuery(hqlStr);
query.setLockMode("user",LockMode.UPGRADE); // 鍔犻攣
List userList = query.list();// 鎵ц鏌ヨ錛岃幏鍙栨暟鎹?/font>
query.setLockMode 瀵規煡璇㈣鍙ヤ腑錛岀壒瀹氬埆鍚嶆墍瀵瑰簲鐨勮褰曡繘琛屽姞閿侊紙鎴戜滑涓篢User 綾繪寚瀹氫簡涓涓埆鍚?“user” 錛夛紝榪欓噷涔熷氨鏄榪斿洖鐨勬墍鏈?user 璁板綍榪涜鍔犻攣銆?br />
瑙傚療榪愯鏈?Hibernate 鐢熸垚鐨?SQL 璇彞錛?br />
select tuser0_.id as id, tuser0_.name as name, tuser0_.group_id
as group_id, tuser0_.user_type as user_type, tuser0_.sex as sex
from t_user tuser0_ where (tuser0_.name='John' ) for update
榪欓噷 Hibernate 閫氳繃浣跨敤鏁版嵁搴撶殑 for update 瀛愬彞瀹炵幇浜嗘偛瑙傞攣鏈哄埗銆?br />
Hibernate 鐨勫姞閿佹ā寮忔湁錛?br />
LockMode.NONE 錛?/strong> 鏃犻攣鏈哄埗銆?br />
LockMode.WRITE 錛?/strong> Hibernate 鍦?Insert 鍜?Update 璁板綍鐨勬椂鍊欎細鑷姩鑾峰彇銆?br />
LockMode.READ 錛?/strong> Hibernate 鍦ㄨ鍙栬褰曠殑鏃跺欎細鑷姩鑾峰彇銆?br />
浠ヤ笂榪欎笁縐嶉攣鏈哄埗涓鑸敱 Hibernate 鍐呴儴浣跨敤錛屽 Hibernate 涓轟簡淇濊瘉 Update榪囩▼涓璞′笉浼氳澶栫晫淇敼錛屼細鍦?save 鏂規硶瀹炵幇涓嚜鍔ㄤ負鐩爣瀵硅薄鍔犱笂 WRITE 閿併?br />
LockMode.UPGRADE 錛?/strong>鍒╃敤鏁版嵁搴撶殑 for update 瀛愬彞鍔犻攣銆?br />
LockMode. UPGRADE_NOWAIT 錛?/strong> Oracle 鐨勭壒瀹氬疄鐜幫紝鍒╃敤 Oracle 鐨?for update nowait 瀛愬彞瀹炵幇鍔犻攣銆?br />
涓婇潰榪欎袱縐嶉攣鏈哄埗鏄垜浠湪搴旂敤灞傝緝涓哄父鐢ㄧ殑錛屽姞閿佷竴鑸氳繃浠ヤ笅鏂規硶瀹炵幇錛?br />
Criteria.setLockMode
Query.setLockMode
Session.lock
娉ㄦ剰錛屽彧鏈夊湪鏌ヨ寮濮嬩箣鍓嶏紙涔熷氨鏄?Hiberate 鐢熸垚 SQL 涔嬪墠錛夎瀹氬姞閿侊紝鎵嶄細鐪熸閫氳繃鏁版嵁搴撶殑閿佹満鍒惰繘琛屽姞閿佸鐞嗭紝鍚﹀垯錛屾暟鎹凡緇忛氳繃涓嶅寘鍚?for update 瀛愬彞鐨?Select SQL 鍔犺澆榪涙潵錛屾墍璋撴暟鎹簱鍔犻攣涔熷氨鏃犱粠璋堣搗銆?br />
]]>
/* private CertBindView getCertBindView(Certbind view) { import org.apache.log4j.Logger; import com.cns.certservice.exception.DAOException; private HibernateTemplate() { } /** /** /** /** /** /** } /** } /** } /** } /** } /**
]]>
]]>
List<CertInfoListView> liinfo = new ArrayList<CertInfoListView>();
String sql="select b.usercode,b.agentcode,i.sn,i.cert,i.certstate,i.endtime, i.ipasskeyno,b.id,b.addtime from certbind b inner join certinfo i on b.ipasskeyno=i.ipasskeyno";
Session session = HibernateTemplate.getInstance().getSession();
StringBuffer sb = new StringBuffer(sql);
sb.append(" where 1=1 ");
if(!CheckEmpty.isEmpty(view.getIpasskeyno())){
sb.append(" and i.ipasskeyno ='").append(view.getIpasskeyno()).append("'");
}
if(!CheckEmpty.isEmpty(view.getAgentcode())){
sb.append(" and b.agentcode ='").append(view.getAgentcode()).append("'");
}
if(!CheckEmpty.isEmpty(view.getSn())){
sb.append(" and i.sn ='").append(view.getSn()).append("'");
}
if(!CheckEmpty.isEmpty(view.getUsercode())){
sb.append(" and b.usercode ='").append(view.getUsercode()).append("'");
}
if(view.getAddtime()!=null){
sb.append(" and b.addtime=").append(view.getAddtime());
}
sb.append(" order by ipasskeyno ,addtime desc");
Query q = session.createSQLQuery(sb.toString());
int pageno = view.getPageno();
int size = view.getPagesize();
if(pageno!=0&&size!=0){
q.setFirstResult((pageno-1)*size);
q.setMaxResults(size);
}
List list = q.list();
Iterator it = list.iterator();
while(it.hasNext()){
Object[] obj = (Object[])it.next();
CertInfoListView c = new CertInfoListView();
for(int i=0;i<obj.length;i++){
if(!CheckEmpty.isEmpty((String)obj[0])){
c.setUsercode((String)obj[0]);
}
if(!CheckEmpty.isEmpty((String)obj[1])){
c.setAgentcode((String)obj[1]);
}
if(!CheckEmpty.isEmpty((String)obj[2])){
c.setSn((String)obj[2]);
}
if(obj[3]!=null){
SerializableClob sc = (SerializableClob)obj[3];
String cc = null;
if(sc!=null){
try {
cc = sc.getSubString(1, (int)sc.length());
} catch (SQLException e) {
}
}
if(cc!=null)c.setCert(cc);
}
if(obj[4]!=null){
BigDecimal b = (BigDecimal)obj[4];
c.setCertstate(b.intValue());
}
if((obj[5])!=null){
c.setEndtime(((Date)obj[5]));
}
if(!CheckEmpty.isEmpty((String)obj[6])){
c.setIpasskeyno((String)obj[6]);
}
c.setCertbindid(Integer.parseInt((String)obj[7]));
c.setAddtime((Date)obj[8]);
}
liinfo.add(c);
}
if(session!=null&&session.isConnected())session.close();
return liinfo;
}
瀹炵幇鍒嗛〉鐨凞AO
/*
* (non-Javadoc)
*
* @see com.cns.certservice.dao.CertBindTabDao#getCertBindList(com.cns.certservice.vo.CertBindView)
*/
public List<CertBindView> getCertBindList(CertBindView view)
throws DAOException {
List<CertBindView> li = null;
List<Certbind> lic = null;
Session session = null;
try {
session = HibernateTemplate.getInstance().getSession();
Criteria cri = session.createCriteria(Certbind.class);
Certbind c = getCertBind(view);
if (c.getAddtime() != null) {
cri.add(Expression.eq("addtime", c.getAddtime()));
}
if (!CheckEmpty.isEmpty(c.getAgentcode())) {
cri.add(Expression.eq("agentcode", c.getAgentcode()));
}
if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
}
if (!CheckEmpty.isEmpty(c.getUsercode())) {
cri.add(Expression.eq("usercode", c.getUsercode()));
}
if (!CheckEmpty.isEmpty(c.getExtend1())) {
cri.add(Expression.eq("extend1", c.getExtend1()));
}
if (!CheckEmpty.isEmpty(c.getExtend2())) {
cri.add(Expression.eq("extend2", c.getExtend2()));
}
if (c.getAgenttype() != 0) {
cri.add(Expression.eq("agenttype", c.getAgenttype()));
}
int pageno = view.getPageno();
int size = view.getPagesize();
if(pageno!=0&&size!=0){
cri.setFirstResult((pageno-1)*size);
cri.setMaxResults(size);
}
lic = cri.list();
if (lic != null) {
li = new ArrayList<CertBindView>();
for (Certbind cer : lic) {
CertBindView v = getCertBindView(cer);
li.add(v);
}
}
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return li;
}
public int getInfoListCount(CertBindView view) throws DAOException {
int count = 0;
Session session = null;
try {
session = HibernateTemplate.getInstance().getSession();
Criteria cri = session.createCriteria(Certbind.class);
Certbind c = getCertBind(view);
if (c.getAddtime() != null) {
cri.add(Expression.eq("addtime", c.getAddtime()));
}
if (!CheckEmpty.isEmpty(c.getAgentcode())) {
cri.add(Expression.eq("agentcode", c.getAgentcode()));
}
if(c.getId()!=0)cri.add(Expression.eq("id", c.getId()));
if (!CheckEmpty.isEmpty(c.getIpasskeyno())) {
cri.add(Expression.eq("ipasskeyno", c.getIpasskeyno()));
}
if (!CheckEmpty.isEmpty(c.getUsercode())) {
cri.add(Expression.eq("usercode", c.getUsercode()));
}
if (!CheckEmpty.isEmpty(c.getExtend1())) {
cri.add(Expression.eq("extend1", c.getExtend1()));
}
if (!CheckEmpty.isEmpty(c.getExtend2())) {
cri.add(Expression.eq("extend2", c.getExtend2()));
}
if (c.getAgenttype() != 0) {
cri.add(Expression.eq("agenttype", c.getAgenttype()));
}
count = (Integer)cri.setProjection(Projections.rowCount()).uniqueResult();
cri.setProjection(null);
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return count;
}
* (non-Javadoc)
*
* @see com.cns.certservice.dao.CertBindTabDao#updateCertBind(com.cns.certservice.vo.CertBindView)
*/
public boolean updateCertBind(CertBindView view) throws DAOException {
boolean res = false;
if (view.getId()==0) {
throw new DAOException("getId from CertBindView is null!");
}
Session session= null;
try {
session = HibernateTemplate.getInstance().getSession();
Certbind c = (Certbind) session.get(Certbind.class, view.getId());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
res = HibernateTemplate.getInstance().updateObject(c);
} catch (HibernateException e) {
log.error(e,e);
throw new DAOException(e);
}finally{
if (session != null && session.isConnected())
session.close();
}
return res;
}
private Certbind getCertBind(CertBindView view) {
Certbind c = new Certbind();
if (view.getAddtime() != null)
c.setAddtime(view.getAddtime());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if(view.getId()!=0)c.setId(view.getId());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
return c;
}
CertBindView c = new CertBindView();
if (view.getAddtime() != null)
c.setAddtime(view.getAddtime());
if (!CheckEmpty.isEmpty(view.getAgentcode()))
c.setAgentcode(view.getAgentcode());
if (!CheckEmpty.isEmpty(view.getExtend1()))
c.setExtend1(view.getExtend1());
if (!CheckEmpty.isEmpty(view.getExtend2()))
c.setExtend2(view.getExtend2());
if (!CheckEmpty.isEmpty(view.getIpasskeyno()))
c.setIpasskeyno(view.getIpasskeyno());
if (!CheckEmpty.isEmpty(view.getUsercode()))
c.setUsercode(view.getUsercode());
if(view.getId()!=0)c.setId(view.getId());
if (view.getAgenttype() != 0)
c.setAgenttype(view.getAgenttype());
return c;
}
]]>
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateTemplate {
* static final session factory
*/
private static SessionFactory sessionFactory = null;
* local thread variable used for storing share session instance
*/
private static final ThreadLocal localSession = new ThreadLocal();
* log4j logger
*/
private static final Logger logger = Logger
.getLogger(HibernateTemplate.class);
/**
* use JTA transaction
*/
/**
* 璇ュ伐鍏峰敮涓瀹炰緥銆?br />
*/
private static HibernateTemplate instance = null;
private static Transaction tx = null;
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final Configuration cfg = new Configuration();
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
* 鑾峰彇鎸佷箙宸ュ叿鐨勫敮涓瀹炰緥錛屼互鍚庝笉鏄嬌鐢ㄥ崟瀹炰緥妯″紡錛岃屼笉鏄噰鐢ㄥ璞℃睜鏀寔銆?br />
* @return銆PersistentTool
* @throws BaseException
*/
public synchronized static HibernateTemplate getInstance() {
if (instance == null) {
instance = new HibernateTemplate();
instance.initHibernate();
}
return instance;
}
* 瀹炵幇Hibernate鐨勫垵濮嬪寲閰嶇疆鐜銆?br />
*/
public void initHibernate() {
try {
//姝ゅ浠庣郴緇熻礬寰勪腑鑾峰彇閰嶇疆鏂囦歡
cfg.configure(CONFIG_FILE_LOCATION);
} catch (HibernateException ex) {
ex.printStackTrace();
}
try {
// 瑁呰澆閰嶇疆錛屾瀯閫燬essionFactory瀵硅薄
sessionFactory = cfg.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
* Get the share session
* @
* @return Session share session
*/
public Session getSession() {
logger.debug("Now enter into getSession method of DaoUtil");
//obtain share session
Session session = (Session) localSession.get();
try {
if (session == null||!session.isOpen()) {
//get session by session factory
session = sessionFactory.openSession();
localSession.set(session);
}
} catch (HibernateException ex) {
ex.printStackTrace();
return session;
}
* Close share session
* @
*/
public void close() {
logger.debug("Now enter into closeSessionl");
//obtain share session
Session session = (Session) localSession.get();
localSession.set(null);
if (session != null) {
try {
session.flush();
session.close();
} catch (HibernateException ex) {
ex.printStackTrace();
}
}
* Begin JTA transaction
* @
*/
public void beginTransaction() {
logger.debug("Now enter into beginTransaction");
try {
Session session = (Session) localSession.get();
tx = session.beginTransaction();
} catch (Exception ex) {
ex.printStackTrace();
}
* Commit transaction
* @
*/
public void commitTransaction() {
try {
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
}
* Rollback transaction when breaching ACID operation
* @
*/
public void rollbackTransaction() {
try {
tx.rollback();
} catch (Exception ex) {
ex.printStackTrace();
}
* Insert a record into table
* @param obj Object
* @throws DAOException
* @
*/
public int insertObject(Object obj) throws DAOException {
int res = 0;
logger.debug("Now enter into insertObject");
//obtain current share session
try {
Session session = HibernateTemplate.getInstance().getSession();
beginTransaction();
Object robj = session.save(obj);
if (robj instanceof Integer) {
res = (Integer) robj;
}
if (robj instanceof String) {
res =1;
}
session.flush();
} catch (HibernateException ex) {
rollbackTransaction();
logger.error("insertObject error:", ex);
throw new DAOException(ex);
} finally {
commitTransaction();
close();
}
return res;
}
/**
* Delete a record of database table by Hibernate po object
* @param obj Object
* @throws DAOException
* @
*/
public boolean deleteObject(Object obj) throws DAOException {
boolean res = false;
logger.debug("Now enter into deleteObject method");
//obtain current share session
try {
Session session = HibernateTemplate.getInstance().getSession();
beginTransaction();
session.delete(obj);
session.flush();
res = true;
} catch (HibernateException ex) {
rollbackTransaction();
logger.error("deleteObject error:", ex);
throw new DAOException(ex);
} finally {
commitTransaction();
close();
}
return res;
}
/**
* Update a record of database table
* @param ob Object
* @throws DAOException
* @
*/
public boolean updateObject(Object ob) throws DAOException {
boolean res = false;
logger.debug("Now enter into updateObject");
//obtain current share session
try {
Session session = HibernateTemplate.getInstance().getSession();
beginTransaction();
session.update(ob);
session.flush();
res= true;
} catch (HibernateException ex) {
rollbackTransaction();
logger.error("updateObject error:", ex);
throw new DAOException(ex);
} finally {
commitTransaction();
close();
}
return res;
}
}
]]>
]]>
]]>