alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (FirstLevelCache.java)

This example Hibernate source code file (FirstLevelCache.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

cache, caching, first, first, firstlevelcache, level, level, log, map, number, number, object, object, string, util

The Hibernate FirstLevelCache.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Middleware LLC.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.envers.reader;
import static org.hibernate.envers.tools.Tools.newHashMap;
import static org.hibernate.envers.tools.Triple.make;
import java.util.Map;
import org.hibernate.envers.internal.EnversMessageLogger;
import org.hibernate.envers.tools.Triple;
import org.jboss.logging.Logger;

/**
 * First level cache for versioned entities, versions reader-scoped. Each entity is uniquely identified by a
 * revision number and entity id.
 * @author Adam Warski (adam at warski dot org)
 * @author Hernán Chanfreau
 */
public class FirstLevelCache {

    public static final EnversMessageLogger LOG = Logger.getMessageLogger(EnversMessageLogger.class, FirstLevelCache.class.getName());

    /**
     * cache for resolve an object for a given id, revision and entityName.
     */
    private final Map<Triple cache;

    /**
     * used to resolve the entityName for a given id, revision and entity.
     */
    private final Map<Triple entityNameCache;

    public FirstLevelCache() {
        cache = newHashMap();
        entityNameCache = newHashMap();
    }

    public Object get(String entityName, Number revision, Object id) {
        LOG.debugf("Resolving object from First Level Cache: EntityName:%s - primaryKey:%s - revision:%s", entityName, id, revision);
        return cache.get(make(entityName, revision, id));
    }

    public void put(String entityName, Number revision, Object id, Object entity) {
        LOG.debugf("Caching entity on First Level Cache:  - primaryKey:%s - revision:%s - entityName:%s", id, revision, entityName);
        cache.put(make(entityName, revision, id), entity);
    }

    public boolean contains(String entityName, Number revision, Object id) {
        return cache.containsKey(make(entityName, revision, id));
    }

    /**
     * Adds the entityName into the cache. The key is a triple make with primaryKey, revision and entity
     * @param entityName, value of the cache
     * @param id, primaryKey
     * @param revision, revision number
     * @param entity, object retrieved by envers
     */
    public void putOnEntityNameCache(Object id, Number revision, Object entity, String entityName) {
        LOG.debugf("Caching entityName on First Level Cache:  - primaryKey:%s - revision:%s - entity:%s -> entityName:%s",
                   id,
                   revision,
                   entity.getClass().getName(),
                   entityName);
    	entityNameCache.put(make(id, revision, entity), entityName);
    }

    /**
     * Gets the entityName from the cache. The key is a triple make with primaryKey, revision and entity
     * @param entityName, value of the cache
     * @param id, primaryKey
     * @param revision, revision number
     * @param entity, object retrieved by envers
     */
    public String getFromEntityNameCache(Object id, Number revision, Object entity) {
        LOG.debugf("Trying to resolve entityName from First Level Cache: - primaryKey:%s - revision:%s - entity:%s",
                   id,
                   revision,
                   entity);
    	return entityNameCache.get(make(id, revision, entity));
    }

	/**
	 * @param id
	 *            , primaryKey
	 * @param revision
	 *            , revision number
	 * @param entity
	 *            , object retrieved by envers
	 * @return true if entityNameCache contains the triple
	 */
    public boolean containsEntityName(Object id, Number revision, Object entity) {
    	return entityNameCache.containsKey(make(id, revision, entity));
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate FirstLevelCache.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.