-
public final class DBKt
-
-
Field Summary
Fields Modifier and Type Field Description private EntityManagerFactoryentityManagerFactoryprivate final Connectionconnectionprivate final EntityManagerextendedEntityManagerprivate final ObjectdbId
-
Method Summary
Modifier and Type Method Description EntityManagerFactorygetEntityManagerFactory()EntityManagerFactorysetEntityManagerFactory(EntityManagerFactory entityManagerFactory)final ConnectiongetConnection()final EntityManagergetExtendedEntityManager()The extended entity manager, which stays valid even after the transaction is committed and the servlet request finishes. final ObjectgetDbId()final DataSourcegetDataSource(VaadinOnKotlin $self)Shorthand for entityManagerFactory. final EntityManagerwithTransactionControlDisabled(EntityManager $self)Returns a new EntityManager which delegates all calls this, with the following exceptions:The EntityManager.getTransaction will fail with IllegalStateException.
final DataSourcetoDataSource(EntityManagerFactory $self)Provides a DataSource which creates JDBC connections using this factory. final <R extends Any> Rdb(Function1<PersistenceContext, R> block)Makes sure given block is executed in a DB transaction. final <T extends Any> List<T>findAll(EntityManager $self)Returns all JPA entities of given type. final <T extends Any> Tget(EntityManager $self, Object id)Retrieves given JPA entity. final <T extends Any> BooleandeleteById(EntityManager $self, Object id)Deletes given entity. final Booleandelete(EntityManager $self, Object entity)Deletes given entity, regardless of whether it is detached or attached. final <T extends Any> IntegerdeleteAll(EntityManager $self)Deletes all instances of given JPA entity. final <T extends Any> TsingleOrNull(TypedQuery<T> $self)TypedQuery.getSingleResult works only for simple types such as Long - it does not work when retrieving a single JPA entity and will fail with NoResultException: No entity found for querywhen no entities are found.-
-
Method Detail
-
getEntityManagerFactory
EntityManagerFactory getEntityManagerFactory()
-
setEntityManagerFactory
EntityManagerFactory setEntityManagerFactory(EntityManagerFactory entityManagerFactory)
-
getConnection
final Connection getConnection()
-
getExtendedEntityManager
final EntityManager getExtendedEntityManager()
The extended entity manager, which stays valid even after the transaction is committed and the servlet request finishes. Automatically allocates and releases a delegate EntityManager.
Can only be used from Vaadin/web servlet thread - cannot be used from async threads.
-
getDataSource
final DataSource getDataSource(VaadinOnKotlin $self)
Shorthand for entityManagerFactory.toDataSource()
-
withTransactionControlDisabled
final EntityManager withTransactionControlDisabled(EntityManager $self)
Returns a new EntityManager which delegates all calls
this, with the following exceptions:The EntityManager.getTransaction will fail with IllegalStateException.
-
toDataSource
final DataSource toDataSource(EntityManagerFactory $self)
Provides a DataSource which creates JDBC connections using this factory.
-
db
final <R extends Any> R db(Function1<PersistenceContext, R> block)
Makes sure given block is executed in a DB transaction. When the block finishes normally, the transaction commits; if the block throws any exception, the transaction is rolled back.
Example of use:
db { em.persist() }- Parameters:
block- the block to run in the transaction.
-
findAll
final <T extends Any> List<T> findAll(EntityManager $self)
Returns all JPA entities of given type.
-
get
final <T extends Any> T get(EntityManager $self, Object id)
Retrieves given JPA entity. Fails if there is no such entity.
- Parameters:
id- the entity id
-
deleteById
final <T extends Any> Boolean deleteById(EntityManager $self, Object id)
Deletes given entity. Triggers the CascadeType.REMOVE properly.
- Parameters:
id- entity id
-
delete
final Boolean delete(EntityManager $self, Object entity)
Deletes given entity, regardless of whether it is detached or attached. If the entity was detached and meanwhile deleted from the database, does nothing and return false.
Triggers the CascadeType.REMOVE properly.
-
deleteAll
final <T extends Any> Integer deleteAll(EntityManager $self)
Deletes all instances of given JPA entity.
-
singleOrNull
final <T extends Any> T singleOrNull(TypedQuery<T> $self)
TypedQuery.getSingleResult works only for simple types such as Long - it does not work when retrieving a single JPA entity and will fail with
NoResultException: No entity found for querywhen no entities are found. Often, null is a better alternative.
-
-
-