Example usage for org.hibernate Session merge

List of usage examples for org.hibernate Session merge

Introduction

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

Prototype

Object merge(Object object);

Source Link

Document

Copy the state of the given object onto the persistent object with the same identifier.

Usage

From source file:org.processbase.engine.bam.db.HibernateUtil.java

License:Open Source License

public MetaFact addMetaFact(MetaFact metaFact) {
    Session session = getSessionFactory().openSession();
    Transaction tx = null;//  ww w  .  j  ava  2s .c  o  m
    try {
        tx = session.beginTransaction();
        metaFact = (MetaFact) session.merge(metaFact);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        session.close();
    }
    return metaFact;
}

From source file:org.processbase.engine.bam.db.HibernateUtil.java

License:Open Source License

public MetaKpi addMetaKpi(MetaKpi metaKpi) {
    Session session = getSessionFactory().openSession();
    Transaction tx = null;/*  ww w.jav a  2  s. c  om*/
    try {
        tx = session.beginTransaction();
        metaKpi = (MetaKpi) session.merge(metaKpi);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        session.close();
    }
    return metaKpi;
}

From source file:org.processbase.engine.bam.db.HibernateUtil.java

License:Open Source License

public void updateMetaKpi(MetaKpi metaKpi) {
    Session session = getSessionFactory().openSession();
    Transaction tx = null;//from w w  w .  j  a v  a 2 s . c  o m
    try {
        tx = session.beginTransaction();
        session.merge(metaKpi);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        session.close();
    }
}

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

License:Apache License

public AscData merge(AscData detachedInstance) {
    log.debug("merging AscData instance");
    try {//from   w ww .  jav  a 2s . c o  m
        //Session session = HibernateUtil.getCurrentSession();
        //boolean isFirstTransaction = HibernateUtil.beginTransaction(session);
        Session session = ManagedSession.createNewSessionAndTransaction();
        AscData result = (AscData) session.merge(detachedInstance);
        log.debug("merge successful");
        //if (isFirstTransaction) {
        //  HibernateUtil.commitCurrentTransaction();
        //}
        ManagedSession.commitTransaction(session);
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        //HibernateUtil.rollbackTransaction();
        throw re;
    }
}

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

License:Apache License

public Occurrence merge(Occurrence detachedInstance) {
    log.debug("merging Occurrence instance");
    try {//from   www.ja v a  2 s  .c om
        //Session session = HibernateUtil.getCurrentSession();
        //boolean isFirstTransaction = HibernateUtil.beginTransaction(session);
        Session session = ManagedSession.createNewSessionAndTransaction();
        Occurrence result = (Occurrence) session.merge(detachedInstance);
        log.debug("merge successful");
        //if (isFirstTransaction) {
        //  HibernateUtil.commitCurrentTransaction();
        //}
        ManagedSession.commitTransaction(session);
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        //HibernateUtil.rollbackTransaction();
        throw re;
    }
}

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

License:Apache License

public User merge(User detachedInstance) {
    log.debug("merging User instance");
    try {/*from   www  . j a v a 2  s.co m*/
        Session session = ManagedSession.createNewSessionAndTransaction();
        User result = (User) session.merge(detachedInstance);
        log.debug("merge successful");
        ManagedSession.commitTransaction(session);
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        throw re;
    }
}

From source file:org.sakaiproject.lessonbuildertool.service.ForumEntity.java

License:Educational Community License

