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:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.UserClassKeyDaoTester.java

License:Open Source License

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

    db.beginTransaction();//w  ww. j a  va  2 s  .  c om

    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);

    User user = new User();
    user.setName("uck_test");
    user.setNamespace("TEST");
    user.setPersona("USER");
    db.persist(user);

    User user2 = new User();
    user2.setName("uck_test2");
    user2.setNamespace("TEST");
    user2.setPersona("USER");
    db.persist(user2);

    UserClassKeyRedemption redemp = new UserClassKeyRedemption();
    redemp.setClassKey(key);
    redemp.setUser(user);
    db.persist(redemp);

    UserClassKeyRedemption redemp2 = new UserClassKeyRedemption();
    redemp2.setClassKey(key);
    redemp2.setUser(user2);
    db.persist(redemp2);

    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(UserClassKeyRedemption.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(UserClassKeyRedemption.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

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

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

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

License:Open Source License

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

    db.beginTransaction();/*from w w w  . j  a  v 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);

    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);

    User user = new User();
    user.setName("uck_test");
    user.setNamespace("TEST");
    user.setPersona("USER");
    db.persist(user);

    User user2 = new User();
    user2.setName("uck_test2");
    user2.setNamespace("TEST");
    user2.setPersona("USER");
    db.persist(user2);

    UserClassKeyRedemption redemp = new UserClassKeyRedemption();
    redemp.setClassKey(key);
    redemp.setUser(user);
    db.persist(redemp);

    UserClassKeyRedemption redemp2 = new UserClassKeyRedemption();
    redemp2.setClassKey(key);
    redemp2.setUser(user2);
    db.persist(redemp2);

    db.getTransaction().commit();

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

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

    int preRedempRefCount = (Integer) db.createCriteria(UserClassKeyRedemption.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 postConstRefCount = (Integer) db.createCriteria(UserClassKeyConstraint.class)
            .add(Restrictions.eq("classKey.id", key.getId())).setProjection(Projections.rowCount())
            .uniqueResult();

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

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

    assertEquals(1, preDelete);
    assertEquals(2, preConstRefCount);
    assertEquals(2, preRedempRefCount);
    assertEquals(0, postDelete);
    assertEquals(0, postConstRefCount);
    assertEquals(0, postRedempRefCount);
}

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

License:Open Source License

@Test
public void testFindLock() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();/*from  w ww. ja v a  2 s  .c o m*/
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    UserLock ld = this.dao.findLock(user, perm);
    assertNotNull(ld);
    assertEquals(lock.getId(), ld.getId());
    assertEquals(lock.getLockKey(), ld.getLockKey());
    assertEquals(lock.getUser().getName(), ld.getUser().getName());
    assertEquals(lock.getResourcePermission().getId(), ld.getResourcePermission().getId());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#unlockUserLock(UnlockUserLock)}.
 *///from  ww  w  . jav  a2s.com
@Test
public void testUnlockUserLockID() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    /* Request. */
    UnlockUserLock request = new UnlockUserLock();
    UserLockType lockReq = new UserLockType();
    request.setUnlockUserLock(lockReq);
    lockReq.setRequestorQName("TESTNS:mdiponio");
    lockReq.setLockKey("abc123");
    lockReq.setUserLockID(lock.getId().intValue());

    UnlockUserLockResponse resp = this.permissions.unlockUserLock(request);
    assertNotNull(resp);
    OperationResponseType op = resp.getUnlockUserLockResponse();
    assertNotNull(op);
    assertTrue(op.getSuccessful());
    assertEquals(0, op.getFailureCode());
    assertNull(op.getFailureReason());

    ses.refresh(lock);
    assertFalse(lock.isIsLocked());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#unlockUserLock(UnlockUserLock)}.
 *//*from  ww  w . j a  v a  2s .  c  om*/
@Test
public void testUnlockUserLockIDWrongKey() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    /* Request. */
    UnlockUserLock request = new UnlockUserLock();
    UserLockType lockReq = new UserLockType();
    request.setUnlockUserLock(lockReq);
    lockReq.setRequestorQName("TESTNS:mdiponio");
    lockReq.setLockKey("wrongkey");
    lockReq.setUserLockID(lock.getId().intValue());

    UnlockUserLockResponse resp = this.permissions.unlockUserLock(request);
    assertNotNull(resp);
    OperationResponseType op = resp.getUnlockUserLockResponse();
    assertNotNull(op);
    assertFalse(op.getSuccessful());
    assertEquals(3, op.getFailureCode());
    assertNotNull(op.getFailureReason());

    ses.refresh(lock);
    assertTrue(lock.isIsLocked());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#unlockUserLock(UnlockUserLock)}.
 *//*from w w  w  . jav a 2s .c om*/
@Test
public void testUnlockUserLockPermUser() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    /* Request. */
    UnlockUserLock request = new UnlockUserLock();
    UserLockType lockReq = new UserLockType();
    request.setUnlockUserLock(lockReq);
    lockReq.setRequestorQName("TESTNS:mdiponio");
    lockReq.setLockKey("abc123");
    UserLockIDUserPermSequence seq = new UserLockIDUserPermSequence();
    PermissionIDType pId = new PermissionIDType();
    pId.setPermissionID(perm.getId().intValue());
    seq.setPermissionID(pId);
    UserIDType uId = new UserIDType();
    uId.setUserQName(user.getNamespace() + ":" + user.getName());
    seq.setUserID(uId);
    lockReq.setUserIDPermissionsTracker(seq);

    UnlockUserLockResponse resp = this.permissions.unlockUserLock(request);
    assertNotNull(resp);
    OperationResponseType op = resp.getUnlockUserLockResponse();
    assertNotNull(op);
    assertTrue(op.getSuccessful());
    assertEquals(0, op.getFailureCode());
    assertNull(op.getFailureReason());

    ses.refresh(lock);
    assertFalse(lock.isIsLocked());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

/**
 * Test method for {@link PermissionsSOAPImpl#unlockUserLock(UnlockUserLock)}.
 */// ww  w. j a  v a  2 s  . co m
@Test
public void testUnlockUserLockPermUserWrongKey() {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();
    User user = new User("locktest", "ns", "USER");
    ses.persist(user);
    UserClass userClass = new UserClass();
    userClass.setName("uc");
    ses.persist(userClass);
    ResourcePermission perm = new ResourcePermission();
    perm.setStartTime(new Date());
    perm.setExpiryTime(new Date());
    perm.setUserClass(userClass);
    perm.setType("RIG");
    ses.persist(perm);
    UserLock lock = new UserLock(user, perm, true, "abc123");
    ses.persist(lock);
    ses.getTransaction().commit();

    /* Request. */
    UnlockUserLock request = new UnlockUserLock();
    UserLockType lockReq = new UserLockType();
    request.setUnlockUserLock(lockReq);
    lockReq.setRequestorQName("TESTNS:mdiponio");
    lockReq.setLockKey("wrongkey");
    UserLockIDUserPermSequence seq = new UserLockIDUserPermSequence();
    PermissionIDType pId = new PermissionIDType();
    pId.setPermissionID(perm.getId().intValue());
    seq.setPermissionID(pId);
    UserIDType uId = new UserIDType();
    uId.setUserQName(user.getNamespace() + ":" + user.getName());
    seq.setUserID(uId);
    lockReq.setUserIDPermissionsTracker(seq);

    UnlockUserLockResponse resp = this.permissions.unlockUserLock(request);
    assertNotNull(resp);
    OperationResponseType op = resp.getUnlockUserLockResponse();
    assertNotNull(op);
    assertFalse(op.getSuccessful());
    assertEquals(3, op.getFailureCode());
    assertNotNull(op.getFailureReason());

    ses.refresh(lock);
    assertTrue(lock.isIsLocked());

    ses.beginTransaction();
    ses.delete(lock);
    ses.delete(perm);
    ses.delete(userClass);
    ses.delete(user);
    ses.getTransaction().commit();
    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

@Test
public void testGetUserFromUserID() throws Exception {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//from  w  w w .j  a  va 2 s  .c  o m
    User user = new User("UserIdTest", "Permission", "USER");
    ses.persist(user);
    ses.getTransaction().commit();

    UserIDType uid = new UserIDType();
    uid.setUserID(String.valueOf(user.getId()));

    Method meth = PermissionsSOAPImpl.class.getDeclaredMethod("getUserFromUserID", UserIDType.class,
            Session.class);
    meth.setAccessible(true);
    User loaded = (User) meth.invoke(this.permissions, uid, ses);
    assertNotNull(loaded);

    ses.beginTransaction();
    ses.delete(user);
    ses.getTransaction().commit();

    assertEquals(user.getId().longValue(), loaded.getId().longValue());
    assertEquals(user.getName(), loaded.getName());
    assertEquals(user.getNamespace(), loaded.getNamespace());
    assertEquals(user.getPersona(), loaded.getPersona());

    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

@Test
public void testGetUserFromUserIDNmNsSeq() throws Exception {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//from  w  w  w.j a va  2  s.co m
    User user = new User("UserIdTest", "Permission", "USER");
    ses.persist(user);
    ses.getTransaction().commit();

    UserIDType uid = new UserIDType();
    UserNameNamespaceSequence seq = new UserNameNamespaceSequence();
    seq.setUserNamespace(user.getNamespace());
    seq.setUserName(user.getName());
    uid.setUserNameNamespaceSequence(seq);

    Method meth = PermissionsSOAPImpl.class.getDeclaredMethod("getUserFromUserID", UserIDType.class,
            Session.class);
    meth.setAccessible(true);
    User loaded = (User) meth.invoke(this.permissions, uid, ses);
    assertNotNull(loaded);

    ses.beginTransaction();
    ses.delete(user);
    ses.getTransaction().commit();

    assertEquals(user.getId().longValue(), loaded.getId().longValue());
    assertEquals(user.getName(), loaded.getName());
    assertEquals(user.getNamespace(), loaded.getNamespace());
    assertEquals(user.getPersona(), loaded.getPersona());

    ses.close();
}

From source file:au.edu.uts.eng.remotelabs.schedserver.permissions.intf.tests.PermissionsSOAPImplTester.java

License:Open Source License

@Test
public void testGetUserFromUserIDQName() throws Exception {
    Session ses = DataAccessActivator.getNewSession();
    ses.beginTransaction();//from w ww.  java 2 s  . c o  m
    User user = new User("UserIdTest", "Permission", "USER");
    ses.persist(user);
    ses.getTransaction().commit();

    UserIDType uid = new UserIDType();
    uid.setUserQName(user.getNamespace() + ":" + user.getName());

    Method meth = PermissionsSOAPImpl.class.getDeclaredMethod("getUserFromUserID", UserIDType.class,
            Session.class);
    meth.setAccessible(true);
    User loaded = (User) meth.invoke(this.permissions, uid, ses);
    assertNotNull(loaded);

    ses.beginTransaction();
    ses.delete(user);
    ses.getTransaction().commit();

    assertEquals(user.getId().longValue(), loaded.getId().longValue());
    assertEquals(user.getName(), loaded.getName());
    assertEquals(user.getNamespace(), loaded.getNamespace());
    assertEquals(user.getPersona(), loaded.getPersona());

    ses.close();
}