Example usage for org.hibernate FlushMode MANUAL

List of usage examples for org.hibernate FlushMode MANUAL

Introduction

In this page you can find the example usage for org.hibernate FlushMode MANUAL.

Prototype

FlushMode MANUAL

To view the source code for org.hibernate FlushMode MANUAL.

Click Source Link

Document

The Session is only ever flushed when Session#flush is explicitly called by the application.

Usage

From source file:org.osaf.cosmo.dao.hibernate.ContentDaoImpl.java

License:Apache License

public Set<ContentItem> loadChildren(CollectionItem collection, Date timestamp) {
    try {/*from  w  ww.  jav a2 s  . c  om*/
        Set<ContentItem> children = new HashSet<ContentItem>();
        Query query = null;

        // use custom HQL query that will eager fetch all associations
        if (timestamp == null)
            query = getSession().getNamedQuery("contentItem.by.parent").setParameter("parent", collection);
        else
            query = getSession().getNamedQuery("contentItem.by.parent.timestamp")
                    .setParameter("parent", collection).setParameter("timestamp", timestamp);

        query.setFlushMode(FlushMode.MANUAL);
        List results = query.list();
        for (Iterator it = results.iterator(); it.hasNext();) {
            ContentItem content = (ContentItem) it.next();
            initializeItem(content);
            children.add(content);
        }

        return children;

    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    }
}

From source file:org.osaf.cosmo.dao.hibernate.ContentDaoImpl.java

License:Apache License

