Java 类javax.persistence.PersistenceUnitUtil 实例源码

项目:cibet    文件:AnnotationUtil.java   
/**
 * 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());
   }
}
项目:bjug-querydsl    文件:JpaQueryTest.java   
@Test
@Transactional
public void testFindProductWithJoinFetch() throws Exception {
    JPAQuery query = new JPAQuery(em);
    QProduct path = QProduct.product;
    Product results = query.from(path)
            .join(path.stockKeepingUnits)
            .fetch() // Eager fetch the last join
            .where(path.productId.eq(1))
            .uniqueResult(path);

    // Get the PersistenceUnitUtil
    PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();

    // Check that the eager fetching occurred!
    Assert.assertTrue(util.isLoaded(results, "stockKeepingUnits"));

}
项目:bjug-querydsl    文件:JpaQueryTest.java   
@Test
@Transactional
public void testFindSkuWithFetchAll() throws Exception {
    JPAQuery query = new JPAQuery(em);
    QStockKeepingUnit path = QStockKeepingUnit.stockKeepingUnit;

    StockKeepingUnit results = query.from(path)
            .fetchAll() // Get all non-association fields eagerly
            .singleResult(path);

    // Get the PersistenceUnitUtil
    PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();

    // Check that the eager fetching occurred!
    Assert.assertTrue(util.isLoaded(results));

}
项目:bjug-querydsl    文件:JpaQueryTest.java   
@Test
@Transactional
public void testFindSkuWithJoinFetch() throws Exception {
    JPAQuery query = new JPAQuery(em);
    QStockKeepingUnit path = QStockKeepingUnit.stockKeepingUnit;

    StockKeepingUnit results = query.from(path)
            .join(path.product())
            .fetch() // Get the product eagerly
            .singleResult(path);

    // Get the PersistenceUnitUtil
    PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();

    // Check that the eager fetching occurred!
    Assert.assertTrue(util.isLoaded(results, "product"));

}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(4)
public void shouldLoadArtistWithLiveConcertsAndNoContracts()
    throws Exception {
    Artist artist = getArtistWithEntityGraph(
        "Artist.WithConcertsAndNoContracts");
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    System.out.printf("++++ artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
    ConcertEvent event = artist.getEvents().values().iterator().next();
    System.out.printf("++++ concert event=%s\n", event );
    assertTrue(util.isLoaded(event, "id"));
    assertTrue(util.isLoaded(event, "name"));
    assertTrue(util.isLoaded(event, "eventType"));
    assertFalse(util.isLoaded(event, "contracts"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(5)
public void shouldLoadArtistWithLiveConcertsAndContracts()
    throws Exception{
    EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcertsAndContracts");
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    Artist artist = getArtistWithEntityGraph(
        "Artist.WithConcertsAndContracts");
    System.out.printf("++++ artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
    ConcertEvent event = artist.getEvents().values().iterator().next();
    System.out.printf("++++ concert event=%s\n", event );
    assertTrue(util.isLoaded(event, "id"));
    assertTrue(util.isLoaded(event, "name"));
    assertTrue(util.isLoaded(event, "eventType"));
    assertTrue(util.isLoaded(event, "contracts"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(2)
public void shouldLoadArtistWithoutConcerts() throws Exception{
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    EntityGraph artistGraph = em.getEntityGraph("Artist.NoConcerts");
    Artist artist = (Artist) em.createQuery("Select a from Artist a")
        .setHint("javax.persistence.fetchgraph", artistGraph)
        .getResultList()
        .get(0);
    System.out.printf("++++ artist=%s\n", artist);
    System.out.printf(">> loaded  artist.id = %s\n", util.isLoaded(artist, "id"));
    System.out.printf(">> loaded  artist.name = %s\n", util.isLoaded(artist, "name"));
    System.out.printf(">> loaded  artist.events = %s\n", util.isLoaded(artist, "events"));
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertFalse(util.isLoaded(artist, "events"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(3)
public void shouldLoadArtistWithConcerts() throws Exception{
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcerts");
    Artist artist = (Artist)
        em.createQuery("Select a from Artist a")
        .setHint("javax.persistence.fetchgraph", artistGraph)
        .getResultList()
        .get(0);
    System.out.printf("++++ artist=%s\n", artist);
    System.out.printf("artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(4)
public void shouldLoadArtistWithLiveConcertsAndNoContracts()
    throws Exception {
    PersistenceUnitUtil util =
            em.getEntityManagerFactory().getPersistenceUnitUtil();
    em.clear();
    EntityGraph artistGraph = em.getEntityGraph(
        "Artist.WithConcertsAndNoContracts");
    Artist artist = (Artist)
        em.createQuery("Select a from Artist a")
        .setHint("javax.persistence.fetchgraph", artistGraph)
        .getResultList()
        .get(0);
    System.out.printf("++++ artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
    ConcertEvent event = artist.getEvents().values().iterator().next();
    System.out.printf("++++ concert event=%s\n", event );
    assertTrue(util.isLoaded(event, "id"));
    assertTrue(util.isLoaded(event, "name"));
    assertTrue(util.isLoaded(event, "eventType"));
    assertFalse(util.isLoaded(event, "contracts"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(5)
public void shouldLoadArtistWithLiveConcertsAndContracts()
    throws Exception{
    PersistenceUnitUtil util =
            em.getEntityManagerFactory().getPersistenceUnitUtil();
    em.clear();
    EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcertsAndContracts");
    Artist artist = (Artist)
        em.createQuery("Select a from Artist a")
        .setHint("javax.persistence.fetchgraph", artistGraph)
        .getResultList()
        .get(0);
    System.out.printf("++++ artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
    ConcertEvent event = artist.getEvents().values().iterator().next();
    System.out.printf("++++ concert event=%s\n", event );
    assertTrue(util.isLoaded(event, "id"));
    assertTrue(util.isLoaded(event, "name"));
    assertTrue(util.isLoaded(event, "eventType"));
    assertTrue(util.isLoaded(event, "contracts"));
}
项目:cibet    文件:CibetUtil.java   
/**
 * checks if the entity and all its properties are completely loaded from the database and all lazy properties are
 * initialised.
 * 
 * @param em
 *           EntityManager for loading the entity
 * @param entity
 * @return
 */
