锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
getCurrentSession() :
The "current session" refers to a hibernate Session bound by hibernate behind the scenes, to the transaction scope.
A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends.
It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs.
To enable this strategy in your hibernate configuration:
set hibernate.transaction.manager_lookup_class to a lookup strategy for your JEE container
set hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory
Only the Session that you obtained with sf.getCurrentSession() is flushed and closed automatically.
Example :
try {
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
tx.begin();
// Do some work
sf.getCurrentSession().createQuery(...);
sf.getCurrentSession().persist(...);
tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
openSession() :
If you decide to use manage the Session yourself the go for sf.openSession() , you have to flush() and close() it.
It does not flush and close() automatically.
Example :
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
Session session = factory.openSession();
try {
tx.begin();
// Do some work
session.createQuery(...);
session.persist(...);
session.flush(); // Extra work you need to do
tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}
finally {
session.close(); // Extra work you need to do
}
]]>
]]>
鍙淇敼User.java
/**
聽聽聽聽 * @return Returns the enabled.
聽聽聽聽 * @hibernate.property column="enabled"聽
聽聽聽聽 */
聽聽聽 public Boolean getEnabled() {
聽聽聽聽聽聽聽 // isEnabled doesnt' work for copying properties to Struts ActionForms
聽聽聽聽聽聽聽 return enabled;
聽聽聽 }
淇敼鎴?br />
/**
聽聽聽聽 * @return Returns the enabled.
聽聽聽聽 * @hibernate.property column="enabled" type="yes_no"
聽聽聽聽 */
聽聽聽 public Boolean getEnabled() {
聽聽聽聽聽聽聽 // isEnabled doesnt' work for copying properties to Struts ActionForms
聽聽聽聽聽聽聽 return enabled;
聽聽聽 }
灝卞姞浜嗚繖涓鐐癸紝灝卞彲浠ヤ簡錛?br />
鍏跺疄 type="yes_no" hibernate浼氭槧灝勬垚CHAR(1) , 鑰宼ype="boolean" 瀹冧細鏄犲皠鎴怋IT , 浼拌dbunit鍦ㄦ彃鍏ample-data.xml鐨勬椂鍊欎細鍑洪敊銆?
]]>
聽聽聽聽聽聽聽聽聽聽聽聽//鐢熸垚鍚屾浠ょ墝閬垮厤琛ㄥ崟閲嶅鎻愪氦
聽聽聽聽聽聽saveToken(request);聽聽
聽聽聽聽聽聽return mapping.findForward(IConstants.SUCCESS_KEY);
聽聽聽聽聽 InsertAction.java
聽聽聽聽聽聽
聽聽聽聽聽聽NewsModel newsModel = new NewsModel();
聽聽聽聽聽//瀵硅薄鍙嶅皠鏈哄埗
聽聽聽聽聽BeanUtils.copyProperties(newsModel,newsForm);
聽聽聽聽聽newsModel.setNewsPublishTime(DateUtil.getCurrentTime());
聽聽聽聽聽newsModel.setNewsKind(newsForm.getNewsKind());
聽聽聽聽聽newsModel.setNewsContent(content);
聽聽聽聽聽//鍥劇墖鏂囦歡璺緞
聽聽聽聽聽if(imageFile!=null&&!imageFile.equals("")){聽聽聽
聽聽聽聽聽聽newsModel.setImagePath("/UploadFiles/"+imageFile);
聽聽聽聽聽}
聽聽聽聽聽//灝忓浘鐗囨枃浠惰礬寰?br />聽聽聽聽聽/**if(iconFile!=null&&!iconFile.equals("")){聽聽聽
聽聽聽聽聽聽newsModel.setIconPath("/UploadFiles/"+iconFile);
聽聽聽聽聽}*/
聽聽聽聽聽//鏁版嵁搴撴柟闈㈢殑澶勭悊#鎻掑叆涓鏉℃柊闂?br />聽聽聽聽聽INewsDao newsDao = new JdbcNewsDaoImpl(con);
聽聽聽聽聽newsDao.insertNews(newsModel);
聽聽聽聽}else{
聽聽聽聽聽//琛ㄥ崟閲嶅鎻愪氦
聽聽聽聽聽if(log.isDebugEnabled()){
聽聽聽聽聽聽log.debug("琛ㄥ崟閲嶅鎻愪氦");
聽聽聽聽聽}
聽聽聽聽}
聽聽聽