com.gtcgroup.justify.jpa.helper.JstEntityManagerUtilHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of justify-jpa Show documentation
Show all versions of justify-jpa Show documentation
JPA MODULE: PED Central is the home for the open-source �Justify� suite of software engineering modules. Justify seeks API alignment between Java 1.8+ application code and JUnit 5 test code.
/*
* [Licensed per the Open Source "MIT License".]
*
* Copyright (c) 2006 - 2017 by
* Global Technology Consulting Group, Inc. at
* http://gtcGroup.com
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gtcgroup.justify.jpa.helper;
import javax.persistence.EntityManager;
import com.gtcgroup.justify.core.exception.internal.JustifyRuntimeException;
/**
* This Helper class provides persistence {@link EntityManager} support.
*
*
* Copyright (c) 2006 - 2017 by Global Technology Consulting Group, Inc. at
* gtcGroup.com .
*
*
* @author Marvin Toll
* @since v3.0
*/
public enum JstEntityManagerUtilHelper {
@SuppressWarnings("javadoc")
INSTANCE;
/**
* This method clears the persistence context (final L1 cache), causing all
* managed entities to become detached. Changes made to entities that have
* not been flushed to the database will not be persisted.
*/
public static void clearAllInstancesFromPersistenceContext(final EntityManager entityManager) {
entityManager.clear();
}
/**
* This method clears the shared (L2) cache of all instances.
*/
public static void evictAllEntitiesFromSharedCache(final EntityManager entityManager) {
entityManager.getEntityManagerFactory().getCache().evictAll();
}
/**
* This method clears the shared (L2) cache of a single instance.
*/
public static void evictEntityInstanceFromSharedCache(final EntityManager entityManager,
final Object populatedEntity) {
try {
entityManager.getEntityManagerFactory().getCache().evict(populatedEntity.getClass(),
retrieveIdentity(entityManager, populatedEntity));
} catch (final Exception e) {
throw new JustifyRuntimeException(e);
}
}
/**
* This method clears the shared (L2) cache of all instances of a single
* object type.
*/
public static void evictEntityInstancesFromSharedCache(final EntityManager entityManager,
final Class entityClass) {
entityManager.getEntityManagerFactory().getCache().evict(entityClass);
}
/**
* This method clears the shared (L2) cache of all instances of a single
* object type.
*/
public static void evictEntityInstancesFromSharedCache(final EntityManager entityManager,
final Class entityClass, final Object... entityIdentities) {
Object result;
for (final Object entityIdentity : entityIdentities) {
result = entityManager.find(entityClass, entityIdentity,
JstFindUtilHelper.FORCE_DATABASE_TRIP);
if (null != result) {
evictEntityInstanceFromSharedCache(entityManager, result);
}
}
}
/**
* @return Object
*/
public static Object retrieveIdentity(final EntityManager entityManager, final Object populatedEntity) {
return entityManager.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(populatedEntity);
}
}