本文整理汇总了Java中org.hibernate.Session.beginTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java Session.beginTransaction方法的具体用法?Java Session.beginTransaction怎么用?Java Session.beginTransaction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.Session
的用法示例。
在下文中一共展示了Session.beginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: approveUser
import org.hibernate.Session; //导入方法依赖的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;
}
示例2: saveComp
import org.hibernate.Session; //导入方法依赖的package包/类
/**
* 持久化composition
*/
@Override
public boolean saveComp(Composition composition) {
Session session = HibernateUtils.getSession(); //生成session实例
Transaction tx = session.beginTransaction(); //创建transaction实例
try {
session.save(composition); //使用session的sava方法将持久化对象保存到数据库中
tx.commit(); //提交事务
return true;
} catch (Exception e) {
e.printStackTrace();
tx.rollback(); //回滚事务
return false;
}finally {
HibernateUtils.closeSession(); //关闭Session实例
}
}
示例3: listar
import org.hibernate.Session; //导入方法依赖的package包/类
public List listar() {
Session session = DAOHibernateUtil.getSession();
List listaCliente = null;
try {
session.beginTransaction();
listaCliente = session.createQuery("From Cliente").list();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
finally{
session.close();
return listaCliente;
}
}
示例4: findAdminByToken
import org.hibernate.Session; //导入方法依赖的package包/类
/**
*
*/
@Override
public Admin findAdminByToken(String admin_token) {
Admin admin=null;
Session session=HibernateUtils.getSession();//生成Session实例
Transaction tx=session.beginTransaction();//生成事务实例
try {
admin=(Admin) session.createCriteria(Admin.class).add(Restrictions.eq("admin_token", admin_token)).uniqueResult();
tx.commit();
//调用session的get()方法,找到此用户到内存中
} catch (Exception e) {
e.printStackTrace();
tx.rollback();//事务回滚
}finally{
HibernateUtils.closeSession();//关闭session实例
}
return admin;
}
示例5: findCategoryById
import org.hibernate.Session; //导入方法依赖的package包/类
/**
* 根据类别id找到类别对象
*/
@Override
public Category findCategoryById(int category_id) {
Category category = null;
Session session = HibernateUtils.getSession();// 生成Session实例
Transaction tx = session.beginTransaction();// 生成事务实例
try {
category = (Category) session.get(Category.class, category_id);
tx.commit();// 提交事务
// 调用session的get()方法,找到此用户到内存中
} catch (Exception e) {
e.printStackTrace();
tx.rollback();// 事务回滚
} finally {
HibernateUtils.closeSession();// 关闭session实例
}
return category;
}
示例6: getAmountContributeOfAllByUser
import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public int getAmountContributeOfAllByUser() {
Session session = HibernateUtils.getSession();
Transaction tx = session.beginTransaction();
String sql = "SELECT id,upload_img_time,upload_img_review_status,username,tel_num FROM user_contribute_img WHERE upload_img_review_status=0 ORDER BY upload_img_time ASC";
List<Contribute_Img > cImgs = new ArrayList<Contribute_Img>();
try {
cImgs = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtils.closeSession(); // 关闭Session实例
}
return cImgs.size();
}
示例7: batchIn1TransactionToApp
import org.hibernate.Session; //导入方法依赖的package包/类
private Transaction batchIn1TransactionToApp(Session session, List<MarketApp> marketApps) {
Transaction tx = null;
try {
session.setDefaultReadOnly(false);
tx = session.beginTransaction();
long start = System.currentTimeMillis();
mergePaginationToApp(session, marketApps);
session.flush();
session.clear();
logger.info("mergePaginationToApp耗时:{}", (System.currentTimeMillis() - start) + " ms!");
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
logger.error("Exception", e);
}
return tx;
}
示例8: getKeys
import org.hibernate.Session; //导入方法依赖的package包/类
public Collection<String> getKeys(Long piid, String prefix, Type type) {
if( piid == null)
throw new PersistentVarsException("Could not find keys for 'null' piid");
Session session = null;
Transaction transaction = null;
Collection<String> list = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
list = getKeysImpl(session, piid, prefix, type);
transaction.commit();
} catch (HibernateException hibernateException) {
throw new PersistentVarsException("HibernatePropertySet.getKeys: " + hibernateException.getMessage());
} finally {
if (transaction != null && transaction.isActive())
transaction.rollback();
if (session != null)
session.close();
}
return list;
}
示例9: beginTransactionIfAllowed
import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public void beginTransactionIfAllowed(Session theSession) {
if (!theSession.getTransaction().isActive()) {
theSession.beginTransaction();
} else {
theSession.getTransaction().rollback();
theSession.beginTransaction();
}
}
示例10: testHbConnection
import org.hibernate.Session; //导入方法依赖的package包/类
@Test
public void testHbConnection() {
Session session = sessionFactory.openSession();
session.beginTransaction();
List<UserData> result = session.createQuery("from UserData").list();
for (UserData user : result) {
System.out.println(user);
//System.out.println(user.getUsers());
}
session.getTransaction().commit();
session.close();
}
示例11: findAll
import org.hibernate.Session; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public List<Order> findAll() {
List<Order> orders = null;
Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
Query query = session.createQuery("from Order");
orders = (List<Order>) query.list();
session.getTransaction().commit();
} catch (RuntimeException e) {
session.getTransaction().rollback();
}
return orders;
}
示例12: getRoleByLevel
import org.hibernate.Session; //导入方法依赖的package包/类
public Role getRoleByLevel(int roleLevel) {
Role role = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try
{
session.beginTransaction();
Criteria crit = session.createCriteria(Role.class);
crit.add(Restrictions.eq("level",roleLevel));
List<Role> resultList = crit.list();
if (resultList.size() > 0) {
role = resultList.get(0);
}
session.getTransaction().commit();
return role;
}
catch (HibernateException e)
{
System.out.println("Hibernate Exception" + e.getMessage());
session.getTransaction().rollback();
throw new RuntimeException(e);
}
}
示例13: listEmailsOfEvent
import org.hibernate.Session; //导入方法依赖的package包/类
public List<String> listEmailsOfEvent(Long eventId) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
List<String> emailList = new ArrayList<String>();
Event event = session.load(Event.class, eventId);
for (Iterator<Person> it = event.getParticipants().iterator(); it.hasNext(); ) {
Person person = it.next();
emailList.addAll(person.getEmailAddresses());
}
session.getTransaction().commit();
return emailList;
}
示例14: getMarkListByUserAndTask
import org.hibernate.Session; //导入方法依赖的package包/类
@Override
public List<Mark> getMarkListByUserAndTask(int user_id, int img_id) {
List<Mark> marks = new ArrayList<Mark>();
Session session = HibernateUtils.getSession();//生成Session实例
Transaction tx = session.beginTransaction();//生成事务实例
try {
marks = session.createSQLQuery("SELECT option_mark_name,manual_mark_name,mark_accuracy FROM mark WHERE user_id = "+user_id+" AND img_id = "+img_id).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
//调用session的get()方法,找到此用户到内存中
tx.commit();//提交事务
} catch (Exception e) {
e.printStackTrace();
tx.rollback();//事务回滚
}finally{
HibernateUtils.closeSession();//关闭session实例
}
List<Mark> marks2 = new ArrayList<Mark>();
for (int i = 0; i < marks.size(); i++) {
Mark mark = new Mark();
Map map = (Map) marks.get(i);
mark.setOption_mark_name(map.get("option_mark_name")+"");
mark.setManual_mark_name(map.get("manual_mark_name")+"");
mark.setMark_accuracy(map.get("mark_accuracy")+"");
marks2.add(mark);
}
return marks2;
}
示例15: isUserEmailExists
import org.hibernate.Session; //导入方法依赖的package包/类
synchronized public static boolean isUserEmailExists(String email)
{
int chk=0;
sf=Logic.getSf();
Session s = null;
List<Object> lst=new ArrayList<Object> ();
try{
s=sf.openSession();
s.beginTransaction();
Query qry=s.createQuery("select usr_id from UserDetails ud where ud.email_id=:eid");
qry.setParameter("eid", email);
lst=qry.getResultList();
s.getTransaction().commit();
}catch (Exception e)
{
chk=-1;
System.out.println("HibernateException Occured!!"+e);
e.printStackTrace();
}
finally
{
if(s!=null)
{
s.clear();
s.close();
}
}
if(chk==0)
{
if (lst.isEmpty())
{
return (false);
}
else
{
return (true);
}
}
else
{
return (true);
}
}