Example usage for org.hibernate Session persist

List of usage examples for org.hibernate Session persist

Introduction

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

Prototype

void persist(Object object);

Source Link

Document

Make a transient instance persistent.

Usage

From source file:saajResponse.java

private void saveEmployee(Employee e) {
    Configuration cfg = new Configuration();
    SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.save(e);/*from   w  w  w.j a  va  2s . co m*/
    session.beginTransaction();
    session.persist(e);
    session.getTransaction().commit();
    System.out.println("Emplyoee saved");
}

From source file:app.model.repository.impl.UserRoleDaoImpl.java

@Transactional
@Override//  ww  w  .  j  a v  a2 s .c o m
public void addUserRole(UserRole userRole) {

    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.getTransaction();

    try {
        transaction.begin();
        session.persist(userRole); //session.save(userRole);
        transaction.commit();
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();
    } finally {
        if (session.isOpen())
            session.close();
    }

}

From source file:at.ac.tuwien.swa.swazam.server.UserRequestHistoryRepo.java

public void insert(UserRequestHistory item) {
    Session session = factory.openSession();
    Transaction tx = null;/*from  ww  w. j  ava2s  .  co  m*/
    try {
        tx = session.beginTransaction();
        session.persist(item);
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null)
            tx.rollback();
    } finally {
        session.close();
    }
}

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

License:Open Source License

@Test
public void testDelete() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//from   w w w  . j  a  v a 2  s. com
    RequestCapabilities req = new RequestCapabilities("a,b");
    ses.persist(req);
    RigCapabilities rig1 = new RigCapabilities("a,b,c,d");
    ses.persist(rig1);
    RigCapabilities rig2 = new RigCapabilities("a,b,c,d,e");
    ses.persist(rig2);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig1.getId(), req.getId()), req, rig1);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig2.getId(), req.getId()), req, rig2);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(req.getId());
    assertNull(this.dao.get(req.getId()));

    ses.refresh(rig1);
    assertEquals(0, rig1.getMatchingCapabilitieses().size());
    ses.refresh(rig2);
    assertEquals(0, rig2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(rig1);
    ses.delete(rig2);
    ses.getTransaction().commit();
}

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

License:Open Source License

public void testDeleteResourcePermission() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from  ww w.j  ava  2 s .c o m*/
    RequestCapabilities req = new RequestCapabilities("a,b");
    ses.persist(req);
    UserClass uc = new UserClass();
    uc.setName("user_class");
    ses.persist(uc);
    RigCapabilities rig1 = new RigCapabilities("a,b,c,d");
    ses.persist(rig1);
    RigCapabilities rig2 = new RigCapabilities("a,b,c,d,e");
    ses.persist(rig2);
    ResourcePermission rc = new ResourcePermission();
    rc.setType("CAPABILITY");
    rc.setRequestCapabilities(req);
    rc.setExpiryTime(new Date());
    rc.setStartTime(new Date());
    rc.setUserClass(uc);
    ses.persist(rc);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig1.getId(), req.getId()), req, rig1);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig2.getId(), req.getId()), req, rig2);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(req.getId());
    assertNull(this.dao.get(req.getId()));

    ses.refresh(rig1);
    assertEquals(0, rig1.getMatchingCapabilitieses().size());
    ses.refresh(rig2);
    assertEquals(0, rig2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(rig1);
    ses.delete(rig2);
    ses.delete(rc);
    ses.delete(uc);
    ses.getTransaction().commit();
}

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

License:Open Source License

@Test
public void testDelete() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//w  w w .j  av  a 2 s  .  co  m
    RequestCapabilities req1 = new RequestCapabilities("a,b");
    ses.persist(req1);
    RequestCapabilities req2 = new RequestCapabilities("c,d");
    ses.persist(req2);
    RigCapabilities rig = new RigCapabilities("a,b,c,d");
    ses.persist(rig);
    MatchingCapabilities match1 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req1.getId()), req1, rig);
    ses.persist(match1);
    MatchingCapabilities match2 = new MatchingCapabilities(
            new MatchingCapabilitiesId(rig.getId(), req2.getId()), req2, rig);
    ses.persist(match2);
    ses.getTransaction().commit();

    this.dao.delete(rig.getId());
    assertNull(this.dao.get(rig.getId()));

    ses.refresh(req1);
    assertEquals(0, req1.getMatchingCapabilitieses().size());
    ses.refresh(req2);
    assertEquals(0, req2.getMatchingCapabilitieses().size());

    ses.beginTransaction();
    ses.delete(req1);
    ses.delete(req2);
    ses.getTransaction().commit();
}

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

