ServiceHub.withEntityManager - corda

corda / net.corda.core.node / ServiceHub / withEntityManager

# withEntityManager

`abstract fun <T> withEntityManager(block: EntityManager.() -> T): T`
`abstract fun withEntityManager(block: Consumer<EntityManager>): Unit`

Exposes the Java Persistence API (JPA) to flows via a restricted EntityManager. This method can be used to persist and query entities which inherit from MappedSchema. This is particularly useful if off-ledger data needs to be kept in conjunction with on-ledger state data.

NOTE: Suspendable flow operations such as send, receive, subFlow and sleep, cannot be called within the lambda.

### Parameters

`block` -

a lambda function with access to an EntityManager.

We want to make sure users have a restricted access to administrative functions. The following methods are blocked:

- close()
- unwrap(cls: Class?)
- getDelegate(): Any
- getMetamodel()
- joinTransaction()
- lock(entity: Any?, lockMode: LockModeType?)
- lock(entity: Any?, lockMode: LockModeType?, properties: MutableMap<String, Any>?)
- setProperty(propertyName: String?, value: Any?)

getTransaction returns a RestrictedEntityTransaction to prevent unsafe manipulation of a flow's underlying database transaction. The following methods are blocked:

- begin()
- commit()
- rollback()