public void setGroups(Collection<String> groups) {

    // Setgroups with a non-null list: we set all contributor entries to none, and then set the
    //    specified groups to contribtor. By only handling groups, we avoid interfering with
    //    anything you might do in the tool. But the moment you use access control, we take
    //    over. Sorry. Once we've done that you could go back into the tool and hack, but I
    //    don't recommend that.
    // Setgroups with a null list: we set all contributor entries to none, and then set all roles
    //    other than maintain to contributor.

    setMasks();/*from  w  w w.ja v  a  2s.co m*/

    //System.out.println("topic 1 " + topic + " " + groups);
    if (topic == null)
        topic = getTopicById(true, id);
    //System.out.println("topic 2 " + topic);
    if (topic == null)
        return;

    // topicCache.remove(id);

    // old entries
    Set<DBMembershipItem> oldMembershipItemSet = uiPermissionsManager.getTopicItemsSet((DiscussionTopic) topic);

    // which old entires to delete
    Set<DBMembershipItem> deleteItemSet = new HashSet<DBMembershipItem>();

    // all entries we will keep
    Set membershipItemSet = new HashSet();

    Site site = null;
    String maintainRole = null;

    // used so we can give an access level to each role. Remove roles from this as we see
    // them, so at the we just do the ones remaining
    List<String> roles = new ArrayList<String>();

    try {
        site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
        maintainRole = AuthzGroupService.getAuthzGroup("/site/" + site.getId()).getMaintainRole();
        Set<Role> roleObjs = AuthzGroupService.getAuthzGroup("/site/" + site.getId()).getRoles();
        for (Role roleObj : roleObjs)
            roles.add(roleObj.getId());
    } catch (Exception e) {
        System.out.println("Unable to get site info for AddEntityControl " + e);
        return;
    }

    DBMembershipItem membershipItem = null;

    boolean haveOwner = false;

    if (groups != null && groups.size() > 0) {

        // this is the groups we've been asked to use
        // remove groups form this as we see them if they already have access
        // so at the end we just add the ones remaining
        List<String> groupNames = new ArrayList<String>();
        for (String groupId : groups)
            groupNames.add(site.getGroup(groupId).getTitle());
        // delete groups from here as they are done.

        // if we've seen an owner. Otherwise set the maintain role as owner

        // Setgroups with a non-null list: we set all contributor entries to none, and then set the
        //    specified groups to contribtor. However we don't touch owner.
        // By only handling groups, we avoid interfering with
        //    anything you might do in the tool. But the moment you use access control, we take
        //    over. Sorry. Once we've done that you could go back into the tool and hack, but I
        //    don't recommend that.

        for (DBMembershipItem item : oldMembershipItemSet) {
            //System.out.println("old item " + item.getPermissionLevelName() + " " + item.getType() + " " + item.getName());
            if (item.getPermissionLevelName().equals("Owner"))
                haveOwner = true;
            if (item.getType().equals(MembershipItem.TYPE_ROLE) && roles.contains(item.getName()))
                roles.remove(item.getName()); // we've seen it, don't need to add
            if (item.getType().equals(MembershipItem.TYPE_GROUP) && groupNames.contains(item.getName())) {
                // if it's one of our groups make it a contributor if it's not already an owner
                if (!item.getPermissionLevelName().equals("Contributor")
                        && !item.getPermissionLevelName().equals("Owner")) {

                    //System.out.println("make contributor");
                    PermissionLevel contributorLevel = permissionLevelManager
                            .createPermissionLevel("Contributor", IdManager.createUuid(), contributorMask);
                    permissionLevelManager.savePermissionLevel(contributorLevel);

                    membershipItem = permissionLevelManager.createDBMembershipItem(item.getName(),
                            "Contributor", MembershipItem.TYPE_GROUP);
                    membershipItem.setPermissionLevel(contributorLevel);
                    permissionLevelManager.saveDBMembershipItem(membershipItem);
                    membershipItemSet.add(membershipItem);
                    deleteItemSet.add(item);
                } else { // if it was contributor or owner, keep it
                    //System.out.println("keep");
                    membershipItemSet.add(item);
                }
                groupNames.remove(item.getName()); // it's done
            } else if (item.getPermissionLevelName().equals("Contributor")) { // only group members are contributors
                // remove contributor from anything else, both groups and roles
                //System.out.println("set none");
                PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel("None",
                        IdManager.createUuid(), noneMask);
                permissionLevelManager.savePermissionLevel(noneLevel);

                membershipItem = permissionLevelManager.createDBMembershipItem(item.getName(), "None",
                        item.getType());
                membershipItem.setPermissionLevel(noneLevel);
                permissionLevelManager.saveDBMembershipItem(membershipItem);
                membershipItemSet.add(membershipItem);
                deleteItemSet.add(item);
            } else { // for other permission types, leave as is
                //System.out.println("leave alone");
                membershipItemSet.add(item);
            }
        }
        // do any left
        for (String name : groupNames) {
            //System.out.println("make contributor: " + name);
            PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel("Contributor",
                    IdManager.createUuid(), contributorMask);
            permissionLevelManager.savePermissionLevel(contributorLevel);

            membershipItem = permissionLevelManager.createDBMembershipItem(name, "Contributor",
                    MembershipItem.TYPE_GROUP);
            membershipItem.setPermissionLevel(contributorLevel);
            permissionLevelManager.saveDBMembershipItem(membershipItem);
            membershipItemSet.add(membershipItem);
        }
        if (!haveOwner) {
            //System.out.println("add owner");
            PermissionLevel ownerLevel = permissionLevelManager.createPermissionLevel("Owner",
                    IdManager.createUuid(), ownerMask);
            permissionLevelManager.savePermissionLevel(ownerLevel);

            membershipItem = permissionLevelManager.createDBMembershipItem(maintainRole, "Owner",
                    MembershipItem.TYPE_ROLE);
            membershipItem.setPermissionLevel(ownerLevel);
            permissionLevelManager.saveDBMembershipItem(membershipItem);
            membershipItemSet.add(membershipItem);
            roles.remove(maintainRole); // we've processed this, so don't make it None
        }
        for (String name : roles) {
            //System.out.println("make none " + name);
            PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel("None",
                    IdManager.createUuid(), noneMask);
            permissionLevelManager.savePermissionLevel(noneLevel);

            membershipItem = permissionLevelManager.createDBMembershipItem(name, "None",
                    MembershipItem.TYPE_ROLE);
            membershipItem.setPermissionLevel(noneLevel);
            permissionLevelManager.saveDBMembershipItem(membershipItem);
            membershipItemSet.add(membershipItem);
        }
    } else {
        // Setgroups with a null list: we set all contributor entries to none, and then set all roles
        //    to contributor.  However we don't touch Owners.

        for (DBMembershipItem item : oldMembershipItemSet) {
            if (item.getPermissionLevelName().equals("Owner"))
                haveOwner = true;
            if (item.getType().equals(MembershipItem.TYPE_ROLE) && roles.contains(item.getName()))
                roles.remove(item.getName()); // we've seen it, don't need to add
            if (item.getType().equals(MembershipItem.TYPE_ROLE)
                    && !item.getPermissionLevelName().equals("Owner")) {
                // turn all roles into contributor, unless already owner
                PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel("Contributor",
                        IdManager.createUuid(), contributorMask);
                permissionLevelManager.savePermissionLevel(contributorLevel);

                membershipItem = permissionLevelManager.createDBMembershipItem(item.getName(), "Contributor",
                        item.getType());
                membershipItem.setPermissionLevel(contributorLevel);
                permissionLevelManager.saveDBMembershipItem(membershipItem);
                membershipItemSet.add(membershipItem);
                deleteItemSet.add(item);
            } else if (item.getPermissionLevelName().equals("Contributor")) {
                // kill other contributors
                PermissionLevel noneLevel = permissionLevelManager.createPermissionLevel("None",
                        IdManager.createUuid(), noneMask);
                permissionLevelManager.savePermissionLevel(noneLevel);

                membershipItem = permissionLevelManager.createDBMembershipItem(item.getName(), "None",
                        item.getType());
                membershipItem.setPermissionLevel(noneLevel);
                permissionLevelManager.saveDBMembershipItem(membershipItem);
                membershipItemSet.add(membershipItem);
                deleteItemSet.add(item);
            } else { // for other permission types, leave as is
                membershipItemSet.add(item);
            }
        }
        if (!haveOwner) {
            //System.out.println("make owner " +maintainRole);
            PermissionLevel ownerLevel = permissionLevelManager.createPermissionLevel("Owner",
                    IdManager.createUuid(), ownerMask);
            permissionLevelManager.savePermissionLevel(ownerLevel);

            membershipItem = permissionLevelManager.createDBMembershipItem(maintainRole, "Owner",
                    MembershipItem.TYPE_ROLE);
            membershipItem.setPermissionLevel(ownerLevel);
            permissionLevelManager.saveDBMembershipItem(membershipItem);
            membershipItemSet.add(membershipItem);
            roles.remove(maintainRole); // we've processed this, so don't make it None
        }
        for (String name : roles) {
            //System.out.println("make contributor: " + name);
            PermissionLevel contributorLevel = permissionLevelManager.createPermissionLevel("Contributor",
                    IdManager.createUuid(), contributorMask);
            permissionLevelManager.savePermissionLevel(contributorLevel);

            membershipItem = permissionLevelManager.createDBMembershipItem(name, "Contributor",
                    MembershipItem.TYPE_ROLE);
            membershipItem.setPermissionLevel(contributorLevel);
            permissionLevelManager.saveDBMembershipItem(membershipItem);
            membershipItemSet.add(membershipItem);
        }
    }

    //System.out.println("delete " + deleteItemSet);
    permissionLevelManager.deleteMembershipItems(deleteItemSet);

    //System.out.println("new membership list ");
    //for (DBMembershipItem item: (Set<DBMembershipItem>)membershipItemSet) {
    //    System.out.println(item.getPermissionLevelName() + " " + item.getType() + " " + item.getName());
    //}

    topic.setMembershipItemSet(membershipItemSet);

    // should do
    //discussionForumManager.saveTopic((DiscussionTopic) topic);
    // but that uses saveOrUpdate, which gives an error because
    // we typically have more than one copy of the topic in the session.
    // The only fix that works without modifying code I can't touch
    // is to do merge rather than saveOrUpdate. But that means I
    // have to do my own hibernate save rather than using the
    // API's savetopic.  I checked the code for saveTopic, and
    // when you're dealing with an existing topic, all you need
    // is the save. The real saveTopic code just sets up fields
    // that would be null if it's a new topic. Of course the changed
    // object won't be visible in other sesssions. So if you try
    // getGroups after doing the save, and you're in the same session,
    // which is typically the same request, you'll get the old value.
    // Sorry about that.

    Session session = sessionFactory.openSession();
    Transaction tx = null;

    try {
        tx = session.beginTransaction();

        session.merge(topic);
        tx.commit();

    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
    } finally {
        if (session != null)
            session.close();
    }

}

