= JPA Cache Store Usage Example
This section provides an example for using JPA cache stores.

.Prerequistes

* Configure {brandname} to marshall your JPA entities. By default, {brandname}
uses ProtoStream for marshalling Java objects. To marshall JPA entities, you
must create a `SerializationContextInitializer` implementation that registers a
`.proto` schema and marshaller with a `SerializationContext`.

.Procedure

. Define a persistence unit "myPersistenceUnit" in `persistence.xml`.
+
[source,xml,options="nowrap",subs=attributes+]
----
include::config_examples/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[]
----

.References

* link:{dev_docs}#protostream_cm_config[Using the ProtoStream Marshaller]
* link:{dev_docs}#protostream[Marshalling Custom Java Objects with ProtoStream]
