Example usage for org.hibernate.criterion Restrictions naturalId

List of usage examples for org.hibernate.criterion Restrictions naturalId

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions naturalId.

Prototype

public static NaturalIdentifier naturalId() 

Source Link

Document

Consider using any of the natural id based loading stuff from session instead, especially in cases where the restriction is the full set of natural id values.

Usage

From source file:org.infinispan.test.hibernate.cache.commons.functional.cluster.NaturalIdInvalidationTest.java

License:LGPL

private void deleteCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {//from  w ww . j a  va2 s  .co  m
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Citizen c = (Citizen) criteria.uniqueResult();
        s.delete(c);
    });
}

From source file:org.infinispan.test.hibernate.cache.commons.functional.ReadWriteTest.java

License:LGPL

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();/* www.j  a v a  2 s. c  om*/

    // Clear the cache before the transaction begins
    cleanupCache();
    TIME_SERVICE.advance(1);

    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);

        Statistics stats = sessionFactory().getStatistics();
        stats.setStatisticsEnabled(true);
        stats.clear();
        assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());

        // first query
        List results = criteria.list();
        assertEquals(1, results.size());
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());

        // query a second time - result should be cached in session
        criteria.list();
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());

        // cleanup
        markRollbackOnly(s);
    });
}

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

License:Apache License

public Item findItemByUid(String uid) {
    try {//from  www. j av a  2  s . co  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.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();
}

From source file:org.riotfamily.dbmsgsrc.model.MessageBundleEntry.java

License:Apache License

public static MessageBundleEntry loadByBundleAndCode(String bundle, String code) {
    return (MessageBundleEntry) getSession().createCriteria(MessageBundleEntry.class).setCacheable(true)
            .setCacheRegion("messages").add(Restrictions.naturalId().set("bundle", bundle).set("code", code))
            .uniqueResult();//from ww  w .  jav a  2  s.co  m
}

From source file:org.riotfamily.dbmsgsrc.model.MessageBundleEntry.java

License:Apache License

@SuppressWarnings("unchecked")
public static void removeEmptyEntries(String bundle) {
    List<MessageBundleEntry> entries = getSession().createCriteria(MessageBundleEntry.class).setCacheable(true)
            .setCacheRegion("messages").add(Restrictions.sizeLe("messages", 1))
            .add(Restrictions.naturalId().set("bundle", bundle)).list();

    for (MessageBundleEntry entry : entries) {
        entry.delete();/*  w ww.  j av a 2  s  .c o  m*/
    }

}

From source file:org.zanata.action.ProjectHome.java

License:Open Source License

@Override
public NaturalIdentifier getNaturalId() {
    return Restrictions.naturalId().set("slug", getSlug());
}

From source file:org.zanata.action.ProjectIterationHome.java

License:Open Source License

@Override
public NaturalIdentifier getNaturalId() {
    return Restrictions.naturalId().set("slug", slug).set("project", projectDAO.getBySlug(projectSlug));
}

From source file:org.zanata.action.VersionHome.java

License:Open Source License

@Override
public NaturalIdentifier getNaturalId() {
    return Restrictions.naturalId().set("slug", getSlug()).set("project",
            projectDAO.getBySlug(getProjectSlug()));
}

From source file:org.zanata.model.TransMemoryJPATest.java

License:Open Source License

private TransMemory loadTM(String slug) {
    return (TransMemory) super.getSession().createCriteria(TransMemory.class)
            .add(Restrictions.naturalId().set("slug", slug)).uniqueResult();
}