[id='jpa-cache-store-example_{context}']
= JPA cache store example

This section provides an example for using JPA cache stores.

.Prerequistes

* Configure {brandname} to marshall your JPA entities.

.Procedure

. Define a persistence unit "myPersistenceUnit" in `persistence.xml`.
+
[source,xml,options="nowrap",subs=attributes+]
----
include::xml/persistence_unit_example.xml[]
----
+
. Create a user entity class.
+
[source,java]
----
include::code_examples/PersistenceUserEntityClass.java[]
----
+
. Configure a cache named "usersCache" with a JPA cache store.
+
Then you can configure a cache "usersCache" to use JPA Cache Store, so that when you put data into the cache, the data would be persisted into the database based on JPA configuration.
+
[source,java]
----
include::code_examples/PersistenceJpaCacheStore.java[]
----

* Caches that use a JPA cache store can store one type of data only, as in the
following example:
+
[source,java]
----
include::code_examples/CacheJpaStore.java[]
----

* The `@EmbeddedId` annotation allows you to use composite keys, as in the
following example:
+
[source,java]
----
include::code_examples/EmbeddedIdExample.java[]
----

[role="_additional-resources"]
.Additional resources
* link:{encoding_docs}[Cache Encoding and Marshalling]
