/** * Instantiates a new DBResultHelper. */ public DBResultHelper() { AnnotationConfiguration conf = new AnnotationConfiguration() .addAnnotatedClass(TestClass.class) .addAnnotatedClass(TestInstance.class) .addAnnotatedClass(TestMethod.class) .addAnnotatedClass(Group.class) .addAnnotatedClass(MethodType.class) .addAnnotatedClass(TestMethodException.class) .addAnnotatedClass(Suite.class).addAnnotatedClass(Test.class); // TODO make configuration accessible to a library user conf.configure("/hibernate.cfg.xml"); factory = conf.buildSessionFactory(); }
if ( rsrc != null ) { log.debug( "{} <- {}", name, rsrc ); addResource( rsrc.getValue() ); addJar( new File( jar.getValue() ) ); addFile( file.getValue() ); addPackage( pckg.getValue() ); addAnnotatedClass( loadedClass );
public static synchronized Session getSession() { if (sessionFactory == null) sessionFactory = new AnnotationConfiguration().configure(configuration).buildSessionFactory(); return sessionFactory.openSession(); }
public SchemaGenerator(String packageName, String output) throws Exception { cfg = new AnnotationConfiguration(); cfg.setProperty("hibernate.hbm2ddl.auto","create"); for(Class<?> clazz : getClasses(packageName)) { cfg.addAnnotatedClass(clazz); } try { this.output = new File(output); if(!this.output.exists()) this.output.mkdirs(); } catch (Exception e) { throw new RuntimeException(e); } }
/*package*/ static SessionFactory initSessionFactory(String resource) { if (_factory == null) { try { _factory = resource == null ? new AnnotationConfiguration().configure().buildSessionFactory() : new AnnotationConfiguration().configure(resource).buildSessionFactory(); log.debug( "Hibernate configuration file loaded: " + (resource == null ? "hibernate.cfg.xml" : resource)); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed log.error("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } return _factory; }
/** * Create the Hibernate <code>Configuration</code> for specified packages * @param packageNames the packages to scan for entities * @return the Hibernate Configuration */ protected Configuration createConfiguration(List<String> packageNames) { mappedClasses = new ArrayList<Class<?>>(); if (packageNames == null) { packageNames = Arrays.asList("model"); } log.info("Creating hibernate annotation configuration"); AnnotationConfiguration cfg = new AnnotationConfiguration(); ResolverUtil<Object> resolverUtil = new ResolverUtil<Object>(); String[] packages = new String[packageNames.size()]; packages = packageNames.toArray(packages); resolverUtil.findAnnotated(Entity.class, packages); resolverUtil.findAnnotated(javax.persistence.Entity.class, packages); for (Class<?> clazz : resolverUtil.getClasses()) { cfg.addAnnotatedClass(clazz); mappedClasses.add(clazz); log.info(" * " + clazz + " added to config"); } Collections.sort(mappedClasses, new Comparator<Class<?>>() { @Override public int compare(Class<?> aClass, Class<?> aClass1) { return aClass.getSimpleName().compareTo(aClass1.getSimpleName()); } }); return cfg; }
private void setAnnotadedClasses() { cfg.addAnnotatedClass(Country.class).addAnnotatedClass(State.class).addAnnotatedClass(PhoneBook.class) .addAnnotatedClass(PhoneBookEntry.class); }
Collection<Class> classes = (Collection<Class>) workingVars.get( HibernatePersistence.LOADED_CLASSES ); for ( Class clazz : classes ) { cfg.addAnnotatedClass( clazz ); ); for ( String pkg : packages ) { cfg.addPackage( pkg ); try { if ( xmlFile.endsWith( META_INF_ORM_XML ) ) useMetaInf = true; cfg.addResource( xmlFile ); try { cfg.addInputStream( new BufferedInputStream( is.getStream() ) );
public Ejb3Configuration() { settingsFactory = new InjectionSettingsFactory(); cfg = new AnnotationConfiguration( settingsFactory ); cfg.setEntityNotFoundDelegate( ejb3EntityNotFoundDelegate ); listenerConfigurator = new EventListenerConfigurator( this ); }
public HibernateDatabase(Properties properties) throws HibernateException { cfg = new AnnotationConfiguration(); cfg.setProperties(properties); setAnnotadedClasses(); loadConfig(); }
@Override public Configuration getConfiguration() { if ( configuration == null ) { configuration = new AnnotationConfiguration(); } return configuration; }
cfg.configure( cfgFileName ); cfg.addProperties( preparedProperties ); //persistence.xml has priority over hibernate.cfg.xml && ( cfg.getInterceptor() == null || cfg.getInterceptor().equals( defaultInterceptor ) ) ) { cfg.setInterceptor( (Interceptor) interceptor.newInstance() ); && ( cfg.getNamingStrategy() == null || cfg.getNamingStrategy().equals( defaultNamingStrategy ) ) ) { cfg.setNamingStrategy( (NamingStrategy) namingStragegy.newInstance() ); if ( ! "true".equalsIgnoreCase( cfg.getProperty( Environment.AUTOCOMMIT ) ) ) { log.warn( "{} = false break the EJB3 specification", Environment.AUTOCOMMIT );
private void loadConfig() { URL xmlConfig = HibernateDatabase.class.getClassLoader().getResource(HIBERNATE_CONFIG_FILE); if (xmlConfig != null) { cfg.configure(xmlConfig); } } }
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 ); } } }
SessionFactory buildSessionFactory() throws HibernateException { return cfg.buildSessionFactory(); }
public Ejb3Configuration addResource(String path, ClassLoader classLoader) throws MappingException { cfg.addResource( path, classLoader ); return this; }
public Ejb3Configuration addFile(File xmlFile) throws MappingException { Thread thread = null; ClassLoader contextClassLoader = null; if (overridenClassLoader != null) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { cfg.addFile( xmlFile ); return this; } finally { if (thread != null) thread.setContextClassLoader( contextClassLoader ); } }
public Ejb3Configuration addPackage(String packageName) throws MappingException { Thread thread = null; ClassLoader contextClassLoader = null; if (overridenClassLoader != null) { thread = Thread.currentThread(); contextClassLoader = thread.getContextClassLoader(); thread.setContextClassLoader( overridenClassLoader ); } try { cfg.addPackage( packageName ); return this; } finally { if (thread != null) thread.setContextClassLoader( contextClassLoader ); } }
public SessionFactory getFactory() { try { if (factory == null) { factory = new AnnotationConfiguration().configure() .buildSessionFactory(); } else if (factory.isClosed()) { factory = new AnnotationConfiguration().configure() .buildSessionFactory(); } } catch (Exception e) { LOG.error("Exception in getFactory while creating new Factory M:" + e.getMessage() + " C: " + e.getCause() + " ST: " + e.getStackTrace()); } return factory; }
private static SessionFactory getServerConnection(Server s) { SessionFactory sessionFactory = null; try { AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration(); Configuration configuration = annotationConfiguration.configure(); // String property1 = configuration // .getProperty(HIBERNATE_CONNECTION_URL); configuration.setProperty(HIBERNATE_CONNECTION_URL, s .getConnection()); // String property2 = configuration // .getProperty(HIBERNATE_CONNECTION_URL); // System.err.println(property2); // System.err.println(property1.equals(property2)); sessionFactory = configuration.buildSessionFactory(); sessionFactories.put(s, sessionFactory); } catch (Throwable ex) { ex.printStackTrace(); throw new ExceptionInInitializerError(ex); } return sessionFactory; }