本文整理汇总了Java中javax.persistence.PersistenceUnitUtil.getIdentifier方法的典型用法代码示例。如果您正苦于以下问题:Java PersistenceUnitUtil.getIdentifier方法的具体用法?Java PersistenceUnitUtil.getIdentifier怎么用?Java PersistenceUnitUtil.getIdentifier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.PersistenceUnitUtil
的用法示例。
在下文中一共展示了PersistenceUnitUtil.getIdentifier方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: primaryKeyAsObject
import javax.persistence.PersistenceUnitUtil; //导入方法依赖的package包/类
/**
* returns the objects ID as a Object value. Native data types like int and long are casted to Integer and Long.
*
* @param entity
* @return
* @throws AnnotationNotFoundException
*/
public static Object primaryKeyAsObject(Object entity) throws AnnotationNotFoundException {
if (entity == null) return null;
try {
Object id = getValueOfAnnotatedFieldOrMethod(entity, Id.class);
if (id == null) {
// OpenJPA returns proprietary object type
EntityManager em = Context.internalRequestScope().getApplicationEntityManager();
if (em.getEntityManagerFactory() == null || em.getEntityManagerFactory().getPersistenceUnitUtil() == null) {
throw new CibetException("CibetContext.getApplicationEntityManager().getEntityManagerFactory() is null");
}
PersistenceUnitUtil puu = em.getEntityManagerFactory().getPersistenceUnitUtil();
id = puu.getIdentifier(entity);
log.debug("found primary key from PersistenceUnitUtil: " + id);
} else {
log.debug("found primary key from annotation: " + id);
}
return id;
} catch (IllegalArgumentException e) {
throw new AnnotationNotFoundException(e.getMessage());
}
}
示例2: initialize
import javax.persistence.PersistenceUnitUtil; //导入方法依赖的package包/类
/**
* Initialize a entity.
* @param em entity manager to use
* @param entity entity to initialize
* @param depth max depth on recursion
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void initialize(EntityManager em, Object entity, int depth) {
// return on nulls, depth = 0 or already initialized objects
if (entity == null || depth == 0) {
return;
}
PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
EntityType entityType = em.getMetamodel().entity(entity.getClass());
Set<Attribute> attributes = entityType.getDeclaredAttributes();
Object id = unitUtil.getIdentifier(entity);
if (id != null) {
Object attached = em.find(entity.getClass(), unitUtil.getIdentifier(entity));
for (Attribute a : attributes) {
if (!unitUtil.isLoaded(entity, a.getName())) {
if (a.isCollection()) {
intializeCollection(em, entity, attached, a, depth);
}
else if(a.isAssociation()) {
intialize(em, entity, attached, a, depth);
}
}
}
}
}