Example usage for org.hibernate Session get

List of usage examples for org.hibernate Session get

Introduction

In this page you can find the example usage for org.hibernate Session get.

Prototype

Object get(String entityName, Serializable id);

Source Link

Document

Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.

Usage

From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java

License:Open Source License

/**
 * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#delete(java.lang.Object)}.
 *///from   w w w .  j a  v a  2  s  .c  o m
public void testDeleteId() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    Serializable id = ses.save(new Config("delete_conf_key", "delete_conf_val"));
    ses.getTransaction().commit();
    ses.close();

    ses = DataAccessActivator.getNewSession();
    assertNotNull(ses.get(Config.class, id));
    ses.close();

    this.dao.delete(id);

    ses = DataAccessActivator.getNewSession();
    assertNull(ses.get(Config.class, id));
    ses.close();
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * If a consent is not in a state where it can be deleted then remove it. It can be in a different status before it can be removed.
 * //from www .ja v a  2  s. c  o  m
 * @param consent
 * @throws ArkSystemException
 */
public void delete(Consent consent) throws ArkSystemException, EntityNotFoundException {
    try {
        Session session = getSession();
        consent = (Consent) session.get(Consent.class, consent.getId());
        if (consent != null) {
            getSession().delete(consent);
        } else {
            throw new EntityNotFoundException(
                    "The Consent record you tried to remove does not exist in the Ark System");
        }

    } catch (HibernateException someHibernateException) {
        log.error("An Exception occured while trying to delete this consent "
                + someHibernateException.getStackTrace());
    } catch (Exception e) {
        log.error("An Exception occured while trying to delete this consent " + e.getStackTrace());
        throw new ArkSystemException(
                "A System Error has occured. We wil have someone contact you regarding this issue");
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public Consent getConsent(Long id) throws ArkSystemException {
    Session session = openSession();
    Consent consent = (Consent) session.get(Consent.class, id);
    return consent;
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public void update(ConsentFile consentFile) throws ArkSystemException, EntityNotFoundException {
    Session session = getSession();

    dateNow = new Date(System.currentTimeMillis());
    consentFile.setUpdateTime(dateNow);//from   w w w  .j  a  va2 s.com

    if ((ConsentFile) session.get(ConsentFile.class, consentFile.getId()) != null) {
        session.update(consentFile);
    } else {
        throw new EntityNotFoundException(
                "The Consent file record you tried to update does not exist in the Ark System");
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * If a consentFile is not in a state where it can be deleted then remove it. It can be in a different status before it can be removed.
 * //from   w  w  w.  j  a v  a2s .  c o  m
 * @param consentFile
 * @throws ArkSystemException
 */
public void delete(ConsentFile consentFile) throws ArkSystemException, EntityNotFoundException {
    try {
        Session session = getSession();
        consentFile = (ConsentFile) session.get(ConsentFile.class, consentFile.getId());
        if (consentFile != null) {
            getSession().delete(consentFile);
        } else {
            throw new EntityNotFoundException(
                    "The Consent file record you tried to remove does not exist in the Ark System");
        }

    } catch (HibernateException someHibernateException) {
        log.error("An Exception occured while trying to delete this consent file "
                + someHibernateException.getStackTrace());
    } catch (Exception e) {
        log.error("An Exception occured while trying to delete this consent file " + e.getStackTrace());
        throw new ArkSystemException(
                "A System Error has occured. We wil have someone contact you regarding this issue");
    }
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * If a subjectFile is not in a state where it can be deleted then remove it. It can be in a different status before it can be removed.
 * /*from www .  ja  v a  2 s  .  c  o  m*/
 * @param subjectFile
 * @throws ArkSystemException
 */
public void delete(SubjectFile subjectFile) throws ArkSystemException, EntityNotFoundException {
    try {
        Session session = getSession();
        subjectFile = (SubjectFile) session.get(SubjectFile.class, subjectFile.getId());
        if (subjectFile != null) {
            getSession().delete(subjectFile);
        } else {
            throw new EntityNotFoundException(
                    "The Consent file record you tried to remove does not exist in the Ark System");
        }

    } catch (HibernateException someHibernateException) {
        log.error("An Exception occured while trying to delete this consent file "
                + someHibernateException.getStackTrace());
    } catch (Exception e) {
        log.error("An Exception occured while trying to delete this consent file " + e.getStackTrace());
        throw new ArkSystemException(
                "A System Error has occured. We wil have someone contact you regarding this issue");
    }
}

From source file:autoancillarieslimited.hiberate.dao.AbstractDao.java

@Override
public T getByID(int id, Class clazz) {
    T item = null;//from  ww  w. j a  v  a2  s  . co  m
    Session session = null;
    Transaction beginTransaction = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        beginTransaction = session.beginTransaction();
        item = (T) session.get(clazz, id);
        session.flush();
        session.clear();
        beginTransaction.commit();
    } catch (HibernateException ex) {
        ex.printStackTrace();
        if (beginTransaction != null) {
            beginTransaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return item;
}

From source file:autoancillarieslimited.hiberate.dao.WareHousesDAO.java

public Region getRegionByID(int id) {
    Region item = null;//ww  w . ja  v  a2  s. co  m
    Session session = null;
    Transaction beginTransaction = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        beginTransaction = session.beginTransaction();
        item = (Region) session.get(Region.class, id);
        session.flush();
        session.clear();
        beginTransaction.commit();
    } catch (HibernateException ex) {
        ex.printStackTrace();
        if (beginTransaction != null) {
            beginTransaction.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return item;
}

From source file:backendsw2.BackEndSW2.java

/**
    * @param userName//from  w  w w .  j  av a2  s  .c  om
    * @param passwd
    * @return return object "account object "
    */
public static Object login(String userName, String passwd) {

    boolean flag = false;
    Account object = new Account();
    Session se = databaseManager.SessionsManager.getSessionFactory().openSession();
    flag = true;
    AdminAccount ad = null;
    Employer em = null;
    Freelancer f = null;
    se.getTransaction().begin();

    // add  criteria to select user
    Criteria cr = se.createCriteria(Account.class);
    Criterion name = Restrictions.eq("userName", userName);
    Criterion pass = Restrictions.eq("password", passwd);
    LogicalExpression andr = Restrictions.and(name, pass);
    cr.add(andr);
    object = (Account) cr.list().get(0);

    try {
        ad = (AdminAccount) se.get(AdminAccount.class, object.getId());
    } catch (Exception e) {
        try {
            em = (Employer) se.get(Employer.class, object.getId());
        } catch (Exception ex) {
            try {
                f = (Freelancer) se.get(Freelancer.class, object.getId());
            } catch (Exception ee) {
                f = null;
            } finally {
                if (flag) {
                    se.close();
                }
                return f;
            }
        } finally {
            if (flag) {
                se.close();
            }
            return em;
        }
    } finally {
        if (flag) {
            se.close();
        }
        return ad;
    }

}

From source file:backendsw2.BackEndSW2.java

public static void boda() {
    Session se = databaseManager.SessionsManager.getSessionFactory().openSession();
    Employer emp = new Employer();
    //        emp.setId(2);
    //               Task task = new Task();
    //               task.setEmployer(emp);
    //               task.setCategory("zdxf");
    //               emp.createTask(task);
    //               
    Offer offer = (Offer) se.get(Offer.class, 2);
    //  emp.acceptFinishedTask(offer);
    //System.out.println(offer.getId());
    // emp.rejectFinishedTask(offer);
    Feedback f = new Feedback();
    f.setDate(LocalDateTime.MAX);
    f.setDescription("this is the test ");
    f.setOffer(offer);/*from  w ww .ja  va2 s  .c  om*/
    f.setRateValue(3);
    //emp.makeFeedback(f ,offer);
    // emp.rejectOffer(offer);
    //emp.acceptFinishedTask(offer);
}