> For the complete documentation index, see [llms.txt](https://manual.docs.microstream.one/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://manual.docs.microstream.one/cache/use-cases/hibernate-second-level-cache.md).

# Hibernate Second Level Cache

{% hint style="warning" %}
**This is the manual for older MicroStream versions (Version < 5.0).**

**The new documentation (Version >= 5.0) is located at:**

[https://docs.microstream.one/](https://docs.microstream.one/manual)
{% endhint %}

MicroStream offers a Hibernate cache region factory, which can be found in the `cache.hibernate` module.

{% code title="pom.xml" %}

```markup
<repositories>
    <repository>
        <id>microstream-releases</id>
        <url>https://repo.microstream.one/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>one.microstream</groupId>
        <artifactId>cache.hibernate</artifactId>
        <version>04.01.00-MS-GA</version>
    </dependency>
</dependencies>
```

{% endcode %}

The region factory's class name is `one.microstream.cache.hibernate.CacheRegionFactory`.\
It is configured via the property `hibernate.cache.region.factory_class`.

Depending on your environment it can be configured in different ways.

If you use a good old persistence.xml, set the property there:

{% code title="persistence.xml" %}

```markup
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
	xmlns="http://xmlns.jcp.org/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
	<persistence-unit name="...">
		<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
		...
		<properties>
			...
			<property name="hibernate.cache.region.factory_class"
				value="one.microstream.cache.hibernate.CacheRegionFactory" />
			...
		</properties>
	</persistence-unit>
</persistence>
```

{% endcode %}

Or for Spring applications:

{% tabs %}
{% tab title="application.properties" %}

```
spring.jpa.properties.hibernate.cache.microstream.missing_cache_strategy=create
spring.jpa.properties.hibernate.cache.region.factory_class=one.microstream.cache.hibernate.CacheRegionFactory
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.javax.persistence.sharedCache.mode=ALL
```

{% endtab %}

{% tab title="application.yml" %}

```yaml
spring:
    jpa:
        properties:
            hibernate:
                cache:
                    microstream:
                        missing_cache_strategy: create
                    region:
                        factory_class: one.microstream.cache.hibernate.CacheRegionFactory
                    use_query_cache: true
                    use_second_level_cache: true
            javax:
                persistence:
                    sharedCache:
                        mode: ALL
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://manual.docs.microstream.one/cache/use-cases/hibernate-second-level-cache.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
