当前位置: 首页>>代码示例>>Java>>正文


Java PersistenceUnitUtil.getIdentifier方法代码示例

本文整理汇总了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());
   }
}
 
开发者ID:Wolfgang-Winter,项目名称:cibet,代码行数:29,代码来源:AnnotationUtil.java

示例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);
				}
			}
		}
	}
}
 
开发者ID:chelu,项目名称:jdal,代码行数:35,代码来源:JpaUtils.java


注:本文中的javax.persistence.PersistenceUnitUtil.getIdentifier方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。