當前位置: 首頁>>代碼示例>>Java>>正文


Java Transaction類代碼示例

本文整理匯總了Java中org.hibernate.Transaction的典型用法代碼示例。如果您正苦於以下問題:Java Transaction類的具體用法?Java Transaction怎麽用?Java Transaction使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Transaction類屬於org.hibernate包,在下文中一共展示了Transaction類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: updateValueOfKey

import org.hibernate.Transaction; //導入依賴的package包/類
@Override
public boolean updateValueOfKey(Constant constant) { 
		Session session = HibernateUtils.getSession();
		 Transaction tx = session.beginTransaction();
		 boolean isUpdate = false;
		 try {
			Query query = session.createQuery("update Constant c set c.value =? where c.id = ?");  
		    query.setString(0,constant.getValue());
		    query.setInteger(1, constant.getId()); 
			int temp = query.executeUpdate();  
			tx.commit();
			if (temp>0) {
				isUpdate = true;
			}
		} catch (Exception e) {
			 e.printStackTrace();
			 tx.rollback();
		}finally {
			HibernateUtils.closeSession();
		}   
		 return isUpdate;
}
 
開發者ID:codekongs,項目名稱:ImageClassify,代碼行數:23,代碼來源:ConstantService.java

示例2: approveUser

import org.hibernate.Transaction; //導入依賴的package包/類
@Override
public boolean approveUser(String username) {
	Boolean bool = false;
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	User myUser = udi.getUserByUsername(username);
	try {
		tx = session.beginTransaction();
		myUser.setApproved(true);
		session.update(myUser);
		tx.commit();
		bool = true;
		System.out.println("User: '" + username + "' successfully approved.");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
	} finally {
		session.close();
	}
	return bool;
}
 
開發者ID:grantleymorrison,項目名稱:FlashBoard,代碼行數:23,代碼來源:AdminDaoImpl.java

示例3: updateMarkAll

import org.hibernate.Transaction; //導入依賴的package包/類
@Override
public boolean updateMarkAll(Mark mark) {
	 Session session = HibernateUtils.getSession();
	 Transaction tx = session.beginTransaction();
	 boolean b = false;
	 try {
		Query query = session.createQuery("update Mark m set m.mark_accuracy =?,m.mark_time =?,m.option_mark_name =?,"
				+ "m.manual_mark_name=? where m.user_id = ? and m.img_id = ?");  
	    query.setString(0, mark.getMark_accuracy());
	    query.setTimestamp(1, mark.getMark_time());
	    query.setString(2,mark.getOption_mark_name());
	    query.setString(3, mark.getManual_mark_name()); 
	    query.setInteger(4, mark.getUser_id());
	    query.setInteger(5, mark.getImg_id()); 
		query.executeUpdate();  
		tx.commit();
		b = true;
	} catch (Exception e) {
		 e.printStackTrace();
		 tx.rollback(); 
	}finally {
		HibernateUtils.closeSession();
	} 
	 return b;
}
 
開發者ID:codekongs,項目名稱:ImageClassify,代碼行數:26,代碼來源:MarkService.java

示例4: main

import org.hibernate.Transaction; //導入依賴的package包/類
public static void main(String[] args) {
	Configuration cfg=null;
	SessionFactory factory=null;
	Session ses=null;
	 Transaction tx=null;
			cfg=new Configuration().configure("com/app/cfgs/hibernate.cfg.xml");
			factory=cfg.buildSessionFactory();
		ses=factory.openSession();
	
		tx=ses.beginTransaction();
		 String hql="delete from bigbazarModel where bazarid=:id";
		 Query q=ses.createQuery(hql);
		 		q.setParameter("id", 1003);
		 			int c=q.executeUpdate();
		 			//int count=Integer.parseUnsignedInt(c);
		 			tx.commit();
		 			System.out.println("\t\t"+c+" rows Deleted");
		 		
		 			factory.close();

}
 
開發者ID:pratikdimble,項目名稱:Hibernate_HQL_UniqueResult_ExecuteUpdate_CopyData_Delete_Update,代碼行數:22,代碼來源:delete_HQL.java

示例5: main

import org.hibernate.Transaction; //導入依賴的package包/類
public static void main(String[] args) {
	Configuration cfg=null;
	SessionFactory factory=null;
	Session ses=null;
	 Transaction tx=null;
			cfg=new Configuration().configure("com/app/cfgs/hibernate.cfg.xml");
			factory=cfg.buildSessionFactory();
		ses=factory.openSession();
	
		tx=ses.beginTransaction();
		 String hql="insert into newMall(mallid,item_name,item_price,item_quantity) "
		 			+ "select bazarid,item_name,item_price,item_quantity from bigbazarModel ";
		 Query q=ses.createQuery(hql);
		 			int c=q.executeUpdate();
		 			//int count=Integer.parseUnsignedInt(c);
		 			tx.commit();
		 			System.out.println("\t\t"+c+" rows Copied Successfully...");
		 		
		 			factory.close();

}
 
