try { Class<?> type = Class.forName(entityType.getType()); SessionFactory sessionFactory = ((CriteriaBuilderImpl) em.getCriteriaBuilder()).getEntityManagerFactory().getSessionFactory(); Class<?>[] entities = helper.getAllPolymorphicEntitiesFromCeiling(type, sessionFactory, true, true);
String original = ((SingularAttributePath) path).getAttribute().getDeclaringType().getJavaType().getName() + "." + ((SingularAttributePath) path).getAttribute().getName() + "." + piece; String copy = path.getJavaType().getName() + "." + piece; copyCollectionPersister(original, copy, ((CriteriaBuilderImpl) builder).getEntityManagerFactory().getSessionFactory()); Metamodel mm = em.getMetamodel(); boolean found = false; path.getJavaType(), em.getSessionFactory(), true, true);
public EntityManager createEntityManager() { return createEntityManager( null ); }
public EntityManagerFactory buildEntityManagerFactory() { Thread thread = null; ClassLoader contextClassLoader = null; if (overridenClassLoader != null) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { NamingHelper.bind(this); return new EntityManagerFactoryImpl( cfg.buildSessionFactory(), transactionType, discardOnClose ); } catch (HibernateException e) { throw new PersistenceException( e ); } finally { if (thread != null) { thread.setContextClassLoader( contextClassLoader ); } } }
/** * {@inheritDoc} */ public Metamodel getMetamodel() { return getEntityManagerFactory().getMetamodel(); }
/** * {@inheritDoc} */ public CriteriaBuilder getCriteriaBuilder() { return getEntityManagerFactory().getCriteriaBuilder(); }
public EntityManager createEntityManager() { return createEntityManager( null ); }
public EntityManagerFactory buildEntityManagerFactory(BootstrapServiceRegistryBuilder builder) { Thread thread = null; ClassLoader contextClassLoader = null; if ( overridenClassLoader != null ) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { final ServiceRegistry serviceRegistry = buildLifecycleControledServiceRegistry( builder ); return new EntityManagerFactoryImpl( transactionType, discardOnClose, getSessionInterceptorClass( cfg.getProperties() ), cfg, serviceRegistry, persistenceUnitName ); } catch (HibernateException e) { throw new PersistenceException( getExceptionHeader() + "Unable to build EntityManagerFactory", e ); } finally { if (thread != null) { thread.setContextClassLoader( contextClassLoader ); } } }
public <X> Root<X> from(Class<X> entityClass) { EntityType<X> entityType = criteriaBuilder.getEntityManagerFactory() .getMetamodel() .entity( entityClass ); if ( entityType == null ) { throw new IllegalArgumentException( entityClass + " is not an entity" ); } return from( entityType ); }
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; import org.hibernate.jpa.internal.EntityManagerFactoryImpl; public Connection getConnection(EntityManagerFactory emf) throws SQLException { final EntityManagerFactoryImpl hibernateEmf = (EntityManagerFactoryImpl) emf; return hibernateEmf.getSessionFactory().getServiceRegistry().getService(ConnectionProvider.class).getConnection(); }
public EntityManager createEntityManager() { return createEntityManager( null ); }
public EntityManagerFactory buildEntityManagerFactory() { Thread thread = null; ClassLoader contextClassLoader = null; if (overridenClassLoader != null) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { configure( (Properties)null, null ); NamingHelper.bind(this); return new EntityManagerFactoryImpl( cfg.buildSessionFactory(), transactionType, discardOnClose, getSessionInterceptorClass( cfg.getProperties() ) ); } catch (HibernateException e) { throw new PersistenceException( getExceptionHeader() + "Unable to build EntityManagerFactory", e ); } finally { if (thread != null) { thread.setContextClassLoader( contextClassLoader ); } } }
@SuppressWarnings({ "unchecked" }) public SingularAttributeJoin( CriteriaBuilderImpl criteriaBuilder, Class<X> javaType, PathSource<Z> pathSource, SingularAttribute<? super Z, ?> joinAttribute, JoinType joinType) { super( criteriaBuilder, javaType, pathSource, joinAttribute, joinType ); this.model = (Bindable<X>) ( Attribute.PersistentAttributeType.EMBEDDED == joinAttribute.getPersistentAttributeType() ? joinAttribute : criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType ) ); }
EntityManagerFactoryImpl emf=(EntityManagerFactoryImpl)lcemfb.getNativeEntityManagerFactory(); SessionFactoryImpl sf=emf.getSessionFactory(); StandardServiceRegistry serviceRegistry = sf.getSessionFactoryOptions().getServiceRegistry(); MetadataSources metadataSources = new MetadataSources(new BootstrapServiceRegistryBuilder().build()); Metadata metadata = metadataSources.buildMetadata(serviceRegistry); SchemaUpdate update=new SchemaUpdate(serviceRegistry,metadata); //To create SchemaUpdate // You can either create SchemaExport from the above details, or you can get the existing one as follows: try { Field field = SessionFactoryImpl.class.getDeclaredField("schemaExport"); field.setAccessible(true); SchemaExport schemaExport = (SchemaExport) field.get(serviceRegistry); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); }
public static void cleanDatabase(EntityManager entityManager) { if (log.isInfoEnabled()) { log.info("will clean database"); } ServiceRegistry serviceRegistry = ((EntityManagerFactoryImpl) entityManager.getEntityManagerFactory()) .getSessionFactory().getServiceRegistry(); Configuration configuration = null; try { Field configurationField = SessionFactoryServiceRegistryImpl.class.getDeclaredField("configuration"); configurationField.setAccessible(true); configuration = (Configuration) configurationField.get(serviceRegistry); } catch (IllegalAccessException e) { if (log.isErrorEnabled()) { log.error("should not occur", e); } } catch (NoSuchFieldException e) { if (log.isErrorEnabled()) { log.error("should not occur", e); } } SchemaExport schemaExport = new SchemaExport(serviceRegistry, configuration); schemaExport.setHaltOnError(true); // drop schemaExport.execute(true, true, true, false); // create schemaExport.execute(true, true, false, true); }
private static CollectionPersister resolvePersister(CriteriaBuilderImpl criteriaBuilder, PluralAttribute attribute) { SessionFactoryImplementor sfi = (SessionFactoryImplementor) criteriaBuilder.getEntityManagerFactory().getSessionFactory(); return sfi.getCollectionPersister( resolveRole( attribute ) ); }
public MapKeyAttribute(CriteriaBuilderImpl criteriaBuilder, MapAttribute<?, K, ?> attribute) { this.attribute = attribute; this.jpaType = attribute.getKeyType(); this.jpaBinableJavaType = attribute.getKeyJavaType(); this.jpaBindableType = Type.PersistenceType .ENTITY.equals( jpaType.getPersistenceType() ) ? BindableType.ENTITY_TYPE : BindableType.SINGULAR_ATTRIBUTE; String guessedRoleName = determineRole( attribute ); SessionFactoryImplementor sfi = (SessionFactoryImplementor) criteriaBuilder.getEntityManagerFactory().getSessionFactory(); mapPersister = sfi.getCollectionPersister( guessedRoleName ); if ( mapPersister == null ) { throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" ); } mapKeyType = mapPersister.getIndexType(); if ( mapKeyType == null ) { throw new IllegalStateException( "Could not determine map-key type [" + guessedRoleName + "]" ); } this.persistentAttributeType = mapKeyType.isEntityType() ? PersistentAttributeType.MANY_TO_ONE : mapKeyType.isComponentType() ? PersistentAttributeType.EMBEDDED : PersistentAttributeType.BASIC; }
@Override protected Session getRawSession() { if ( session == null ) { SessionBuilder sessionBuilder = getEntityManagerFactory().getSessionFactory().withOptions(); if (sessionInterceptorClass != null) { try { Interceptor interceptor = (Interceptor) sessionInterceptorClass.newInstance(); sessionBuilder.interceptor( interceptor ); } catch (InstantiationException e) { throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e); } catch (IllegalAccessException e) { throw new PersistenceException("Unable to instanciate session interceptor: " + sessionInterceptorClass, e); } catch (ClassCastException e) { throw new PersistenceException("Session interceptor does not implement Interceptor: " + sessionInterceptorClass, e); } } sessionBuilder.autoJoinTransactions( getTransactionType() != PersistenceUnitTransactionType.JTA ); session = sessionBuilder.openSession(); if ( persistenceContextType == PersistenceContextType.TRANSACTION ) { ( (SessionImplementor) session ).setAutoClear( true ); } } return session; }
final SessionFactoryImplementor factoryImplementor = ( SessionFactoryImplementor ) entityManagerFactory.getSessionFactory(); final NamedSQLQueryDefinition queryDefinition = factoryImplementor.getNamedSQLQuery( name ); try {