ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [08/50] [abbrv] incubator-ignite git commit: # ignite-63
Date Fri, 23 Jan 2015 09:02:16 GMT
# ignite-63


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/ed7736f3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/ed7736f3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/ed7736f3

Branch: refs/heads/ignite-63
Commit: ed7736f3f80309631f8985d464a7d573bf9cc075
Parents: fbc373c
Author: sboikov <sboikov@gridgain.com>
Authored: Fri Jan 23 10:35:14 2015 +0300
Committer: sboikov <sboikov@gridgain.com>
Committed: Fri Jan 23 10:35:14 2015 +0300

----------------------------------------------------------------------
 .../hibernate/example-hibernate-L2-cache.xml    |    2 +-
 ...idHibernateAbstractRegionAccessStrategy.java |   97 +
 .../GridHibernateAccessStrategyAdapter.java     |  367 ++++
 .../GridHibernateCollectionRegion.java          |   99 +
 .../hibernate/GridHibernateEntityRegion.java    |  110 +
 .../GridHibernateGeneralDataRegion.java         |   69 +
 .../hibernate/GridHibernateNaturalIdRegion.java |   98 +
 .../GridHibernateNonStrictAccessStrategy.java   |  220 ++
 .../GridHibernateQueryResultsRegion.java        |   71 +
 .../GridHibernateReadOnlyAccessStrategy.java    |  106 +
 .../GridHibernateReadWriteAccessStrategy.java   |  282 +++
 .../cache/hibernate/GridHibernateRegion.java    |  101 +
 .../hibernate/GridHibernateRegionFactory.java   |  230 +++
 .../GridHibernateTimestampsRegion.java          |   40 +
 ...ridHibernateTransactionalAccessStrategy.java |  139 ++
 .../GridHibernateTransactionalDataRegion.java   |   96 +
 .../apache/ignite/cache/hibernate/package.html  |   26 +
 .../hibernate/GridCacheHibernateBlobStore.java  |  592 ++++++
 .../GridCacheHibernateBlobStoreEntry.hbm.xml    |   29 +
 .../GridCacheHibernateBlobStoreEntry.java       |   86 +
 .../ignite/cache/store/hibernate/package.html   |   23 +
 ...idHibernateAbstractRegionAccessStrategy.java |   97 -
 .../GridHibernateAccessStrategyAdapter.java     |  367 ----
 .../GridHibernateCollectionRegion.java          |   99 -
 .../hibernate/GridHibernateEntityRegion.java    |  110 -
 .../GridHibernateGeneralDataRegion.java         |   69 -
 .../hibernate/GridHibernateNaturalIdRegion.java |   98 -
 .../GridHibernateNonStrictAccessStrategy.java   |  220 --
 .../GridHibernateQueryResultsRegion.java        |   71 -
 .../GridHibernateReadOnlyAccessStrategy.java    |  106 -
 .../GridHibernateReadWriteAccessStrategy.java   |  282 ---
 .../cache/hibernate/GridHibernateRegion.java    |  101 -
 .../hibernate/GridHibernateRegionFactory.java   |  230 ---
 .../GridHibernateTimestampsRegion.java          |   40 -
 ...ridHibernateTransactionalAccessStrategy.java |  139 --
 .../GridHibernateTransactionalDataRegion.java   |   96 -
 .../gridgain/grid/cache/hibernate/package.html  |   26 -
 .../hibernate/GridCacheHibernateBlobStore.java  |  592 ------
 .../GridCacheHibernateBlobStoreEntry.hbm.xml    |   29 -
 .../GridCacheHibernateBlobStoreEntry.java       |   86 -
 .../grid/cache/store/hibernate/package.html     |   23 -
 ...idHibernateL2CacheConfigurationSelfTest.java |  379 ++++
 .../hibernate/GridHibernateL2CacheSelfTest.java | 1922 ++++++++++++++++++
 ...idHibernateL2CacheTransactionalSelfTest.java |  135 ++
 .../GridCacheHibernateBlobStoreSelfTest.java    |  108 +
 .../cache/store/hibernate/hibernate.cfg.xml     |   40 +
 .../ignite/cache/store/hibernate/package.html   |   23 +
 .../bamboo/GridHibernateTestSuite.java          |    4 +-
 ...idHibernateL2CacheConfigurationSelfTest.java |  379 ----
 .../hibernate/GridHibernateL2CacheSelfTest.java | 1922 ------------------
 ...idHibernateL2CacheTransactionalSelfTest.java |  136 --
 .../GridCacheHibernateBlobStoreSelfTest.java    |  108 -
 .../cache/store/hibernate/hibernate.cfg.xml     |   40 -
 .../grid/cache/store/hibernate/package.html     |   23 -
 pom.xml                                         |    4 +-
 55 files changed, 5493 insertions(+), 5494 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/examples/config/hibernate/example-hibernate-L2-cache.xml
----------------------------------------------------------------------
diff --git a/examples/config/hibernate/example-hibernate-L2-cache.xml b/examples/config/hibernate/example-hibernate-L2-cache.xml
index 7eac93f..86b224a 100644
--- a/examples/config/hibernate/example-hibernate-L2-cache.xml
+++ b/examples/config/hibernate/example-hibernate-L2-cache.xml
@@ -42,7 +42,7 @@
         <property name="generate_statistics">true</property>
 
         <!-- Specify GridGain as L2 cache provider. -->
-        <property name="cache.region.factory_class">org.gridgain.grid.cache.hibernate.GridHibernateRegionFactory</property>
+        <property name="cache.region.factory_class">org.apache.ignite.cache.hibernate.GridHibernateRegionFactory</property>
 
         <!-- Specify the name of the grid, that will be used for second level caching. -->
         <property name="org.gridgain.hibernate.grid_name">hibernate-grid</property>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAbstractRegionAccessStrategy.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAbstractRegionAccessStrategy.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAbstractRegionAccessStrategy.java
