/** * 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()); } }
@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")); }
@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)); }
@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")); }
@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")); }
@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")); }
@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")); }
@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")); }
@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")); }
@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")); }
/** * 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; }
public EclipseLinkEntityEventListener(PersistenceUnitUtil persistenceUnitUtil, InternalEntityListener internalEntityListener) { this.persistenceUnitUtil = persistenceUnitUtil; this.listener = internalEntityListener; this.entityUtil = new EntityUtil(); this.entityFilter = internalEntityListener.entityFilter(); this.fieldFilter = internalEntityListener.fieldFilter(); }
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 ); }
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; }
@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")); }
@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")); }
/** * 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); } } } } }
@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")); }
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(); }
public PersistenceUnitUtil getPersistenceUnitUtil() { if ( ! isOpen() ) { throw new IllegalStateException("EntityManagerFactory is closed"); } return util; }
@Override public PersistenceUnitUtil getPersistenceUnitUtil() { return nativeEntityManagerFactory.getPersistenceUnitUtil(); }
@Override public PersistenceUnitUtil getPersistenceUnitUtil() { return null; }
public PersistenceUnitUtil getPersistenceUnitUtil() { return delegate.getPersistenceUnitUtil(); }
public SecurePersistenceUnitUtil(PersistenceUnitUtil util) { persistenceUnitUtil = notNull(PersistenceUnitUtil.class, util); providerUtil = createProviderUtil(); }
public PersistenceUnitUtil getPersistenceUnitUtil() { return null; }
public IdentityUtil(PersistenceUnitUtil persistenceUnitUtil) { this.persistenceUnitUtil = persistenceUnitUtil; }
public boolean isInitialized(final Object value) { PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil(); return persistenceUnitUtil.isLoaded(value); }
public PersistenceUnitUtil getPersistenceUnitUtil() { throw new UnsupportedOperationException("Not supported."); }
public PersistenceUnitUtil getPersistenceUnitUtil() { throw new UnsupportedOperationException(); }
public PersistenceUnitUtil getPersistenceUnitUtil() { return emf.getPersistenceUnitUtil(); }
public PersistenceUnitUtil getPersistenceUnitUtil() { return entityManagerFactory.getPersistenceUnitUtil(); }
@Override public PersistenceUnitUtil getPersistenceUnitUtil() { assert false; return null; }
JPATypeAdapter(PersistenceUnitUtil puu, List<BoundField> boundFields) { this.puu = puu; this.boundFields = boundFields; }
public Factory(PersistenceUnitUtil puu) { this.puu = puu; }
public PersistenceUnitUtil getPersistenceUnitUtil() { throw new NotYetImplementedException(); }
@Override public PersistenceUnitUtil getPersistenceUnitUtil() { return emf.getPersistenceUnitUtil(); }