開發者ID:pratikdimble,項目名稱:Hibernate_HQL_UniqueResult_ExecuteUpdate_CopyData_Delete_Update,代碼行數:22,代碼來源:copy_HQL.java

示例6: main

import org.hibernate.Transaction; //導入依賴的package包/類
public static void main(String[] args) {
	Configuration cfg=null;
	SessionFactory factory=null;
	Session ses=null;
	 Transaction tx=null;
			cfg=new Configuration().configure("com/app/cfgs/hibernate.cfg.xml");
			factory=cfg.buildSessionFactory();
		ses=factory.openSession();
	
		tx=ses.beginTransaction();
		 String hql="update bigbazarModel set item_price=:price where bazarid=:id";
		 Query q=ses.createQuery(hql);
		 		q.setParameter("id", 1002);
		 		q.setParameter("price", 60.0f);
		 			int c=q.executeUpdate();
		 			//int count=Integer.parseUnsignedInt(c);
		 			tx.commit();
		 			System.out.println("\t\t"+c+" rows Updated");
		 		
		 			factory.close();

}
 
開發者ID:pratikdimble,項目名稱:Hibernate_HQL_UniqueResult_ExecuteUpdate_CopyData_Delete_Update,代碼行數:23,代碼來源:update_HQL.java

示例7: saveCatogory

import org.hibernate.Transaction; //導入依賴的package包/類
/**
 * 持久化category對象
 */
@Override
public boolean saveCatogory(Category category) {
	Session session = HibernateUtils.getSession(); // 生成session實例
	Transaction tx = session.beginTransaction(); // 創建transaction實例
	try {
		session.save(category); // 使用session的sava方法將持久化對象保存到數據庫中
		tx.commit(); // 提交事務
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback(); // 回滾事務
		return false;
	} finally {
		HibernateUtils.closeSession(); // 關閉Session實例
	}
}
 
開發者ID:codekongs,項目名稱:ImageClassify,代碼行數:20,代碼來源:CategoryService.java

示例8: unassign

import org.hibernate.Transaction; //導入依賴的package包/類
public String unassign(String managerExternalId, Session hibSession) {
    Transaction tx = null;
    try {
        if (hibSession.getTransaction()==null || !hibSession.getTransaction().isActive())
            tx = hibSession.beginTransaction();
        
        ExamAssignment oldAssignment = new ExamAssignment(this);
        
        setAssignedPeriod(null);
        if (getAssignedRooms()==null) setAssignedRooms(new HashSet());
        getAssignedRooms().clear();
        setAssignedPreference(null);
        
        HashSet otherExams = new HashSet();
        
        for (Iterator j=getConflicts().iterator();j.hasNext();) {
            ExamConflict conf = (ExamConflict)j.next();
            for (Iterator i=conf.getExams().iterator();i.hasNext();) {
                Exam x = (Exam)i.next();
                if (!x.equals(this)) {
                    x.getConflicts().remove(conf);
                    otherExams.add(x);
                }
            }
            hibSession.delete(conf);
            j.remove();
        }

        ExamEvent event = getEvent();
        if (event!=null) hibSession.delete(event);
        
        hibSession.update(this);
        for (Iterator i=otherExams.iterator();i.hasNext();)
            hibSession.update((Exam)i.next());
        
        SubjectArea subject = null;
        Department dept = null;
        for (Iterator i=new TreeSet(getOwners()).iterator();i.hasNext();) {
            ExamOwner owner = (ExamOwner)i.next();
            subject = owner.getCourse().getSubjectArea();
            dept = subject.getDepartment();
            break;
        }
        
        ChangeLog.addChange(hibSession,
                TimetableManager.findByExternalId(managerExternalId),
                getSession(),
                this,
                getName()+" ("+
                (oldAssignment.getPeriod()==null?"N/A":oldAssignment.getPeriodAbbreviation()+" "+oldAssignment.getRoomsName(", "))+
                " → N/A)",
                ChangeLog.Source.EXAM_INFO,
                ChangeLog.Operation.UNASSIGN,
                subject,
                dept);

        if (tx!=null) tx.commit();
        return null;
    } catch (Exception e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
        return "Unassignment of "+getName()+" failed, reason: "+e.getMessage();
    }
}
 
開發者ID:Jenner4S,項目名稱:unitimes,代碼行數:65,代碼來源:Exam.java

示例9: save

import org.hibernate.Transaction; //導入依賴的package包/類
protected void save(final Session session, final Object entity) throws HibernateException {

       Transaction transaction = null;

       try {
           transaction = session.beginTransaction();
           session.save(entity);
           transaction.commit();
       } catch (HibernateException hibernateException) {
           
           if (transaction != null)
               transaction.rollback();
               
           logger.error(fatal, hibernateException.toString());
           throw hibernateException;
       } 
       
   }
 
開發者ID:will-gilbert,項目名稱:OSWf-OSWorkflow-fork,代碼行數:19,代碼來源:HibernateStore.java