License:Open Source License

@Test
public void testFindKey() {
    Session db = DataAccessActivator.getNewSession();

    db.beginTransaction();//from   w  ww. j a  va 2  s  . co  m

    UserClass uc = new UserClass();
    uc.setName("UC_Test");
    uc.setPriority((short) 1);
    uc.setTimeHorizon(0);
    db.persist(uc);

    UserClassKey key = new UserClassKey();
    key.setRemaining(1);
    key.setUserClass(uc);
    key.generateKey();
    db.persist(key);

    db.getTransaction().commit();

    UserClassKey loadedKey = this.dao.findKey(key.getRedeemKey());

    db.beginTransaction();
    db.delete(key);
    db.delete(uc);
    db.getTransaction().commit();

    assertNotNull(loadedKey);
    assertNotNull(loadedKey.getId());
    assertEquals(key.getRedeemKey(), loadedKey.getRedeemKey());
    assertEquals(key.getRemaining(), loadedKey.getRemaining());

    assertEquals(0, loadedKey.getConstraints().size());
    assertEquals(0, loadedKey.getRedemptions().size());
    assertNull(loadedKey.getExpiry());
}

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

License:Open Source License

@Test
public void testDelete() {
    Session db = DataAccessActivator.getNewSession();

    db.beginTransaction();//from   w w  w.ja v  a2 s.  c o  m

    UserClass uc = new UserClass();
    uc.setName("UC_Test");
    uc.setPriority((short) 1);
    uc.setTimeHorizon(0);
    db.persist(uc);

    UserClassKey key = new UserClassKey();
    key.setRemaining(1);
    key.setUserClass(uc);
    key.generateKey();
    db.persist(key);

    db.getTransaction().commit();

    int preDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    this.dao.delete(this.dao.get(key.getId()));

    int postDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    db.beginTransaction();
    db.delete(uc);
    db.getTransaction().commit();

    assertEquals(1, preDelete);
    assertEquals(0, postDelete);
}

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

License:Open Source License

@Test
public void testDeleteId() {
    Session db = DataAccessActivator.getNewSession();

    db.beginTransaction();//w ww.j  av  a2s  . c o m

    UserClass uc = new UserClass();
    uc.setName("UC_Test");
    uc.setPriority((short) 1);
    uc.setTimeHorizon(0);
    db.persist(uc);

    UserClassKey key = new UserClassKey();
    key.setRemaining(1);
    key.setUserClass(uc);
    key.generateKey();
    db.persist(key);

    db.getTransaction().commit();

    int preDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    this.dao.delete(key.getId());

    int postDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    db.beginTransaction();
    db.delete(uc);
    db.getTransaction().commit();

    assertEquals(1, preDelete);
    assertEquals(0, postDelete);
}

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

License:Open Source License

@Test
public void testDeleteWithConstRef() {
    Session db = DataAccessActivator.getNewSession();

    db.beginTransaction();/*from w w  w .  j  ava 2 s.  c  o m*/

    UserClass uc = new UserClass();
    uc.setName("UC_Test");
    uc.setPriority((short) 1);
    uc.setTimeHorizon(0);
    db.persist(uc);

    UserClassKey key = new UserClassKey();
    key.setRemaining(1);
    key.setUserClass(uc);
    key.generateKey();
    db.persist(key);

    UserClassKeyConstraint keyConst = new UserClassKeyConstraint();
    keyConst.setName("Const_1");
    keyConst.setValue("Value_1");
    keyConst.setClassKey(key);
    db.persist(keyConst);

    UserClassKeyConstraint keyConst2 = new UserClassKeyConstraint();
    keyConst2.setName("Const_2");
    keyConst2.setValue("Value_2");
    keyConst2.setClassKey(key);
    db.persist(keyConst2);

    db.getTransaction().commit();

    int preDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    int preRefCount = (Integer) db.createCriteria(UserClassKeyConstraint.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    this.dao.delete(this.dao.get(key.getId()));

    int postDelete = (Integer) db.createCriteria(UserClassKey.class)
            .add(Restrictions.eq("redeemKey", key.getRedeemKey())).setProjection(Projections.rowCount())
            .uniqueResult();

    int postRefCount = (Integer) db.createCriteria(UserClassKeyConstraint.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

    db.beginTransaction();
    db.delete(uc);
    db.getTransaction().commit();

    assertEquals(1, preDelete);
    assertEquals(2, preRefCount);
    assertEquals(0, postDelete);
    assertEquals(0, postRefCount);
}