new file mode 100644
index 0000000..dd09c4a
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAbstractRegionAccessStrategy.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+import org.jetbrains.annotations.*;
+
+/**
+ * Implementation of L2 cache access strategy delegating to {@link GridHibernateAccessStrategyAdapter}.
+ */
+public abstract class GridHibernateAbstractRegionAccessStrategy implements RegionAccessStrategy {
+    /** */
+    protected final GridHibernateAccessStrategyAdapter stgy;
+
+    /**
+     * @param stgy Access strategy implementation.
+     */
+    protected GridHibernateAbstractRegionAccessStrategy(GridHibernateAccessStrategyAdapter stgy) {
+        this.stgy = stgy;
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override public Object get(Object key, long txTs) throws CacheException {
+        return stgy.get(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean putFromLoad(Object key, Object val, long txTs, Object ver) throws CacheException {
+        stgy.putFromLoad(key, val);
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean putFromLoad(Object key, Object val, long txTs, Object ver, boolean minimalPutOverride)
+        throws CacheException {
+        stgy.putFromLoad(key, val, minimalPutOverride);
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override public SoftLock lockItem(Object key, Object ver) throws CacheException {
+        return stgy.lock(key);
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override public SoftLock lockRegion() throws CacheException {
+        return stgy.lockRegion();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void unlockRegion(SoftLock lock) throws CacheException {
+        stgy.unlockRegion(lock);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void unlockItem(Object key, SoftLock lock) throws CacheException {
+        stgy.unlock(key, lock);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void remove(Object key) throws CacheException {
+        stgy.remove(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void removeAll() throws CacheException {
+        stgy.removeAll();
+    }
+
+    /** {@inheritDoc} */
+    @Override public void evict(Object key) throws CacheException {
+        stgy.evict(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void evictAll() throws CacheException {
+        stgy.evictAll();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAccessStrategyAdapter.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAccessStrategyAdapter.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAccessStrategyAdapter.java
new file mode 100644
index 0000000..84f5114
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateAccessStrategyAdapter.java
@@ -0,0 +1,367 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.resources.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+import org.jetbrains.annotations.*;
+
+import java.io.*;
+import java.util.concurrent.*;
+
+/**
+ * Common interface used to implement Hibernate L2 cache access strategies ({@link RegionAccessStrategy},
+ * {@link EntityRegionAccessStrategy} and {@link CollectionRegionAccessStrategy}).
+ * <p>
+ * The expected sequences of steps related to various CRUD operations executed by Hibernate are:
+ * <p>
+ * Insert:
+ * <ul>
+ *     <li>Start DB transaction.</li>
+ *     <li>Execute database insert.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#insert}.</li>
+ *     <li>Commit DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#afterInsert}.</li>
+ * </ul>
+ * In case if some step fails and DB transaction is rolled back then
+ * {@link GridHibernateAccessStrategyAdapter#afterInsert} is not called.
+ * <p>
+ * Update:
+ * <ul>
+ *     <li>Start DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#lock}.</li>
+ *     <li>Execute database update.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#update}.</li>
+ *     <li>Commit DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#afterUpdate}.</li>
+ * </ul>
+ * In case if {@link GridHibernateAccessStrategyAdapter#lock} was called, but some other step fails and DB
+ * transaction is rolled back then {@link GridHibernateAccessStrategyAdapter#unlock} is called for all locked keys.
+ * <p>
+ * Delete:
+ * <ul>
+ *     <li>Start DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#lock} for removing key.</li>
+ *     <li>Execute database delete.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#remove}.</li>
+ *     <li>Commit DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#unlock}.</li>
+ * </ul>
+ * In case if {@link GridHibernateAccessStrategyAdapter#lock} was called, but some other step fails and DB
+ * transaction is rolled back then {@link GridHibernateAccessStrategyAdapter#unlock} is called for all locked keys.
+ * <p>
+ * In case if custom SQL update query is executed Hibernate clears entire cache region,
+ * for this case operations sequence is:
+ * <ul>
+ *     <li>Start DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#lockRegion}.</li>
+ *     <li>Execute database query.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#removeAll}.</li>
+ *     <li>Commit DB transaction.</li>
+ *     <li>Call {@link GridHibernateAccessStrategyAdapter#unlockRegion}.</li>
+ * </ul>
+ */
+public abstract class GridHibernateAccessStrategyAdapter {
+    /** */
+    protected final GridCache<Object, Object> cache;
+
+    /** Grid. */
+    protected final Ignite ignite;
+
+    /** */
+    protected final IgniteLogger log;
+
+    /**
+     * @param ignite Grid.
+     * @param cache Cache.
+     */
+    protected GridHibernateAccessStrategyAdapter(Ignite ignite, GridCache<Object, Object> cache) {
+        this.cache = cache;
+        this.ignite = ignite;
+
+        log = ignite.log();
+    }
+
+    /**
+     * Gets value from cache. Used by {@link RegionAccessStrategy#get}.
+     *
+     * @param key Key.
+     * @return Cached value.
+     * @throws CacheException If failed.
+     */
+    @Nullable protected Object get(Object key) throws CacheException {
+        try {
+            return cache.get(key);
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /**
+     * Puts in cache value loaded from the database. Used by {@link RegionAccessStrategy#putFromLoad}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @param minimalPutOverride MinimalPut flag
+     * @throws CacheException If failed.
+     */
+    protected void putFromLoad(Object key, Object val, boolean minimalPutOverride) throws CacheException {
+        putFromLoad(key, val);
+    }
+
+    /**
+     * Puts in cache value loaded from the database. Used by {@link RegionAccessStrategy#putFromLoad}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @throws CacheException If failed.
+     */
+    protected void putFromLoad(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /**
+     * Called during database transaction execution before Hibernate attempts to update or remove given key.
+     * Used by {@link RegionAccessStrategy#lockItem}.
+     *
+     * @param key Key.
+     * @return Lock representation or {@code null}.
+     * @throws CacheException If failed.
+     */
+    @Nullable protected abstract SoftLock lock(Object key) throws CacheException;
+
+    /**
+     * Called after Hibernate failed to update or successfully removed given key.
+     * Used by {@link RegionAccessStrategy#unlockItem}.
+     *
+     * @param key Key.
+     * @param lock The lock previously obtained from {@link #lock}
+     * @throws CacheException If failed.
+     */
+    protected abstract void unlock(Object key, SoftLock lock) throws CacheException;
+
+    /**
+     * Called after Hibernate updated object in the database but before transaction completed.
+     * Used by {@link EntityRegionAccessStrategy#update} and {@link NaturalIdRegionAccessStrategy#update}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @return {@code True} if operation updated cache.
+     * @throws CacheException If failed.
+     */
+    protected abstract boolean update(Object key, Object val) throws CacheException;
+
+    /**
+     * Called after Hibernate updated object in the database and transaction successfully completed.
+     * Used by {@link EntityRegionAccessStrategy#afterUpdate} and {@link NaturalIdRegionAccessStrategy#afterUpdate}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @param lock The lock previously obtained from {@link #lock}
+     * @return {@code True} if operation updated cache.
+     * @throws CacheException If failed.
+     */
+    protected abstract boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException;
+
+    /**
+     * Called after Hibernate inserted object in the database but before transaction completed.
+     * Used by {@link EntityRegionAccessStrategy#insert} and {@link NaturalIdRegionAccessStrategy#insert}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @return {@code True} if operation updated cache.
+     * @throws CacheException If failed.
+     */
+    protected abstract boolean insert(Object key, Object val) throws CacheException;
+
+    /**
+     * Called after Hibernate inserted object in the database and transaction successfully completed.
+     * Used by {@link EntityRegionAccessStrategy#afterInsert} and {@link NaturalIdRegionAccessStrategy#afterInsert}.
+     *
+     * @param key Key.
+     * @param val Value.
+     * @return {@code True} if operation updated cache.
+     * @throws CacheException If failed.
+     */
+    protected abstract boolean afterInsert(Object key, Object val) throws CacheException;
+
+    /**
+     * Called after Hibernate removed object from database but before transaction completed.
+     * Used by {@link RegionAccessStrategy#remove}.
+     *
+     * @param key Key,
+     * @throws CacheException If failed.
+     */
+    protected abstract void remove(Object key) throws CacheException;
+
+    /**
+     * Called to remove object from cache without regard to transaction.
+     * Used by {@link RegionAccessStrategy#evict}.
+     *
+     * @param key Key.
+     * @throws CacheException If failed.
+     */
+    protected void evict(Object key) throws CacheException {
+        evict(ignite, cache, key);
+    }
+
+    /**
+     * Called to remove all data from cache without regard to transaction.
+     * Used by {@link RegionAccessStrategy#evictAll}.
+     *
+     * @throws CacheException If failed.
+     */
+    protected void evictAll() throws CacheException {
+        evictAll(cache);
+    }
+
+    /**
+     * Called during database transaction execution before Hibernate executed
+     * update operation which should invalidate entire cache region.
+     * Used by {@link RegionAccessStrategy#lockRegion}.
+     *
+     * @throws CacheException If failed.
+     * @return Lock representation or {@code null}.
+     */
+    @Nullable protected SoftLock lockRegion() throws CacheException {
+        return null;
+    }
+
+    /**
+     * Called after transaction clearing entire cache region completed.
+     * Used by {@link RegionAccessStrategy#unlockRegion}.
+     *
+     * @param lock The lock previously obtained from {@link #lockRegion}
+     * @throws CacheException If failed.
+     */
+    protected void unlockRegion(SoftLock lock) throws CacheException {
+        // No-op.
+    }
+
+    /**
+     * Called during database transaction execution to clear entire cache region after
+     * Hibernate executed database update, but before transaction completed.
+     * Used by {@link RegionAccessStrategy#removeAll}.
+     *
+     * @throws CacheException If failed.
+     */
+    protected final void removeAll() throws CacheException {
+        evictAll();
+    }
+
+    /**
+     * Called to remove object from cache without regard to transaction.
+     *
+     * @param ignite Grid.
+     * @param cache Cache.
+     * @param key Key.
+     * @throws CacheException If failed.
+     */
+    static void evict(Ignite ignite, GridCacheProjection<Object,Object> cache, Object key) throws CacheException {
+        try {
+            ignite.compute(cache.gridProjection()).call(new ClearKeyCallable(key, cache.name()));
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /**
+     * Called to remove all data from cache without regard to transaction.
+     *
+     * @param cache Cache.
+     * @throws CacheException If failed.
+     */
+    static void evictAll(GridCacheProjection<Object,Object> cache) throws CacheException {
+        try {
+            cache.globalClearAll();
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /**
+     * Callable invalidates given key.
+     */
+    private static class ClearKeyCallable implements Callable<Void>, Externalizable {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @IgniteInstanceResource
+        private Ignite ignite;
+
+        /** */
+        private Object key;
+
+        /** */
+        private String cacheName;
+
+        /**
+         * Empty constructor required by {@link Externalizable}.
+         */
+        public ClearKeyCallable() {
+            // No-op.
+        }
+
+        /**
+         * @param key Key to clear.
+         * @param cacheName Cache name.
+         */
+        private ClearKeyCallable(Object key, String cacheName) {
+            this.key = key;
+            this.cacheName = cacheName;
+        }
+
+        /** {@inheritDoc} */
+        @Override public Void call() throws IgniteCheckedException {
+            GridCache<Object, Object> cache = ignite.cache(cacheName);
+
+            assert cache != null;
+
+            cache.clear(key);
+
+            return null;
+        }
+
+        /** {@inheritDoc} */
+        @Override public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeObject(key);
+
+            U.writeString(out, cacheName);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+            key = in.readObject();
+
+            cacheName = U.readString(in);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateCollectionRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateCollectionRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateCollectionRegion.java
new file mode 100644
index 0000000..6935f45
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateCollectionRegion.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+import org.hibernate.cache.spi.access.*;
+
+/**
+ * Implementation of {@link CollectionRegion}. This region is used to store collection data.
+ * <p>
+ * L2 cache for collection can be enabled in the Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entities. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *     &lt;mapping class="com.example.ChildEntity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with nonstrict-read-write access strategy for entities and collection. --&gt;
+ *     &lt;collection-cache collection="com.example.Entity" usage="nonstrict-read-write"/&gt;
+ *     &lt;collection-cache collection="com.example.ChildEntity" usage="nonstrict-read-write"/&gt;
+ *     &lt;collection-cache collection="com.example.Entity.children" usage="nonstrict-read-write"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache for collection can be enabled using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * public class Entity {
+ *    ...
+ *
+ *    &#064;javax.persistence.OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
+ *    &#064;javax.persistence.JoinColumn(name="PARENT_ID")
+ *    &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+ *    public List&lt;ChildEntity&gt; getChildren() {...}
+ * }
+ * </pre>
+ * Note: the collection cache does not cache the state of the actual entities in the cache, it caches only identifier
+ * values. For this reason, the collection cache should always be used in conjunction with
+ * the second-level cache for those entities expected to be cached as part of a collection cache.
+ */
+public class GridHibernateCollectionRegion extends GridHibernateTransactionalDataRegion implements CollectionRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache.
+     * @param dataDesc Region data description.
+     */
+    public GridHibernateCollectionRegion(GridHibernateRegionFactory factory, String name,
+        Ignite ignite, GridCache<Object, Object> cache, CacheDataDescription dataDesc) {
+        super(factory, name, ignite, cache, dataDesc);
+    }
+
+    /** {@inheritDoc} */
+    @Override public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
+        return new AccessStrategy(createAccessStrategy(accessType));
+    }
+
+    /**
+     * Collection region access strategy.
+     */
+    private class AccessStrategy extends GridHibernateAbstractRegionAccessStrategy
+        implements CollectionRegionAccessStrategy {
+        /**
+         * @param stgy Access strategy implementation.
+         */
+        private AccessStrategy(GridHibernateAccessStrategyAdapter stgy) {
+            super(stgy);
+        }
+
+        /** {@inheritDoc} */
+        @Override public CollectionRegion getRegion() {
+            return GridHibernateCollectionRegion.this;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateEntityRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateEntityRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateEntityRegion.java
new file mode 100644
index 0000000..1892931
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateEntityRegion.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+import org.hibernate.cache.spi.access.*;
+
+/**
+ * Implementation of {@link EntityRegion}. This region is used to store entity data.
+ * <p>
+ * L2 cache for entity can be enabled in the Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with nonstrict-read-write access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="nonstrict-read-write"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache for entity can be enabled using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+ * public class Entity { ... }
+ * </pre>
+ */
+public class GridHibernateEntityRegion extends GridHibernateTransactionalDataRegion implements EntityRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache,
+     * @param dataDesc Region data description.
+     */
+    public GridHibernateEntityRegion(GridHibernateRegionFactory factory, String name, Ignite ignite,
+        GridCache<Object, Object> cache, CacheDataDescription dataDesc) {
+        super(factory, name, ignite, cache, dataDesc);
+    }
+
+    /** {@inheritDoc} */
+    @Override public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
+        return new AccessStrategy(createAccessStrategy(accessType));
+    }
+
+    /**
+     * Entity region access strategy.
+     */
+    private class AccessStrategy extends GridHibernateAbstractRegionAccessStrategy
+        implements EntityRegionAccessStrategy {
+        /**
+         * @param stgy Access strategy implementation.
+         */
+        private AccessStrategy(GridHibernateAccessStrategyAdapter stgy) {
+            super(stgy);
+        }
+
+        /** {@inheritDoc} */
+        @Override public EntityRegion getRegion() {
+            return GridHibernateEntityRegion.this;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean insert(Object key, Object val, Object ver) throws CacheException {
+            return stgy.insert(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean afterInsert(Object key, Object val, Object ver) throws CacheException {
+            return stgy.afterInsert(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean update(Object key, Object val, Object currVer, Object previousVer)
+            throws CacheException {
+            return stgy.update(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean afterUpdate(Object key, Object val, Object currVer, Object previousVer, SoftLock lock)
+            throws CacheException {
+            return stgy.afterUpdate(key, val, lock);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateGeneralDataRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateGeneralDataRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateGeneralDataRegion.java
new file mode 100644
index 0000000..c1ec886
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateGeneralDataRegion.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+import org.jetbrains.annotations.*;
+
+/**
+ * Implementation of {@link GeneralDataRegion}. This interface defines common contract for {@link QueryResultsRegion}
+ * and {@link TimestampsRegion}.
+ */
+public class GridHibernateGeneralDataRegion extends GridHibernateRegion implements GeneralDataRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache.
+     */
+    public GridHibernateGeneralDataRegion(GridHibernateRegionFactory factory, String name,
+        Ignite ignite, GridCache<Object, Object> cache) {
+        super(factory, name, ignite, cache);
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override public Object get(Object key) throws CacheException {
+        try {
+            return cache.get(key);
+        } catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void put(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+        } catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void evict(Object key) throws CacheException {
+        GridHibernateAccessStrategyAdapter.evict(ignite, cache, key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void evictAll() throws CacheException {
+        GridHibernateAccessStrategyAdapter.evictAll(cache);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNaturalIdRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNaturalIdRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNaturalIdRegion.java
new file mode 100644
index 0000000..2edcee8
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNaturalIdRegion.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+import org.hibernate.cache.spi.access.*;
+
+/**
+ * Implementation of {@link NaturalIdRegion}. This region is used to store naturalId data.
+ * <p>
+ * L2 cache for entity naturalId and target cache region can be set using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+ * &#064;org.hibernate.annotations.NaturalIdCache
+ * public class Entity {
+ *     &#064;org.hibernate.annotations.NaturalId
+ *     private String entityCode;
+ *
+ *     ...
+ * }
+ * </pre>
+ */
+public class GridHibernateNaturalIdRegion extends GridHibernateTransactionalDataRegion implements NaturalIdRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache,
+     * @param dataDesc Region data description.
+     */
+    public GridHibernateNaturalIdRegion(GridHibernateRegionFactory factory, String name,
+        Ignite ignite, GridCache<Object, Object> cache, CacheDataDescription dataDesc) {
+        super(factory, name, ignite, cache, dataDesc);
+    }
+
+    /** {@inheritDoc} */
+    @Override public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException {
+        return new AccessStrategy(createAccessStrategy(accessType));
+    }
+
+    /**
+     * NaturalId region access strategy.
+     */
+    private class AccessStrategy extends GridHibernateAbstractRegionAccessStrategy implements
+        NaturalIdRegionAccessStrategy {
+        /**
+         * @param stgy Access strategy implementation.
+         */
+        private AccessStrategy(GridHibernateAccessStrategyAdapter stgy) {
+            super(stgy);
+        }
+
+        /** {@inheritDoc} */
+        @Override public NaturalIdRegion getRegion() {
+            return GridHibernateNaturalIdRegion.this;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean insert(Object key, Object val) throws CacheException {
+            return stgy.insert(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean afterInsert(Object key, Object val) throws CacheException {
+            return stgy.afterInsert(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean update(Object key, Object val) throws CacheException {
+            return stgy.update(key, val);
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException {
+            return stgy.afterUpdate(key, val, lock);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNonStrictAccessStrategy.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNonStrictAccessStrategy.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNonStrictAccessStrategy.java
new file mode 100644
index 0000000..1ae2555
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateNonStrictAccessStrategy.java
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.internal.util.*;
+import org.apache.ignite.internal.util.typedef.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+import org.jetbrains.annotations.*;
+
+import java.util.*;
+
+/**
+ * Implementation of {@link AccessType#NONSTRICT_READ_WRITE} cache access strategy.
+ * <p>
+ * Configuration of L2 cache and per-entity cache access strategy can be set in the
+ * Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with nonstrict-read-write access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="nonstrict-read-write"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache access strategy can be set using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+ * public class Entity { ... }
+ * </pre>
+ */
+public class GridHibernateNonStrictAccessStrategy extends GridHibernateAccessStrategyAdapter {
+    /** */
+    private final ThreadLocal<WriteContext> writeCtx;
+
+    /**
+     * @param ignite Grid.
+     * @param cache Cache.
+     * @param writeCtx Thread local instance used to track updates done during one Hibernate transaction.
+     */
+    protected GridHibernateNonStrictAccessStrategy(Ignite ignite, GridCache<Object, Object> cache, ThreadLocal writeCtx) {
+        super(ignite, cache);
+
+        this.writeCtx = (ThreadLocal<WriteContext>)writeCtx;
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override protected SoftLock lock(Object key) throws CacheException {
+        WriteContext ctx = writeCtx.get();
+
+        if (ctx == null)
+            writeCtx.set(ctx = new WriteContext());
+
+        ctx.locked(key);
+
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void unlock(Object key, SoftLock lock) throws CacheException {
+        try {
+            WriteContext ctx = writeCtx.get();
+
+            if (ctx != null && ctx.unlocked(key)) {
+                writeCtx.remove();
+
+                ctx.updateCache(cache);
+            }
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean update(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException {
+        WriteContext ctx = writeCtx.get();
+
+        if (ctx != null) {
+            ctx.updated(key, val);
+
+            unlock(key, lock);
+
+            return true;
+        }
+
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean insert(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterInsert(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+
+            return true;
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void remove(Object key) throws CacheException {
+        WriteContext ctx = writeCtx.get();
+
+        if (ctx != null)
+            ctx.removed(key);
+    }
+
+    /**
+     * Information about updates done during single database transaction.
+     */
+    @SuppressWarnings("TypeMayBeWeakened")
+    private static class WriteContext {
+        /** */
+        private Map<Object, Object> updates;
+
+        /** */
+        private Set<Object> rmvs;
+
+        /** */
+        private Set<Object> locked = new GridLeanSet<>();
+
+        /**
+         * Marks key as locked.
+         *
+         * @param key Key.
+         */
+        void locked(Object key) {
+            locked.add(key);
+        }
+
+        /**
+         * Marks key as unlocked.
+         *
+         * @param key Key.
+         * @return {@code True} if last locked key was unlocked.
+         */
+        boolean unlocked(Object key) {
+            locked.remove(key);
+
+            return locked.isEmpty();
+        }
+
+        /**
+         * Marks key as updated.
+         *
+         * @param key Key.
+         * @param val Value.
+         */
+        void updated(Object key, Object val) {
+            if (updates == null)
+                updates = new GridLeanMap<>();
+
+            updates.put(key, val);
+        }
+
+        /**
+         * Marks key as removed.
+         *
+         * @param key Key.
+         */
+        void removed(Object key) {
+            if (rmvs == null)
+                rmvs = new GridLeanSet<>();
+
+            rmvs.add(key);
+        }
+
+        /**
+         * Updates cache.
+         *
+         * @param cache Cache.
+         * @throws IgniteCheckedException If failed.
+         */
+        void updateCache(GridCache<Object, Object> cache) throws IgniteCheckedException {
+            if (!F.isEmpty(rmvs))
+                cache.removeAll(rmvs);
+
+            if (!F.isEmpty(updates))
+                cache.putAll(updates);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateQueryResultsRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateQueryResultsRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateQueryResultsRegion.java
new file mode 100644
index 0000000..6a473fd
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateQueryResultsRegion.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.*;
+import org.hibernate.cache.spi.*;
+
+/**
+ * Implementation of {@link QueryResultsRegion}. This region is used to store query results.
+ * <p>
+ * Query results caching can be enabled in the Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Enable query cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with nonstrict-read-write access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="nonstrict-read-write"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * By default queries are not cached even after enabling query caching, to enable results caching for a particular
+ * query, call {@link Query#setCacheable(boolean)}:
+ * <pre name="code" class="java">
+ *     Session ses = getSession();
+ *
+ *     Query qry = ses.createQuery("...");
+ *
+ *     qry.setCacheable(true); // Enable L2 cache for query.
+ * </pre>
+ * Note: the query cache does not cache the state of the actual entities in the cache, it caches only identifier
+ * values. For this reason, the query cache should always be used in conjunction with
+ * the second-level cache for those entities expected to be cached as part of a query result cache
+ */
+public class GridHibernateQueryResultsRegion extends GridHibernateGeneralDataRegion implements QueryResultsRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache.
+     */
+    public GridHibernateQueryResultsRegion(GridHibernateRegionFactory factory, String name,
+        Ignite ignite, GridCache<Object, Object> cache) {
+        super(factory, name, ignite, cache);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadOnlyAccessStrategy.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadOnlyAccessStrategy.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadOnlyAccessStrategy.java
new file mode 100644
index 0000000..930bd7c
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadOnlyAccessStrategy.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+import org.jetbrains.annotations.*;
+
+/**
+ * Implementation of {@link AccessType#READ_ONLY} cache access strategy.
+ * <p>
+ * Configuration of L2 cache and per-entity cache access strategy can be set in the
+ * Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with read-only access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="read-only"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache access strategy can be set using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
+ * public class Entity { ... }
+ * </pre>
+
+ *
+ */
+public class GridHibernateReadOnlyAccessStrategy extends GridHibernateAccessStrategyAdapter {
+    /**
+     * @param ignite Grid.
+     * @param cache Cache.
+     */
+    public GridHibernateReadOnlyAccessStrategy(Ignite ignite, GridCache<Object, Object> cache) {
+        super(ignite, cache);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean insert(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterInsert(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+
+            return true;
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override protected SoftLock lock(Object key) throws CacheException {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void unlock(Object key, SoftLock lock) throws CacheException {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void remove(Object key) throws CacheException {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean update(Object key, Object val) throws CacheException {
+        throw new UnsupportedOperationException("Updates are not supported for read-only access strategy.");
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException {
+        throw new UnsupportedOperationException("Updates are not supported for read-only access strategy.");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadWriteAccessStrategy.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadWriteAccessStrategy.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadWriteAccessStrategy.java
new file mode 100644
index 0000000..848dac4
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateReadWriteAccessStrategy.java
@@ -0,0 +1,282 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.internal.util.*;
+import org.apache.ignite.transactions.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+
+import java.util.*;
+
+import static org.apache.ignite.transactions.IgniteTxConcurrency.*;
+import static org.apache.ignite.transactions.IgniteTxIsolation.*;
+
+/**
+ * Implementation of {@link AccessType#READ_WRITE} cache access strategy.
+ * <p>
+ * Configuration of L2 cache and per-entity cache access strategy can be set in the
+ * Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with read-write access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="read-write"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache access strategy can be set using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
+ * public class Entity { ... }
+ * </pre>
+ */
+public class GridHibernateReadWriteAccessStrategy extends GridHibernateAccessStrategyAdapter {
+    /** */
+    private final ThreadLocal<TxContext> txCtx;
+
+    /**
+     * @param ignite Grid.
+     * @param cache Cache.
+     * @param txCtx Thread local instance used to track updates done during one Hibernate transaction.
+     */
+    protected GridHibernateReadWriteAccessStrategy(Ignite ignite, GridCache<Object, Object> cache, ThreadLocal txCtx) {
+        super(ignite, cache);
+
+        this.txCtx = (ThreadLocal<TxContext>)txCtx;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Object get(Object key) throws CacheException {
+        try {
+            return cache.get(key);
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void putFromLoad(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected SoftLock lock(Object key) throws CacheException {
+        try {
+            TxContext ctx = txCtx.get();
+
+            if (ctx == null)
+                txCtx.set(ctx = new TxContext());
+
+            lockKey(key);
+
+            ctx.locked(key);
+
+            return null;
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void unlock(Object key, SoftLock lock) throws CacheException {
+        try {
+            TxContext ctx = txCtx.get();
+
+            if (ctx != null)
+                unlock(ctx, key);
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean update(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException {
+        try {
+            TxContext ctx = txCtx.get();
+
+            if (ctx != null) {
+                cache.putx(key, val);
+
+                unlock(ctx, key);
+
+                return true;
+            }
+
+            return false;
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean insert(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterInsert(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+
+            return true;
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void remove(Object key) throws CacheException {
+        try {
+            TxContext ctx = txCtx.get();
+
+            if (ctx != null)
+                cache.removex(key);
+        }
+        catch (IgniteCheckedException e) {
+            rollbackCurrentTx();
+
+            throw new CacheException(e);
+        }
+    }
+
+    /**
+     *
+     * @param ctx Transaction context.
+     * @param key Key.
+     * @throws IgniteCheckedException If failed.
+     */
+    private void unlock(TxContext ctx, Object key) throws IgniteCheckedException {
+        if (ctx.unlocked(key)) { // Finish transaction if last key is unlocked.
+            txCtx.remove();
+
+            IgniteTx tx = cache.tx();
+
+            assert tx != null;
+
+            try {
+                tx.commit();
+            }
+            finally {
+                tx.close();
+            }
+
+            assert cache.tx() == null;
+        }
+    }
+
+    /**
+     * Roll backs current transaction.
+     */
+    private void rollbackCurrentTx() {
+        try {
+            TxContext ctx = txCtx.get();
+
+            if (ctx != null) {
+                txCtx.remove();
+
+                IgniteTx tx = cache.tx();
+
+                if (tx != null)
+                    tx.rollback();
+            }
+        }
+        catch (IgniteCheckedException e) {
+            log.error("Failed to rollback cache transaction.", e);
+        }
+    }
+
+    /**
+     * @param key Key.
+     * @throws IgniteCheckedException If failed.
+     */
+    private void lockKey(Object key) throws IgniteCheckedException {
+        if (cache.tx() == null)
+            cache.txStart(PESSIMISTIC, REPEATABLE_READ);
+
+        cache.get(key); // Acquire distributed lock.
+    }
+
+    /**
+     * Information about updates done during single database transaction.
+     */
+    @SuppressWarnings("TypeMayBeWeakened")
+    private static class TxContext {
+        /** */
+        private Set<Object> locked = new GridLeanSet<>();
+
+        /**
+         * Marks key as locked.
+         *
+         * @param key Key.
+         */
+        void locked(Object key) {
+            locked.add(key);
+        }
+
+        /**
+         * Marks key as unlocked.
+         *
+         * @param key Key.
+         * @return {@code True} if last locked key was unlocked.
+         */
+        boolean unlocked(Object key) {
+            locked.remove(key);
+
+            return locked.isEmpty();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegion.java
new file mode 100644
index 0000000..b30799f
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegion.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+
+import java.util.*;
+
+/**
+ * Implementation of {@link Region}. This interface defines base contract for all L2 cache regions.
+ */
+public class GridHibernateRegion implements Region {
+    /** */
+    protected final GridHibernateRegionFactory factory;
+
+    /** */
+    private final String name;
+
+    /** Cache instance. */
+    protected final GridCache<Object, Object> cache;
+
+    /** Grid instance. */
+    protected Ignite ignite;
+
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache.
+     */
+    public GridHibernateRegion(GridHibernateRegionFactory factory, String name, Ignite ignite,
+        GridCache<Object, Object> cache) {
+        this.factory = factory;
+        this.name = name;
+        this.ignite = ignite;
+        this.cache = cache;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String getName() {
+        return name;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void destroy() throws CacheException {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean contains(Object key) {
+        return cache.containsKey(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getSizeInMemory() {
+        return -1;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getElementCountInMemory() {
+        return cache.size();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getElementCountOnDisk() {
+        return -1;
+    }
+
+    /** {@inheritDoc} */
+    @Override public Map toMap() {
+        return cache.toMap();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long nextTimestamp() {
+        return System.currentTimeMillis();
+    }
+
+    /** {@inheritDoc} */
+    @Override public int getTimeout() {
+        return 0;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegionFactory.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegionFactory.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegionFactory.java
new file mode 100644
index 0000000..751785d
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateRegionFactory.java
@@ -0,0 +1,230 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.apache.ignite.internal.util.typedef.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.*;
+import org.hibernate.cache.spi.RegionFactory;
+import org.hibernate.cache.spi.access.AccessType;
+import org.hibernate.cfg.*;
+
+import java.util.*;
+
+import static org.hibernate.cache.spi.access.AccessType.*;
+
+/**
+ * Hibernate L2 cache region factory.
+ * <p>
+ * Following Hibernate settings should be specified to enable second level cache and to use this
+ * region factory for caching:
+ * <pre name="code" class="brush: xml; gutter: false;">
+ * hibernate.cache.use_second_level_cache=true
+ * hibernate.cache.region.factory_class=org.apache.ignite.cache.hibernate.GridHibernateRegionFactory
+ * </pre>
+ * Note that before region factory is started you need to start properly configured GridGain node in the same JVM.
+ * For example to start GridGain node one of loader provided in {@code org.gridgain.grid.startup} package can be used.
+ * <p>
+ * Name of grid to be used for region factory must be specified as following Hibernate property:
+ * <pre name="code" class="brush: xml; gutter: false;">
+ * org.gridgain.hibernate.grid_name=&lt;grid name&gt;
+ * </pre>
+ * Each Hibernate cache region must be associated with some {@link org.apache.ignite.cache.GridCache}, by default it is assumed that
+ * for each cache region there is a {@link org.apache.ignite.cache.GridCache} with the same name. Also it is possible to define
+ * region to cache mapping using properties with prefix {@code org.gridgain.hibernate.region_cache}.
+ * For example if for region with name "region1" cache with name "cache1" should be used then following
+ * Hibernate property should be specified:
+ * <pre name="code" class="brush: xml; gutter: false;">
+ * org.gridgain.hibernate.region_cache.region1=cache1
+ * </pre>
+ */
+public class GridHibernateRegionFactory implements RegionFactory {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Hibernate L2 cache grid name property name. */
+    public static final String GRID_NAME_PROPERTY = "org.gridgain.hibernate.grid_name";
+
+    /** Default cache property name. */
+    public static final String DFLT_CACHE_NAME_PROPERTY = "org.gridgain.hibernate.default_cache";
+
+    /** Property prefix used to specify region name to cache name mapping. */
+    public static final String REGION_CACHE_PROPERTY = "org.gridgain.hibernate.region_cache.";
+
+    /** */
+    public static final String DFLT_ACCESS_TYPE_PROPERTY = "org.gridgain.hibernate.default_access_type";
+
+    /** */
+    public static final String GRID_CONFIG_PROPERTY = "org.gridgain.hibernate.grid_config";
+
+    /** Grid providing caches. */
+    private Ignite ignite;
+
+    /** Default cache. */
+    private GridCache<Object, Object> dfltCache;
+
+    /** Default region access type. */
+    private AccessType dfltAccessType;
+
+    /** Region name to cache name mapping. */
+    private final Map<String, String> regionCaches = new HashMap<>();
+
+    /** Map needed to provide the same transaction context for different regions. */
+    private final ThreadLocal threadLoc = new ThreadLocal();
+
+    /** {@inheritDoc} */
+    @Override public void start(Settings settings, Properties props) throws CacheException {
+        String gridName = props.getProperty(GRID_NAME_PROPERTY);
+
+        if (gridName != null)
+            ignite = G.ignite(gridName);
+        else {
+            String gridCfg = props.getProperty(GRID_CONFIG_PROPERTY);
+
+            if (gridCfg == null)
+                throw new CacheException("Either grid name or path to grid configuration must be specified.");
+
+            try {
+                ignite = G.start(gridCfg);
+            }
+            catch (IgniteCheckedException e) {
+                throw new CacheException(e);
+            }
+        }
+
+        if (ignite == null)
+            throw new CacheException("Grid '" + gridName + "' for hibernate L2 cache is not started.");
+
+        String accessType = props.getProperty(DFLT_ACCESS_TYPE_PROPERTY, NONSTRICT_READ_WRITE.name());
+
+        dfltAccessType = AccessType.valueOf(accessType);
+
+        for (Map.Entry<Object, Object> prop : props.entrySet()) {
+            String key = prop.getKey().toString();
+
+            if (key.startsWith(REGION_CACHE_PROPERTY)) {
+                String regionName = key.substring(REGION_CACHE_PROPERTY.length());
+
+                String cacheName = prop.getValue().toString();
+
+                if (ignite.cache(cacheName) == null)
+                    throw new CacheException("Cache '" + cacheName + "' specified for region '" + regionName + "' " +
+                        "is not configured.");
+
+                regionCaches.put(regionName, cacheName);
+            }
+        }
+
+        String dfltCacheName = props.getProperty(DFLT_CACHE_NAME_PROPERTY);
+
+        if (dfltCacheName != null) {
+            dfltCache = ignite.cache(dfltCacheName);
+
+            if (dfltCache == null)
+                throw new CacheException("Cache specified as default is not configured: " + dfltCacheName);
+        }
+
+        IgniteLogger log = ignite.log().getLogger(GridHibernateRegionFactory.class);
+
+        if (log.isDebugEnabled())
+            log.debug("GridHibernateRegionFactory started [grid=" + gridName + ']');
+    }
+
+    /** {@inheritDoc} */
+    @Override public void stop() {
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isMinimalPutsEnabledByDefault() {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override public AccessType getDefaultAccessType() {
+        return dfltAccessType;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long nextTimestamp() {
+        return System.currentTimeMillis();
+    }
+
+    /** {@inheritDoc} */
+    @Override public EntityRegion buildEntityRegion(String regionName, Properties props, CacheDataDescription metadata)
+        throws CacheException {
+        return new GridHibernateEntityRegion(this, regionName, ignite, regionCache(regionName), metadata);
+    }
+
+    /** {@inheritDoc} */
+    @Override public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties props,
+        CacheDataDescription metadata) throws CacheException {
+        return new GridHibernateNaturalIdRegion(this, regionName, ignite, regionCache(regionName), metadata);
+    }
+
+    /** {@inheritDoc} */
+    @Override public CollectionRegion buildCollectionRegion(String regionName, Properties props,
+        CacheDataDescription metadata) throws CacheException {
+        return new GridHibernateCollectionRegion(this, regionName, ignite, regionCache(regionName), metadata);
+    }
+
+    /** {@inheritDoc} */
+    @Override public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties props)
+        throws CacheException {
+        return new GridHibernateQueryResultsRegion(this, regionName, ignite, regionCache(regionName));
+    }
+
+    /** {@inheritDoc} */
+    @Override public TimestampsRegion buildTimestampsRegion(String regionName, Properties props) throws CacheException {
+        return new GridHibernateTimestampsRegion(this, regionName, ignite, regionCache(regionName));
+    }
+
+    /**
+     * Reuse same thread local for the same cache across different regions.
+     *
+     * @param cacheName Cache name.
+     * @return Thread local instance used to track updates done during one Hibernate transaction.
+     */
+    ThreadLocal threadLocalForCache(String cacheName) {
+        return threadLoc;
+    }
+
+    /**
+     * @param regionName L2 cache region name.
+     * @return Cache for given region.
+     * @throws CacheException If cache for given region is not configured.
+     */
+    private GridCache<Object, Object> regionCache(String regionName) throws CacheException {
+        String cacheName = regionCaches.get(regionName);
+
+        if (cacheName == null) {
+            if (dfltCache != null)
+                return dfltCache;
+
+            cacheName = regionName;
+        }
+
+        GridCache<Object, Object> cache = ignite.cache(cacheName);
+
+        if (cache == null)
+            throw new CacheException("Cache '" + cacheName + "' for region '" + regionName + "' is not configured.");
+
+        return cache;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTimestampsRegion.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTimestampsRegion.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTimestampsRegion.java
new file mode 100644
index 0000000..5f7a9cb
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTimestampsRegion.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.spi.*;
+
+/**
+ * Implementation of {@link TimestampsRegion}. This region is automatically created when query
+ * caching is enabled and it holds most recent updates timestamps to queryable tables.
+ * Name of timestamps region is {@code "org.hibernate.cache.spi.UpdateTimestampsCache"}.
+ */
+public class GridHibernateTimestampsRegion extends GridHibernateGeneralDataRegion implements TimestampsRegion {
+    /**
+     * @param factory Region factory.
+     * @param name Region name.
+     * @param ignite Grid.
+     * @param cache Region cache.
+     */
+    public GridHibernateTimestampsRegion(GridHibernateRegionFactory factory, String name,
+        Ignite ignite, GridCache<Object, Object> cache) {
+        super(factory, name, ignite, cache);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/ed7736f3/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTransactionalAccessStrategy.java
----------------------------------------------------------------------
diff --git a/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTransactionalAccessStrategy.java b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTransactionalAccessStrategy.java
new file mode 100644
index 0000000..a498db9
--- /dev/null
+++ b/modules/hibernate/src/main/java/org/apache/ignite/cache/hibernate/GridHibernateTransactionalAccessStrategy.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.cache.hibernate;
+
+import org.apache.ignite.*;
+import org.apache.ignite.cache.*;
+import org.hibernate.cache.*;
+import org.hibernate.cache.spi.access.*;
+import org.jetbrains.annotations.*;
+
+/**
+ * Implementation of {@link AccessType#TRANSACTIONAL} cache access strategy.
+ * <p>
+ * It is supposed that this strategy is used in JTA environment and Hibernate and
+ * {@link org.apache.ignite.cache.GridCache} corresponding to the L2 cache region are configured to use the same transaction manager.
+ * <p>
+ * Configuration of L2 cache and per-entity cache access strategy can be set in the
+ * Hibernate configuration file:
+ * <pre name="code" class="xml">
+ * &lt;hibernate-configuration&gt;
+ *     &lt;!-- Enable L2 cache. --&gt;
+ *     &lt;property name="cache.use_second_level_cache"&gt;true&lt;/property&gt;
+ *
+ *     &lt;!-- Use GridGain as L2 cache provider. --&gt;
+ *     &lt;property name="cache.region.factory_class"&gt;org.apache.ignite.cache.hibernate.GridHibernateRegionFactory&lt;/property&gt;
+ *
+ *     &lt;!-- Specify entity. --&gt;
+ *     &lt;mapping class="com.example.Entity"/&gt;
+ *
+ *     &lt;!-- Enable L2 cache with transactional access strategy for entity. --&gt;
+ *     &lt;class-cache class="com.example.Entity" usage="transactional"/&gt;
+ * &lt;/hibernate-configuration&gt;
+ * </pre>
+ * Also cache access strategy can be set using annotations:
+ * <pre name="code" class="java">
+ * &#064;javax.persistence.Entity
+ * &#064;javax.persistence.Cacheable
+ * &#064;org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
+ * public class Entity { ... }
+ * </pre>
+ */
+public class GridHibernateTransactionalAccessStrategy extends GridHibernateAccessStrategyAdapter {
+    /**
+     * @param ignite Grid.
+     * @param cache Cache.
+     */
+    public GridHibernateTransactionalAccessStrategy(Ignite ignite, GridCache<Object, Object> cache) {
+        super(ignite, cache);
+    }
+
+    /** {@inheritDoc} */
+    @Nullable @Override protected Object get(Object key) throws CacheException {
+        try {
+            return cache.get(key);
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void putFromLoad(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected SoftLock lock(Object key) throws CacheException {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void unlock(Object key, SoftLock lock) throws CacheException {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean update(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+
+            return true;
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterUpdate(Object key, Object val, SoftLock lock) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean insert(Object key, Object val) throws CacheException {
+        try {
+            cache.putx(key, val);
+
+            return true;
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean afterInsert(Object key, Object val) throws CacheException {
+        return false;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void remove(Object key) throws CacheException {
+        try {
+            cache.removex(key);
+        }
+        catch (IgniteCheckedException e) {
+            throw new CacheException(e);
+        }
+    }
+}


Mime
View raw message