public static boolean isLoaded(EntityManager em, Object entity) {
   if (entity == null) {
      return true;
   }

   log.debug("check load state!");
   if (!em.contains(entity)) {
      log.debug("Entity is not in persistence context. Cannot check load state");
      // return true;
   }

   PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
   if (util == null) {
      log.debug("no PersistenceUnitUtil");
      return true;
   }

   boolean loadstate = true;
   Class<?> clazz = entity.getClass();
   while (clazz != null) {
      Field[] fields = clazz.getDeclaredFields();
      for (Field f : fields) {
         if (Modifier.isStatic(f.getModifiers())) continue;
         if (Modifier.isTransient(f.getModifiers())) continue;
         if (AnnotationUtil.isFieldOrGetterOrSetterAnnotationPresent(clazz, f, Transient.class)) continue;
         boolean loaded = util.isLoaded(entity, f.getName());
         log.debug(f.getName() + " [" + f.getType() + "] loaded: " + loaded);
         if (!loaded) loadstate = false;
      }
      clazz = clazz.getSuperclass();
   }

   return loadstate;
}
项目:dachs    文件:EclipseLinkEntityEventListener.java   
public EclipseLinkEntityEventListener(PersistenceUnitUtil persistenceUnitUtil, InternalEntityListener internalEntityListener)
{
    this.persistenceUnitUtil = persistenceUnitUtil;
    this.listener = internalEntityListener;
    this.entityUtil = new EntityUtil();

    this.entityFilter = internalEntityListener.entityFilter();
    this.fieldFilter = internalEntityListener.fieldFilter();
}
项目:Hibernate-Search-GenericJPA    文件:ObjectHandlerTask.java   
public ObjectHandlerTask(
        BatchBackend batchBackend, Class<?> entityClass, EntityIndexBinding entityIndexBinding,
        Supplier<EntityProvider> emProvider, BiConsumer<ObjectHandlerTask, EntityProvider> entityManagerDisposer,
        PersistenceUnitUtil peristenceUnitUtil) {
    this(
            batchBackend,
            entityClass,
            entityIndexBinding,
            emProvider,
            entityManagerDisposer,
            peristenceUnitUtil,
            null,
            null
    );
}
项目:Hibernate-Search-GenericJPA    文件:ObjectHandlerTask.java   
public ObjectHandlerTask(
        BatchBackend batchBackend, Class<?> entityClass, EntityIndexBinding entityIndexBinding,
        Supplier<EntityProvider> emProvider, BiConsumer<ObjectHandlerTask, EntityProvider> entityManagerDisposer,
        PersistenceUnitUtil peristenceUnitUtil, NumberCondition condition, Consumer<Exception> exceptionConsumer) {
    this.batchBackend = batchBackend;
    this.entityClass = entityClass;
    this.entityIndexBinding = entityIndexBinding;
    this.emProvider = emProvider;
    this.entityManagerDisposer = entityManagerDisposer;
    this.peristenceUnitUtil = peristenceUnitUtil;
    this.condition = condition;
    this.exceptionConsumer = exceptionConsumer;
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(2)
public void shouldLoadArtistWithoutConcerts() throws Exception{
    Artist artist = getArtistWithEntityGraph("Artist.NoConcerts");
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    System.out.printf("++++ artist=%s\n", artist);
    System.out.printf(">> loaded  artist.id = %s\n", util.isLoaded(artist, "id"));
    System.out.printf(">> loaded  artist.name = %s\n", util.isLoaded(artist, "name"));
    System.out.printf(">> loaded  artist.events = %s\n", util.isLoaded(artist, "events"));
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertFalse(util.isLoaded(artist, "events"));
}
项目:javaee7-developer-handbook    文件:ArtistEntityGraphRetrievalTest.java   
@Test
@InSequence(3)
public void shouldLoadArtistWithConcerts() throws Exception{
    PersistenceUnitUtil util =
        em.getEntityManagerFactory().getPersistenceUnitUtil();
    Artist artist = getArtistWithEntityGraph("Artist.WithConcerts");
    System.out.printf("++++ artist=%s\n", artist);
    System.out.printf("artist=%s\n", artist);
    assertTrue(util.isLoaded(artist, "id"));
    assertTrue(util.isLoaded(artist, "name"));
    assertTrue(util.isLoaded(artist, "events"));
}
项目:jdal    文件:JpaUtils.java   
/**
 * 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);
                }
            }
        }
    }
}
项目:deltaspike    文件:HouseRepositoryTest.java   
@Test
@InSequence(2)
public void shouldNotLoadLazyAssociationsWithoutGraph() throws Exception
{
    House house = repository.findOptionalByName("Bellevue");
    assertNotNull(house);

    PersistenceUnitUtil puu = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();

    assertFalse(puu.isLoaded(house, "flats"));
    assertFalse(puu.isLoaded(house, "garages"));
}
项目:deltaspike    文件:PersistenceUnitUtilDelegateFactory.java   
public static PersistenceUnitUtil get(EntityManager entityManager)
{
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final String vendorName = (String) entityManagerFactory.getProperties().get("VendorName");
    if (vendorName != null && "openjpa".equalsIgnoreCase(vendorName))
    {
        return new OpenJpaPersistenceUnitUtilDelegate(entityManager);
    }
    return entityManagerFactory.getPersistenceUnitUtil();
}
项目:helium    文件:EntityManagerFactoryImpl.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    if ( ! isOpen() ) {
        throw new IllegalStateException("EntityManagerFactory is closed");
    }
    return util;
}
项目:cibet    文件:CibetEntityManagerFactory.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
   return nativeEntityManagerFactory.getPersistenceUnitUtil();
}
项目:cibet    文件:JdbcBridgeEntityManagerFactory.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
   return null;
}
项目:jpasecurity    文件:DelegatingEntityManagerFactory.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return delegate.getPersistenceUnitUtil();
}
项目:jpasecurity    文件:SecurePersistenceUnitUtil.java   
public SecurePersistenceUnitUtil(PersistenceUnitUtil util) {
    persistenceUnitUtil = notNull(PersistenceUnitUtil.class, util);
    providerUtil = createProviderUtil();
}
项目:jpasecurity    文件:MockitoPersistenceProvider.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return null;
}
项目:dachs    文件:IdentityUtil.java   
public IdentityUtil(PersistenceUnitUtil persistenceUnitUtil)
{
    this.persistenceUnitUtil = persistenceUnitUtil;
}
项目:OpenCyclos    文件:FetchDAOImpl.java   
public boolean isInitialized(final Object value) {
    PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
    return persistenceUnitUtil.isLoaded(value);
}
项目:training    文件:MockStockPriceEntityManagerFactory.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    throw new UnsupportedOperationException("Not supported.");
}
项目:switchyard    文件:NoopEntityManagerFactory.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return null;
}
项目:ef-orm    文件:JefEntityManagerFactory.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    throw new UnsupportedOperationException();
}
项目:olingo-odata2    文件:JPQLBuilderFactoryTest.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
  return null;
}
项目:functional-jpa    文件:RichEntityManagerFactory.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return emf.getPersistenceUnitUtil();
}
项目:spearal-jpa2    文件:EntityManagerFactoryWrapper.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return entityManagerFactory.getPersistenceUnitUtil();
}
项目:hexa.tools    文件:EntityManagerFactoryImpl.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil()
{
    assert false;
    return null;
}
项目:breeze.server.java    文件:JPATypeAdapter.java   
JPATypeAdapter(PersistenceUnitUtil puu, List<BoundField> boundFields) {
    this.puu = puu;
    this.boundFields = boundFields;
}
项目:breeze.server.java    文件:JPATypeAdapter.java   
public Factory(PersistenceUnitUtil puu) {
    this.puu = puu;
}
项目:fuse-bxms-integ    文件:NoopEntityManagerFactory.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return null;
}
项目:raidenjpa    文件:RaidenEntityManagerFactory.java   
public PersistenceUnitUtil getPersistenceUnitUtil() {
    throw new NotYetImplementedException();
}
项目:bundles    文件:DelegatedEntityManagerFactory.java   
@Override
public PersistenceUnitUtil getPersistenceUnitUtil() {
    return emf.getPersistenceUnitUtil();
}