protected void checkForDuplicateICalUid(ICalendarItem item, CollectionItem parent) {

    // TODO: should icalUid be required?  Currrently its not and all
    // items created by the webui dont' have it.
    if (item.getIcalUid() == null)
        return;/*from   w  w w  . j  a  v  a2  s . c om*/

    // ignore modifications
    if (item instanceof NoteItem && ((NoteItem) item).getModifies() != null)
        return;

    // Lookup item by parent/icaluid
    Query hibQuery = null;
    if (item instanceof NoteItem)
        hibQuery = getSession().getNamedQuery("noteItemId.by.parent.icaluid")
                .setParameter("parentid", getBaseModelObject(parent).getId())
                .setParameter("icaluid", item.getIcalUid());
    else
        hibQuery = getSession().getNamedQuery("icalendarItem.by.parent.icaluid")
                .setParameter("parentid", getBaseModelObject(parent).getId())
                .setParameter("icaluid", item.getIcalUid());

    hibQuery.setFlushMode(FlushMode.MANUAL);

    Long itemId = (Long) hibQuery.uniqueResult();

    // if icaluid is in use throw exception
    if (itemId != null) {
        // If the note is new, then its a duplicate icaluid
        if (getBaseModelObject(item).getId() == -1) {
            Item dup = (Item) getSession().load(HibItem.class, itemId);
            throw new IcalUidInUseException(
                    "iCal uid" + item.getIcalUid() + " already in use for collection " + parent.getUid(),
                    item.getUid(), dup.getUid());
        }
        // If the note exists and there is another note with the same
        // icaluid, then its a duplicate icaluid
        if (getBaseModelObject(item).getId().equals(itemId)) {
            Item dup = (Item) getSession().load(HibItem.class, itemId);
            throw new IcalUidInUseException(
                    "iCal uid" + item.getIcalUid() + " already in use for collection " + parent.getUid(),
                    item.getUid(), dup.getUid());
        }
    }
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

public Item findItemByUid(String uid) {
    try {/*from  w ww  . j a  va2s .c  o  m*/
        // prevent auto flushing when looking up item by uid
        getSession().setFlushMode(FlushMode.MANUAL);

        // take advantage of optimized caching with naturalId
        Item item = (Item) getSession().createCriteria(HibItem.class)
                .add(Restrictions.naturalId().set("uid", uid)).setCacheable(true).setFlushMode(FlushMode.MANUAL)
                .uniqueResult();

        // Prevent proxied object from being returned
        if (item instanceof HibernateProxy)
            item = (Item) ((HibernateProxy) item).getHibernateLazyInitializer().getImplementation();

        return item;
    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    }
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

public Ticket findTicket(String key) {
    if (key == null)
        throw new IllegalArgumentException("key cannot be null");

    try {/*from   w w  w.  j a v  a 2 s .  co  m*/
        // prevent auto flushing when looking up ticket
        getSession().setFlushMode(FlushMode.MANUAL);
        Query hibQuery = getSession().getNamedQuery("ticket.by.key").setParameter("key", key);
        hibQuery.setCacheable(true);
        hibQuery.setFlushMode(FlushMode.MANUAL);
        return (Ticket) hibQuery.uniqueResult();
    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    }
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

protected Item findItemByParentAndName(Long userDbId, Long parentDbId, String name) {
    Query hibQuery = null;//from   w  w  w. j  a  va 2  s.  com
    if (parentDbId != null) {
        hibQuery = getSession().getNamedQuery("item.by.ownerId.parentId.name").setParameter("ownerid", userDbId)
                .setParameter("parentid", parentDbId).setParameter("name", name);

    } else {
        hibQuery = getSession().getNamedQuery("item.by.ownerId.nullParent.name")
                .setParameter("ownerid", userDbId).setParameter("name", name);
    }
    hibQuery.setFlushMode(FlushMode.MANUAL);
    return (Item) hibQuery.uniqueResult();
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

protected Item findItemByParentAndNameMinusItem(Long userDbId, Long parentDbId, String name, Long itemId) {
    Query hibQuery = null;//from   www .ja  va2 s.  co m
    if (parentDbId != null) {
        hibQuery = getSession().getNamedQuery("item.by.ownerId.parentId.name.minusItem")
                .setParameter("itemid", itemId).setParameter("ownerid", userDbId)
                .setParameter("parentid", parentDbId).setParameter("name", name);
    } else {
        hibQuery = getSession().getNamedQuery("item.by.ownerId.nullParent.name.minusItem")
                .setParameter("itemid", itemId).setParameter("ownerid", userDbId).setParameter("name", name);
    }
    hibQuery.setFlushMode(FlushMode.MANUAL);
    return (Item) hibQuery.uniqueResult();
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

protected HomeCollectionItem findRootItem(Long dbUserId) {
    Query hibQuery = getSession().getNamedQuery("homeCollection.by.ownerId").setParameter("ownerid", dbUserId);
    hibQuery.setCacheable(true);//from  www .  j  a  v a2  s  .co m
    hibQuery.setFlushMode(FlushMode.MANUAL);

    return (HomeCollectionItem) hibQuery.uniqueResult();
}

From source file:org.osaf.cosmo.dao.hibernate.ItemDaoImpl.java

License:Apache License

protected void checkForDuplicateUid(Item item) {
    // verify uid not in use
    if (item.getUid() != null) {

        // Lookup item by uid
        Query hibQuery = getSession().getNamedQuery("itemid.by.uid").setParameter("uid", item.getUid());
        hibQuery.setFlushMode(FlushMode.MANUAL);

        Long itemId = (Long) hibQuery.uniqueResult();

        // if uid is in use throw exception
        if (itemId != null) {
            throw new UidInUseException(item.getUid(), "uid " + item.getUid() + " already in use");
        }//  w ww  .  j av a2 s . c om
    }
}

From source file:org.osaf.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

public User updateUser(User user) {
    try {/*from  w  w  w.ja  v a2s . c o  m*/
        // prevent auto flushing when querying for existing users
        getSession().setFlushMode(FlushMode.MANUAL);

        User findUser = findUserByUsernameOrEmailIgnoreCaseAndId(getBaseModelObject(user).getId(),
                user.getUsername(), user.getEmail());

        if (findUser != null) {
            if (findUser.getEmail().equals(user.getEmail()))
                throw new DuplicateEmailException(user);
            else
                throw new DuplicateUsernameException(user);
        }

        user.updateTimestamp();
        getSession().update(user);
        getSession().flush();

        return user;
    } catch (HibernateException e) {
        getSession().clear();
        throw convertHibernateAccessException(e);
    } catch (InvalidStateException ise) {
        logInvalidStateException(ise);
        throw ise;
    }
}

From source file:org.osaf.cosmo.dao.hibernate.UserDaoImpl.java

License:Apache License

private User findUserByUsername(String username) {
    // take advantage of optimized caching with naturalId
    return (User) getSession().createCriteria(HibUser.class)
            .add(Restrictions.naturalId().set("username", username)).setCacheable(true)
            .setFlushMode(FlushMode.MANUAL).uniqueResult();
}