From source file:org.springframework.orm.hibernate3.HibernateTemplate.java

License:Apache License

@Override
public <T> T merge(final T entity) throws DataAccessException {
    return executeWithNativeSession(new HibernateCallback<T>() {
        @Override/*from www .j  a  v a 2  s  .c o m*/
        @SuppressWarnings("unchecked")
        public T doInHibernate(Session session) throws HibernateException {
            checkWriteOperationAllowed(session);
            return (T) session.merge(entity);
        }
    });
}

From source file:org.springframework.orm.hibernate3.StatelessHibernateTemplate.java

License:Apache License

public Object merge(final Object entity) throws DataAccessException {
    return execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException {
            checkWriteOperationAllowed(session);
            return session.merge(entity);
        }//from  ww w .ja va  2 s  .c  o  m
    }, true);
}

From source file:org.uclab.mm.kcl.edkat.dao.ConclusionDAOImpl.java

License:Apache License

/**
 * This function is the implementation for add new Conclusion
 * @param objConclusion/*from  www. ja  v a  2 s .c o  m*/
 * @return object of Conclusion
*/
public Conclusion addConclusion(Conclusion objConclusion) {

    try {
        Session session = this.sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        objConclusion = (Conclusion) session.merge(objConclusion);
        tx.commit();
        session.close();
        logger.info("Conclusion saved successfully, Conclusion Details=" + objConclusion);
        return objConclusion;
    } catch (Exception ex) {
        logger.info("Error occured in adding Conclusion, Error Details=" + ex.getMessage());
        return objConclusion;
    }

}