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:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

public void populateObjectTypes(Session hibernateSession, String[] typeNames) throws Exception {

    for (String typeName : typeNames) {

        //Check if present

        HibernateIdentityObjectType hibernateType = (HibernateIdentityObjectType) hibernateSession
                .createCriteria(HibernateIdentityObjectType.class).add(Restrictions.eq("name", typeName))
                .uniqueResult();/*from  ww w .  ja  v a 2  s.co  m*/

        if (hibernateType == null) {
            hibernateType = new HibernateIdentityObjectType(typeName);
            hibernateSession.persist(hibernateType);
        }

    }

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

public void populateRelationshipTypes(Session hibernateSession, String[] typeNames) throws Exception {

    for (String typeName : typeNames) {
        HibernateIdentityObjectRelationshipType hibernateType = (HibernateIdentityObjectRelationshipType) hibernateSession
                .createCriteria(HibernateIdentityObjectRelationshipType.class)
                .add(Restrictions.eq("name", typeName)).uniqueResult();

        if (hibernateType == null) {
            hibernateType = new HibernateIdentityObjectRelationshipType(typeName);
            hibernateSession.persist(hibernateType);
        }/*from w  w  w  . ja  v  a 2  s. c  o m*/

    }

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

public void populateCredentialTypes(Session hibernateSession, String[] typeNames) throws Exception {

    for (String typeName : typeNames) {
        HibernateIdentityObjectCredentialType hibernateType = (HibernateIdentityObjectCredentialType) hibernateSession
                .createCriteria(HibernateIdentityObjectCredentialType.class)
                .add(Restrictions.eq("name", typeName)).uniqueResult();

        if (hibernateType == null) {
            hibernateType = new HibernateIdentityObjectCredentialType(typeName);
            hibernateSession.persist(hibernateType);
        }/*ww w  .j  a va2  s  .c  om*/

    }

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

public void addRealm(Session hibernateSession, String realmName) throws IdentityException {

    try {//from w  w w.jav  a  2  s  .  com

        HibernateRealm realm = new HibernateRealm(realmName);
        hibernateSession.persist(realm);

    } catch (Exception e) {
        if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Exception occurred: ", e);
        }

        throw new IdentityException("Failed to create store realm", e);
    }
}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

private HibernateRealm getRealm(Session hibernateSession, IdentityStoreInvocationContext ctx)
        throws IdentityException {
    if (getRealmName(ctx) == null) {
        throw new IllegalStateException("Realm Id not present");
    }/*from  w  ww.j  a va 2s. c  o  m*/

    HibernateRealm realm = null;

    // If store is not realm aware return null to create/get objects accessible from other realms 
    if (!isRealmAware()) {
        realm = (HibernateRealm) hibernateSession.createCriteria(HibernateRealm.class)
                .add(Restrictions.eq("name", DEFAULT_REALM_NAME)).setCacheable(true).uniqueResult();

        if (realm == null) {
            throw new IllegalStateException("Default store realm is not present: " + DEFAULT_REALM_NAME);
        }

    } else {
        realm = (HibernateRealm) hibernateSession.createCriteria(HibernateRealm.class)
                .add(Restrictions.eq("name", getRealmName(ctx))).setCacheable(true).uniqueResult();

        // TODO: other way to not lazy initialize realm? special method called on every new session creation
        if (realm == null) {
            HibernateRealm newRealm = new HibernateRealm(getRealmName(ctx));
            hibernateSession.persist(newRealm);
            return newRealm;
        }
    }

    return realm;
}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateModelTestCase.java

License:Open Source License

public void testPersistence() {

    begin();//  www .  j a  va2 s.  c o  m
    Session session = getSessionFactory().getCurrentSession();

    HibernateRealm defaultRealm = new HibernateRealm("default");
    session.persist(defaultRealm);

    Map<String, String> props = new HashMap<String, String>();
    props.put("test", "testValue");
    defaultRealm.setProperties(props);

    defaultRealm = (HibernateRealm) session.get(HibernateRealm.class, defaultRealm.getId());

    Assert.assertEquals(defaultRealm.getProperties().keySet().size(), 1);

    //

    HibernateIdentityObjectType iot = new HibernateIdentityObjectType();
    iot.setName("User");

    session.persist(iot);
    Assert.assertTrue(session.contains(iot));

    HibernateIdentityObject io = new HibernateIdentityObject();
    io.setName("John Kowalski");
    io.setRealm(defaultRealm);
    io.setIdentityType(iot);

    session.persist(io);
    Assert.assertTrue(session.contains(io));

    session.delete(io);
    Assert.assertFalse(session.contains(io));

    //

    //

    HibernateIdentityObjectRelationshipType iort = new HibernateIdentityObjectRelationshipType();
    iort.setName("Member");

    session.persist(iort);
    Assert.assertTrue(session.contains(iort));

    Assert.assertTrue(session.contains(iort));

    session.delete(iort);
    Assert.assertFalse(session.contains(iort));

    commit();

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateModelTestCase.java

License:Open Source License

public void testRelationships() {
    begin();//www. j  a va 2 s . c om

    Session session = getSessionFactory().getCurrentSession();

    HibernateRealm realm = new HibernateRealm("default");
    session.persist(realm);

    HibernateIdentityObjectType groupType = new HibernateIdentityObjectType("Group");
    session.persist(groupType);
    HibernateIdentityObjectType userType = new HibernateIdentityObjectType("User");
    session.persist(userType);

    HibernateIdentityObject user1 = new HibernateIdentityObject("user1", userType, realm);
    session.persist(user1);
    HibernateIdentityObject user2 = new HibernateIdentityObject("user2", userType, realm);
    session.persist(user2);
    HibernateIdentityObject user3 = new HibernateIdentityObject("user3", userType, realm);
    session.persist(user3);

    HibernateIdentityObject group1 = new HibernateIdentityObject("group1", groupType, realm);
    session.persist(group1);
    HibernateIdentityObject group2 = new HibernateIdentityObject("group2", groupType, realm);
    session.persist(group2);
    HibernateIdentityObject group3 = new HibernateIdentityObject("group3", groupType, realm);
    session.persist(group3);

    HibernateIdentityObjectRelationshipType memberType = new HibernateIdentityObjectRelationshipType("member");
    session.persist(memberType);

    HibernateIdentityObjectRelationship relationship = new HibernateIdentityObjectRelationship(memberType,
            group1, user1);
    session.persist(relationship);
    relationship = new HibernateIdentityObjectRelationship(memberType, group1, user2);
    session.persist(relationship);
    relationship = new HibernateIdentityObjectRelationship(memberType, group2, user3);
    session.persist(relationship);
    relationship = new HibernateIdentityObjectRelationship(memberType, group2, group1);
    session.persist(relationship);

    commit();

    //

    begin();

    session = getSessionFactory().getCurrentSession();

    Query ioQuery = session.createQuery("select o from HibernateIdentityObject o where o.name like :name");

    group1 = (HibernateIdentityObject) ioQuery.setParameter("name", "group1").uniqueResult();
    Assert.assertEquals(2, group1.getFromRelationships().size());
    Assert.assertEquals(1, group1.getToRelationships().size());
    group2 = (HibernateIdentityObject) ioQuery.setParameter("name", "group2").uniqueResult();
    Assert.assertEquals(2, group2.getFromRelationships().size());
    Assert.assertEquals(0, group2.getToRelationships().size());

    user3 = (HibernateIdentityObject) ioQuery.setParameter("name", "user3").uniqueResult();
    Assert.assertEquals(1, user3.getToRelationships().size());
    Assert.assertEquals(0, user3.getFromRelationships().size());
    Assert.assertEquals("group2",
            user3.getToRelationships().iterator().next().getFromIdentityObject().getName());
    commit();

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateModelTestCase.java

License:Open Source License

public void testNameTypeConstraint() throws Exception {

    begin();//from   www. j  a va2  s  .com
    Session session = getSessionFactory().getCurrentSession();

    HibernateRealm realm = new HibernateRealm("default");
    session.persist(realm);

    HibernateIdentityObjectType groupType = new HibernateIdentityObjectType("Group");
    session.persist(groupType);
    HibernateIdentityObjectType userType = new HibernateIdentityObjectType("User");
    session.persist(userType);

    HibernateIdentityObject user1 = new HibernateIdentityObject("user1", userType, realm);
    session.persist(user1);
    HibernateIdentityObject user2 = new HibernateIdentityObject("user2", userType, realm);
    session.persist(user2);

    commit();

    begin();

    session = getSessionFactory().getCurrentSession();

    try {

        HibernateIdentityObject user3 = new HibernateIdentityObject("user2", userType, realm);
        session.persist(user3);

        Assert.assertFalse(getHibernateSupport().commitTransaction());

    } catch (Exception e) {
        // may be expected
        getHibernateSupport().rollbackTransaction();
    }

    begin();

    session = getSessionFactory().getCurrentSession();

    HibernateIdentityObject user4 = new HibernateIdentityObject("group1", userType, realm);
    session.persist(user4);

    HibernateIdentityObject group1 = new HibernateIdentityObject("group1", groupType, realm);
    session.persist(group1);
    HibernateIdentityObject group2 = new HibernateIdentityObject("group2", groupType, realm);
    session.persist(group2);

    //session.flush();

    try {

        HibernateIdentityObject group3 = new HibernateIdentityObject("group2", groupType, realm);
        session.persist(group3);

        // Should fail
        Assert.assertFalse(getHibernateSupport().commitTransaction());
    } catch (Exception e) {
        //expected
        getHibernateSupport().rollbackTransaction();
    }

    begin();

    session = getSessionFactory().getCurrentSession();

    HibernateIdentityObject group4 = new HibernateIdentityObject("user1", groupType, realm);
    session.persist(group4);

    session.flush();

    commit();

}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateModelTestCase.java

License:Open Source License

public void testAttributes() throws Exception {
    begin();/*from w w  w. j a  v a  2  s . co m*/

    Session session = getSessionFactory().getCurrentSession();

    HibernateRealm realm = new HibernateRealm("default");
    session.persist(realm);

    HibernateIdentityObjectType userType = new HibernateIdentityObjectType("User");
    session.persist(userType);
    HibernateIdentityObject user1 = new HibernateIdentityObject("user1", userType, realm);
    session.persist(user1);

    Set<String> values1 = new HashSet<String>();
    values1.add("Val1");
    values1.add("Val2");
    values1.add("Val3");

    HibernateIdentityObjectAttribute attr = new HibernateIdentityObjectAttribute(user1, "simple1",
            HibernateIdentityObjectAttribute.TYPE_TEXT);
    attr.setTextValues(values1);
    user1.getAttributes().add(attr);
    attr = new HibernateIdentityObjectAttribute(user1, "simple2", HibernateIdentityObjectAttribute.TYPE_TEXT);
    attr.setTextValues(values1);
    user1.getAttributes().add(attr);

    commit();

    begin();

    session = getSessionFactory().getCurrentSession();

    user1 = (HibernateIdentityObject) session.get(HibernateIdentityObject.class, new Long(user1.getId()));
    Assert.assertEquals(2, user1.getAttributes().size());
    //      assertNotNull(user1.getProfileAttributes().get("simple1"));
    //      assertEquals(3, user1.getProfileAttributes().get("simple1").length);

    commit();

}

From source file:org.rebioma.server.services.AscDataDbImpl.java

License:Apache License

public void persist(AscData transientInstance) {
    log.debug("persisting AscData instance");
    try {/*from www. j av a2  s.c om*/
        Session session = ManagedSession.createNewSessionAndTransaction();
        session.persist(transientInstance);
        //HibernateUtil.getCurrentSession().persist(transientInstance);
        log.debug("persist successful");
    } catch (RuntimeException re) {
        log.error("persist failed", re);
        throw re;
    }
}