@Override public EntityManagerFactory getEntityManagerFactory() { return em.getEntityManagerFactory(); }
@Override public EntityManagerFactory getEntityManagerFactory() { return em().getEntityManagerFactory(); }
/** * Returns whether the named query with the given name exists. * * @param em must not be {@literal null}. * @param queryName must not be {@literal null}. * @return */ private static boolean hasNamedQuery(EntityManager em, String queryName) { /* * See DATAJPA-617, we have to use a dedicated em for the lookups to avoid a * potential rollback of the running tx. */ EntityManager lookupEm = em.getEntityManagerFactory().createEntityManager(); try { lookupEm.createNamedQuery(queryName); return true; } catch (IllegalArgumentException e) { LOG.debug("Did not find named query {}", queryName); return false; } finally { lookupEm.close(); } }
protected EntityManager getEntityManager(Command<?> command) { EntityManager em = (EntityManager) txm.getResource(EnvironmentName.CMD_SCOPED_ENTITY_MANAGER); if (em != null && em.isOpen() && em.getEntityManagerFactory().equals(emf)) { return em; } return null; }
public static boolean isTransactionalEntityManager(EntityManager em) { EntityManager target = EntityManagerFactoryUtils.doGetTransactionalEntityManager( em.getEntityManagerFactory(), em.getProperties(), true); return target != null; }
public boolean isLoaded(Object entity) { return em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(entity); }
public boolean isLoaded(Object entity, String attribute) { return em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(entity, attribute); }
public Object getIdentifier(Object entity) { return em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(entity); }
validatingEm = getEntityManager().getEntityManagerFactory().createEntityManager(); validatingEm.createQuery(query);
private void addNamedQueriesTo(EntityManager entityManager) { EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory(); entityManagerFactory.addNamedQuery(LOAD_SAGA_NAMED_QUERY, entityManager.createQuery(LOAD_SAGA_QUERY)); entityManagerFactory.addNamedQuery( DELETE_ASSOCIATION_NAMED_QUERY, entityManager.createQuery(DELETE_ASSOCIATION_QUERY) ); entityManagerFactory.addNamedQuery( FIND_ASSOCIATION_IDS_NAMED_QUERY, entityManager.createQuery(FIND_ASSOCIATION_IDS_QUERY)); entityManagerFactory.addNamedQuery( DELETE_ASSOCIATIONS_NAMED_QUERY, entityManager.createQuery(DELETE_ASSOCIATIONS_QUERY) ); entityManagerFactory.addNamedQuery( FIND_ASSOCIATIONS_NAMED_QUERY, entityManager.createQuery(FIND_ASSOCIATIONS_QUERY) ); entityManagerFactory.addNamedQuery(DELETE_SAGA_NAMED_QUERY, entityManager.createQuery(DELETE_SAGA_QUERY)); entityManagerFactory.addNamedQuery(UPDATE_SAGA_NAMED_QUERY, entityManager.createQuery(UPDATE_SAGA_QUERY)); }
em.getTransaction().commit(); PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
if (em != null && em.isOpen() && em.getEntityManagerFactory().equals(emf)) { sharedEM = true; return em;
@Test public void test() { SimpleEntity[] entities = new SimpleEntity[2]; entities[0] = new SimpleEntity(); entities[0].name = "test"; TransactionUtil.doInJPA( this::sessionFactory, em -> { entities[1] = em.merge( entities[0] ); assertNotNull( em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( entities[1] ) ); } ); // Call as detached entity try ( SessionFactory sessionFactory = sessionFactory() ) { assertNotNull( sessionFactory.getPersistenceUnitUtil().getIdentifier( entities[1] ) ); } }
@Test public void testJPQLUpperAttributeValueBindParameterType() throws Exception { doInJPA( this::entityManagerFactory, entityManager -> { //tag::basic-attribute-converter-query-parameter-converter-object-example[] SessionFactory sessionFactory = entityManager.getEntityManagerFactory() .unwrap( SessionFactory.class ); MetamodelImplementor metamodelImplementor = (MetamodelImplementor) sessionFactory.getMetamodel(); Type captionType = metamodelImplementor .entityPersister( Photo.class.getName() ) .getPropertyType( "caption" ); Photo photo = (Photo) entityManager.createQuery( "select p " + "from Photo p " + "where upper(caption) = upper(:caption) ", Photo.class ) .unwrap( Query.class ) .setParameter( "caption", new Caption("Nicolae Grigorescu"), captionType) .getSingleResult(); //end::basic-attribute-converter-query-parameter-converter-object-example[] assertEquals( "Dorobantul", photo.getName() ); } ); }
/** * Gets the persistence unit. * * @return the persistence unit */ private static String getPersistenceUnit() { return (String) em.getEntityManagerFactory().getProperties().get(Constants.PERSISTENCE_UNIT_NAME); }
@Test public void testMaxSize() { doInJPA( this::entityManagerFactory, entityManager -> { EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory(); SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class ); assertEquals( expectedQueryStatisticsMaxSize(), sessionFactory.getSessionFactoryOptions().getQueryStatisticsMaxSize() ); StatisticsImplementor statistics = (StatisticsImplementor) sessionFactory.getStatistics(); for ( int i = 0; i < 10; i++ ) { statistics.queryExecuted( String.valueOf( i ), 100, i * 1000 ); } assertEquals( 1000, statistics.getQueryStatistics( "1" ).getExecutionTotalTime() ); for ( int i = 100; i < 300; i++ ) { statistics.queryExecuted( String.valueOf( i ), 100, i * 1000 ); } assertEquals( 0, statistics.getQueryStatistics( "1" ).getExecutionTotalTime() ); } ); } }
entityManager.getEntityManagerFactory().getCache().evict( Person.class );
entityManager.getEntityManagerFactory().getCache().evictAll();
/** * @param entityClassName * @param em * @return */ public static Class<?> getEntityClass(String entityClassName, EntityManager em) { MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel(); Class<?> entityClass = metamodel.getEntityClass(entityClassName); return entityClass; }
SessionImplementor sessionImplementor = entityManager.unwrap( SessionImplementor.class ); SessionFactory sessionFactory = entityManager.getEntityManagerFactory().unwrap( SessionFactory.class ); PersistenceUnitUtil persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();