Package io.axoniq.dataprotection.cryptoengine.jpa

## Class JpaCryptoEngine

- java.lang.Object
- - io.axoniq.dataprotection.cryptoengine.DatabaseBackedCryptoEngine
  - - io.axoniq.dataprotection.cryptoengine.jpa.JpaCryptoEngine

- All Implemented Interfaces:
    `CryptoEngine`

      public class JpaCryptoEngine
      extends DatabaseBackedCryptoEngine

  Implementation of `CryptoEngine` that extends from `DatabaseBackedCryptoEngine` and stores its data in a relational database using JPA. It needs a `EntityManagerFactory` to operate. This class will manage its own transactions. Therefore, it doesn't need any type of transaction manager, and `EntityManagerFactory` must have the `RESOURCE_LOCAL` transaction type.

  This class by itself doesn't do any key caching. If this is required, applications may configure a second-level cache on the `EntityManagerFactory`.

- - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `JpaCryptoEngine​(jakarta.persistence.EntityManagerFactory emf)` | Creates a new `JpaCryptoEngine`.
    `JpaCryptoEngine​(jakarta.persistence.EntityManagerFactory emf, Class<? extends KeyEntity> entityClass)` | Creates a new `JpaCryptoEngine` for a custom entity class.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `deleteKey​(String id)` | Deletes the key for a given id.
    `protected Class<? extends KeyEntity>` | `entityClass()` | Provides access to the entity class specified in the constructor; made `protected` to make this information available to subclasses
    `SecretKey` | `getKey​(String id)` | Retrieves the key for a given id.
    `protected SecretKey` | `putKeyIfAbsent​(String id, SecretKeySpec secretKeySpec)` | Stores the key for the given id, if no key is currently registered for this id.

    - ### Methods inherited from class io.axoniq.dataprotection.cryptoengine.DatabaseBackedCryptoEngine

      `createCipher, createDigestCipher, getKeyType, getOrCreateKey, setKeyType`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JpaCryptoEngine

          public JpaCryptoEngine​(jakarta.persistence.EntityManagerFactory emf)

      Creates a new `JpaCryptoEngine`. Will use `DefaultKeyEntity` as the class to store key entities.
      Parameters:
        `emf` - the `EntityManagerFactory` to use.

    - #### JpaCryptoEngine

          public JpaCryptoEngine​(jakarta.persistence.EntityManagerFactory emf,
                                 Class<? extends KeyEntity> entityClass)

      Creates a new `JpaCryptoEngine` for a custom entity class.
      Parameters:
        `emf` - the `EntityManagerFactory` to use.
        `entityClass` - the entity used to store key information

  - ### Method Detail

    - #### putKeyIfAbsent

          protected SecretKey putKeyIfAbsent​(String id,
                                             SecretKeySpec secretKeySpec)

      Description copied from class: `DatabaseBackedCryptoEngine`
      Stores the key for the given id, if no key is currently registered for this id. Returns the new key belonging to id, which is either the key that was already registered or the key provided as the 2nd argument if no key was registered yet. (Please note that this is different behaviour from `ConcurrentHashMap.putIfAbsent(Object, Object)`, which always returns the prior value belonging to key.)
      Specified by:
        `putKeyIfAbsent` in class `DatabaseBackedCryptoEngine`
      Parameters:
        `id` - the id for which to store/retrieve the secret key
        `secretKeySpec` - the `SecretKeySpec` to store if none has been stored yet for id
      Returns:
        the effective `SecretKeySpec` for id

    - #### getKey

          public SecretKey getKey​(String id)

      Description copied from interface: `CryptoEngine`
      Retrieves the key for a given id.
      Parameters:
        `id` - the id of the `SecretKey`
      Returns:
        the existing `SecretKey` associated with the id, or `null` if no such key exists

    - #### deleteKey

          public void deleteKey​(String id)

      Description copied from interface: `CryptoEngine`
      Deletes the key for a given id. Does nothing if the key doesn't exist.
      Parameters:
        `id` - the id of the `SecretKey`

    - #### entityClass

          protected Class<? extends KeyEntity> entityClass()

      Provides access to the entity class specified in the constructor; made `protected` to make this information available to subclasses
      Returns:
        the entity class