List of usage examples for org.hibernate.cfg Configuration Configuration
public Configuration()
From source file:com.akursat.util.HibernateUtil.java
private static SessionFactory buildSessionFactory() { try {/*from w w w . j av a 2 s .com*/ // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:com.alachisoft.sample.hibernate.factory.CustomerFactory.java
License:Open Source License
public CustomerFactory() { Object obj = new Configuration(); Object obj2 = ((Configuration) obj).configure(); factory = new Configuration().configure().buildSessionFactory(); session = factory.openSession();// w ww.j a va 2 s. c o m }
From source file:com.alfredmuponda.lostandfound.persistence.HibernateUtil.java
private static SessionFactory buildSessionFactory() { try {//from www.j a v a 2 s .c om // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); System.out.println("Hibernate Configuration loaded"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); System.out.println("Hibernate serviceRegistry created"); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:com.alfredmuponda.lostandfound.persistence.HibernateUtil.java
private static SessionFactory buildSessionAnnotationFactory() { try {/*w w w . ja v a2s. co m*/ // Create the SessionFactory from hibernate.cfg.xml Configuration configuration = new Configuration(); configuration.configure("hibernate-annotation.cfg.xml"); System.out.println("Hibernate Annotation Configuration loaded"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); System.out.println("Hibernate Annotation serviceRegistry created"); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:com.alfredmuponda.lostandfound.persistence.HibernateUtil.java
private static SessionFactory buildSessionJavaConfigFactory() { try {/*from w ww .j a va 2 s.co m*/ Configuration configuration = new Configuration(); //Create Properties, can be read from property files too Properties props = new Properties(); props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); props.put("hibernate.connection.url", "jdbc:mysql://localhost/LostAndFound"); props.put("hibernate.connection.username", "hitrac"); props.put("hibernate.connection.password", "hitrac"); props.put("hibernate.current_session_context_class", "thread"); configuration.setProperties(props); //we can set mapping file or class with annotation //addClass(Employee1.class) will look for resource // com/journaldev/hibernate/model/Employee1.hbm.xml (not good) //configuration.addAnnotatedClass(Employee1.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); System.out.println("Hibernate Java Config serviceRegistry created"); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
From source file:com.almuradev.backpack.backend.DatabaseManager.java
License:MIT License
public static void init(Path databaseRootPath, String name) { final Configuration configuration = new Configuration(); configuration.setProperty("hibernate.connection.provider_class", CONNECTION_PROVIDER); configuration.setProperty("hibernate.dialect", DIALECT); configuration.setProperty("hibernate.hikari.dataSourceClassName", DRIVER_CLASSPATH); configuration.setProperty("hibernate.hikari.dataSource.url", DATA_SOURCE_PREFIX + databaseRootPath.toString() + File.separator + name + DATA_SOURCE_SUFFIX); configuration.setProperty("hibernate.hbm2ddl.auto", AUTO_SCHEMA_MODE); registerTables(configuration);/*ww w .ja va2 s . co m*/ sessionFactory = configuration.buildSessionFactory( new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build()); }
From source file:com.amalto.core.query.StorageFullTextTest.java
License:Open Source License
public void testGenerateIdFetchSize() throws Exception { // Test "stream resultset" RDBMSDataSource dataSource = new RDBMSDataSource("TestDataSource", "MySQL", "", "", "", 0, 0, "", "", false, "update", false, new HashMap(), "", "", null, "", "", "", false); Configuration configuration = new Configuration(); configuration.setProperty(Environment.STATEMENT_FETCH_SIZE, "1000"); HibernateStorage storage = new HibernateStorage("HibernateStorage"); storage.init(getDatasource("RDBMS-1-NO-FT")); storage.prepare(repository, Collections.<Expression>emptySet(), false, false); Class storageClass = storage.getClass(); Field dataSourceField = storageClass.getDeclaredField("dataSource"); dataSourceField.setAccessible(true); dataSourceField.set(storage, dataSource); Field configurationField = storageClass.getDeclaredField("configuration"); configurationField.setAccessible(true); configurationField.set(storage, configuration); Method generateIdFetchSizeMethod = storageClass.getDeclaredMethod("generateIdFetchSize", null); generateIdFetchSizeMethod.setAccessible(true); assertEquals(Integer.MIN_VALUE, generateIdFetchSizeMethod.invoke(storage, null)); // Test config batch size dataSource = new RDBMSDataSource("TestDataSource", "H2", "", "", "", 0, 0, "", "", false, "update", false, new HashMap(), "", "", null, "", "", "", false); storage = new HibernateStorage("HibernateStorage"); storage.init(getDatasource("RDBMS-1-NO-FT")); storage.prepare(repository, Collections.<Expression>emptySet(), false, false); storageClass = storage.getClass();/*from w w w .j a v a2s.com*/ dataSourceField = storageClass.getDeclaredField("dataSource"); dataSourceField.setAccessible(true); dataSourceField.set(storage, dataSource); configurationField = storageClass.getDeclaredField("configuration"); configurationField.setAccessible(true); configurationField.set(storage, configuration); assertEquals(1000, generateIdFetchSizeMethod.invoke(storage, null)); // Test default batch size configuration = new Configuration(); configurationField = storageClass.getDeclaredField("configuration"); configurationField.setAccessible(true); configurationField.set(storage, configuration); assertEquals(500, generateIdFetchSizeMethod.invoke(storage, null)); }
From source file:com.amalto.core.storage.hibernate.HibernateStorage.java
License:Open Source License
@SuppressWarnings("serial") protected void internalInit() { if (!dataSource.supportFullText()) { LOGGER.warn("Storage '" + storageName + "' (" + storageType //$NON-NLS-1$//$NON-NLS-2$ + ") is not configured to support full text queries."); //$NON-NLS-1$ }/*from www. j a va2 s . co m*/ configuration = new Configuration() { protected transient Mapping mapping = buildMapping(); @Override public Mappings createMappings() { return new MDMMappingsImpl(); } class MDMMappingsImpl extends MappingsImpl { @Override public Table addTable(String schema, String catalog, String name, String subselect, boolean isAbstract) { name = getObjectNameNormalizer().normalizeIdentifierQuoting(name); schema = getObjectNameNormalizer().normalizeIdentifierQuoting(schema); catalog = getObjectNameNormalizer().normalizeIdentifierQuoting(catalog); String key = subselect == null ? Table.qualify(catalog, schema, name) : subselect; Table table = tables.get(key); if (table == null) { table = new MDMTable(); table.setAbstract(isAbstract); table.setName(name); table.setSchema(schema); table.setCatalog(catalog); table.setSubselect(subselect); tables.put(key, table); } else { if (!isAbstract) { table.setAbstract(false); } } return table; } @Override public Table addDenormalizedTable(String schema, String catalog, String name, boolean isAbstract, String subSelect, final Table includedTable) throws DuplicateMappingException { name = getObjectNameNormalizer().normalizeIdentifierQuoting(name); schema = getObjectNameNormalizer().normalizeIdentifierQuoting(schema); catalog = getObjectNameNormalizer().normalizeIdentifierQuoting(catalog); String key = subSelect == null ? Table.qualify(catalog, schema, name) : subSelect; if (tables.containsKey(key)) { throw new DuplicateMappingException("Table " + key + " is duplicated.", //$NON-NLS-1$//$NON-NLS-2$ DuplicateMappingException.Type.TABLE, name); } Table table = new MDMDenormalizedTable(includedTable) { @SuppressWarnings({ "unchecked" }) @Override public Iterator<Index> getIndexIterator() { List<Index> indexes = new ArrayList<Index>(); Iterator<Index> IndexIterator = super.getIndexIterator(); while (IndexIterator.hasNext()) { Index parentIndex = IndexIterator.next(); Index index = new Index(); index.setName(tableResolver.get(parentIndex.getName())); index.setTable(this); index.addColumns(parentIndex.getColumnIterator()); indexes.add(index); } return indexes.iterator(); } }; table.setAbstract(isAbstract); table.setName(name); table.setSchema(schema); table.setCatalog(catalog); table.setSubselect(subSelect); tables.put(key, table); return table; } } }; // Setting our own entity resolver allows to ensure the DTD found/used are what we expect (and not potentially // one provided by the application server). configuration.setEntityResolver(ENTITY_RESOLVER); }
From source file:com.anyframe.appserver.websocket.client.hibernate.GetVo.java
License:Open Source License
private static SessionFactory getInstanceofSessionFactory() { // A SessionFactory is set up once for an application if (sessionFactory == null) sessionFactory = new Configuration().configure() // configures settings from hibernate.cfg.xml .buildSessionFactory();/* w ww.j ava2 s . c o m*/ return sessionFactory; }
From source file:com.anyuan.thomweboss.persistence.dao.HibernateDaoTemplate.java
License:Apache License
/** * ?hibernatejdbctransaction//w ww. jav a 2 s.c o m * @author Thomsen * @since Dec 15, 2012 11:20:02 PM * @return */ public static Transaction getTranscation() { Transaction transaction = null; try { Configuration configuration = new Configuration().configure(); // classpathhibernate.cfg.xml ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(registry); // hibernate4.0 ?buildSessionFactory() // Session session = sessionFactory.getCurrentSession(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } catch (HibernateException e) { e.printStackTrace(); } if (transaction == null) { throw new HibernateException("transaction is null"); } return transaction; }