示例10: cleanupOnlineSectioningLog

import org.hibernate.Transaction; //導入依賴的package包/類
public static void cleanupOnlineSectioningLog(int days) {
	if (days < 0) return;
	org.hibernate.Session hibSession = new _RootDAO().createNewSession();
	Transaction tx = null;
	try {
		tx = hibSession.beginTransaction();
		int rows = hibSession.createQuery(
				"delete from OnlineSectioningLog where timeStamp < " + HibernateUtil.addDate("current_date()", ":days")
				).setInteger("days", - days).executeUpdate();
		if (rows > 0)
			sLog.info("All records older than " + days + " days deleted from the online sectioning log (" + rows + " records).");
		tx.commit();
	} catch (Throwable t) {
		sLog.warn("Failed to cleanup query log: " + t.getMessage(), t);
		if (tx != null) tx.rollback();
	} finally {
		hibSession.close();
	}
}
 
開發者ID:Jenner4S,項目名稱:unitimes,代碼行數:20,代碼來源:LogCleaner.java

示例11: saveImageCategory

import org.hibernate.Transaction; //導入依賴的package包/類
@Override
public boolean saveImageCategory(Image_Category image_Category) {
	Session session = HibernateUtils.getSession(); //生成session實例
    Transaction tx = session.beginTransaction();  //創建transaction實例
     
    try {   
        session.save(image_Category); 
        tx.commit();            //提交事務 
        return true;
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();          //回滾事務 
		return false;
	}finally {
	  HibernateUtils.closeSession();  //關閉Session實例
	} 
}
 
開發者ID:codekongs,項目名稱:ImageClassify,代碼行數:18,代碼來源:ImageService.java

示例12: testUserCreation

import org.hibernate.Transaction; //導入依賴的package包/類
public static void testUserCreation() {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	
	User newUser = new User("jef", "jeff", "jeff", "jeff", "jeff");
	//SQLIntegrityConstraintViolationException
	try {
		tx = session.beginTransaction();
		session.save(newUser);
		tx.commit();
		System.out.println("User: '" + newUser.getUsername() + "' has been successfully created!");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
		System.out.println("User creation failed!");
		he.printStackTrace();
		
	} finally {
		session.close();
	}
}
 
開發者ID:grantleymorrison,項目名稱:FlashBoard,代碼行數:23,代碼來源:Driver2.java

示例13: remove

import org.hibernate.Transaction; //導入依賴的package包/類
protected void remove(Address addr) {
  	if (!ClusterDiscoveryDAO.isConfigured()) return;
  	org.hibernate.Session hibSession = ClusterDiscoveryDAO.getInstance().createNewSession();
String own_address = addressAsString(addr);
Transaction tx = null;
      try {
      	tx = hibSession.beginTransaction();
      	ClusterDiscovery cluster = ClusterDiscoveryDAO.getInstance().get(new ClusterDiscovery(own_address, cluster_name), hibSession);
      	if (cluster != null)
      		hibSession.delete(cluster);
      	hibSession.flush();
          if (tx != null) tx.commit();
      } catch (Exception e) {
	if (tx != null) tx.rollback();
	log.info("Failed to delete data for cluster " + cluster_name + ": " + e.getMessage());
} finally {
	hibSession.close();
}
  }
 
開發者ID:Jenner4S,項目名稱:unitimes,代碼行數:20,代碼來源:UniTimeClusterDiscovery.java

示例14: insertQuiz

import org.hibernate.Transaction; //導入依賴的package包/類
public Integer insertQuiz(Quiz quiz) {
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	Integer quizId = null;
	
	try {
		// Attemps to add and get quiz Id back
		tx = session.beginTransaction();
		quizId = (Integer) session.save(quiz);
		System.out.println(quizId);
		tx.commit();
	} catch (HibernateException e) {
		if (tx != null) {
			// Roll back IF transaction fails
			tx.rollback();
		}
		e.printStackTrace();
	} finally {
		session.close();
	}
	
	return quizId;
}
 
開發者ID:grantleymorrison,項目名稱:FlashBoard,代碼行數:24,代碼來源:QuizDaoImpl.java

示例15: promoteUserToWriter

import org.hibernate.Transaction; //導入依賴的package包/類
@Override
public boolean promoteUserToWriter(String username) {
	Boolean bool = false;
	Session session = HibernateUtil.getSession();
	Transaction tx = null;
	User myUser = udi.getUserByUsername(username);
	try {
		tx = session.beginTransaction();
		myUser.setRoleFlag(2);
		session.save(myUser);
		tx.commit();
		bool = true;
		System.out.println("User: '" + username + "' successfully approved.");
	} catch (HibernateException he) {
		if (tx != null) {
			tx.rollback();
		}
	} finally {
		session.close();
	}
	return bool;
}
 
開發者ID:grantleymorrison,項目名稱:FlashBoard,代碼行數:23,代碼來源:AdminDaoImpl.java


注:本文中的org.hibernate.Transaction類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。