Package org.hibernate.ejb

Examples of org.hibernate.ejb.Ejb3Configuration


    // ClassLoaderServiceImpl#fromConfigSettings
    properties.put( AvailableSettings.ENVIRONMENT_CLASSLOADER, osgiClassLoader );

    osgiClassLoader.addBundle( requestingBundle );

    Ejb3Configuration cfg = new Ejb3Configuration();
    Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
    return configured != null ? configured.buildEntityManagerFactory( getBuilder( cfg, properties ) ) : null;
  }
View Full Code Here


    properties.put( org.hibernate.ejb.AvailableSettings.SCANNER,
        new OsgiScanner( ( (BundleReference) info.getClassLoader() ).getBundle() ) );

    osgiClassLoader.addClassLoader( info.getClassLoader() );

    Ejb3Configuration cfg = new Ejb3Configuration();
    Ejb3Configuration configured = cfg.configure( info, properties );
    return configured != null ? configured.buildEntityManagerFactory( getBuilder( cfg, properties ) ) : null;
  }
View Full Code Here

        props.put("hibernate.hbm2ddl.auto", "create-drop");
        props.put("hibernate.connection.username", "sa");
        props.put("hibernate.connection.password", "");
        props.put("hibernate.show_sql", "false");

        cfg = new Ejb3Configuration();
        cfg.addAnnotatedClass(Person.class);
    }
View Full Code Here

        props.put("hibernate.connection.password", "");
        props.put("hibernate.show_sql", "false");
        props.put("hibernate.ejb.interceptor",
            "org.candlepin.hibernate.EmptyStringInterceptor");

        Ejb3Configuration cfg = new Ejb3Configuration();
        cfg.addAnnotatedClass(Thing.class);
        cfg.addProperties(props);

        emf = cfg.buildEntityManagerFactory();
        em = emf.createEntityManager();
    }
View Full Code Here

  }

  @Override
  public void createTables(final String persistenceUnitName, final Map<String, Object> configOverwrites)
  {
    final Ejb3Configuration cfg = new Ejb3Configuration();
    cfg.configure(persistenceUnitName, configOverwrites);
    final SchemaExport schemaExport = new SchemaExport(cfg.getHibernateConfiguration());
    schemaExport.setOutputFile("schema.sql");
    schemaExport.create(true, true);
  }
View Full Code Here

  }

  @Override
  public void updateTables(String persistenceUnitName, final Map<String, Object> configOverwrites)
  {
    final Ejb3Configuration cfg = new Ejb3Configuration();
    cfg.configure(persistenceUnitName, configOverwrites);
    final SchemaUpdate schemaUpdate = new SchemaUpdate(cfg.getHibernateConfiguration());
    schemaUpdate.setOutputFile("schema.sql");
    schemaUpdate.execute(true, true);
  }
View Full Code Here

  }

  @Override
  public void dropTables(String persistenceUnitName, final Map<String, Object> configOverwrites)
  {
    final Ejb3Configuration cfg = new Ejb3Configuration();
    cfg.configure(persistenceUnitName, configOverwrites);
    final SchemaExport schemaExport = new SchemaExport(cfg.getHibernateConfiguration());
    schemaExport.setOutputFile("schema.sql");
    schemaExport.drop(true, true);
  }
View Full Code Here

        /* JPA basically makes it impossible to get configuration information out of persistence.xml
         * and the only non-deprecated Hibernate class (Configuration) wants to use hibernate.cfg.xml
         * so without resorting to XML parsing, this is about the best we can do.
         */

        Ejb3Configuration ejbConf = new Ejb3Configuration();
        ejbConf.configure(persistenceUnit, Collections.EMPTY_MAP);
        return (String) ejbConf.getProperties().get("hibernate.connection.url");
    }
View Full Code Here

                if (StringUtils.isEmpty(hibernateDataSource) && dbConfig == null) {
                    throw new JPAException("Cannot start a JPA manager without a properly configured database" + getConfigInfoString(configName),
                            new NullPointerException("No datasource configured"));
                }

                Ejb3Configuration cfg = new Ejb3Configuration();

                if (dbConfig.getDatasource() != null) {
                    cfg.setDataSource(dbConfig.getDatasource());
                }

                if (!Play.configuration.getProperty(propPrefix + "jpa.ddl", Play.mode.isDev() ? "update" : "none").equals("none")) {
                    cfg.setProperty("hibernate.hbm2ddl.auto", Play.configuration.getProperty(propPrefix + "jpa.ddl", "update"));
                }

                String driver = null;
                if (StringUtils.isEmpty(propPrefix)) {
                    driver = Play.configuration.getProperty("db.driver");
                } else {
                    driver = Play.configuration.getProperty(propPrefix + "driver");
                }
                cfg.setProperty("hibernate.dialect", getDefaultDialect(propPrefix, driver));
                cfg.setProperty("javax.persistence.transaction", "RESOURCE_LOCAL");


                cfg.setInterceptor(new PlayInterceptor());

                // This setting is global for all JPAs - only configure if configuring default JPA
                if (StringUtils.isEmpty(propPrefix)) {
                    if (Play.configuration.getProperty(propPrefix + "jpa.debugSQL", "false").equals("true")) {
                        org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.ALL);
                    } else {
                        org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.OFF);
                    }
                }
                // inject additional  hibernate.* settings declared in Play! configuration
                Properties additionalProperties = (Properties) Utils.Maps.filterMap(Play.configuration, "^" + propPrefix + "hibernate\\..*");
                // We must remove prefix from names
                Properties transformedAdditionalProperties = new Properties();
                for (Map.Entry<Object, Object> entry : additionalProperties.entrySet()) {
                    Object key = entry.getKey();
                    if (!StringUtils.isEmpty(propPrefix)) {
                        key = ((String) key).substring(propPrefix.length()); // chop off the prefix
                    }
                    transformedAdditionalProperties.put(key, entry.getValue());
                }
                cfg.addProperties(transformedAdditionalProperties);


                try {
                    // nice hacking :) I like it..
                    Field field = cfg.getClass().getDeclaredField("overridenClassLoader");
                    field.setAccessible(true);
                    field.set(cfg, Play.classloader);
                } catch (Exception e) {
                    Logger.error(e, "Error trying to override the hibernate classLoader (new hibernate version ???)");
                }

                for (Class<?> clazz : classes) {
                    cfg.addAnnotatedClass(clazz);
                    if (Logger.isTraceEnabled()) {
                        Logger.trace("JPA Model : %s", clazz);
                    }
                }
                String[] moreEntities = Play.configuration.getProperty(propPrefix + "jpa.entities", "").split(", ");
                for (String entity : moreEntities) {
                    if (entity.trim().equals("")) {
                        continue;
                    }
                    try {
                        cfg.addAnnotatedClass(Play.classloader.loadClass(entity));
                    } catch (Exception e) {
                        Logger.warn("JPA -> Entity not found: %s", entity);
                    }
                }

                for (ApplicationClass applicationClass : Play.classes.all()) {
                    if (applicationClass.isClass() || applicationClass.javaPackage == null) {
                        continue;
                    }
                    Package p = applicationClass.javaPackage;
                    Logger.info("JPA -> Adding package: %s", p.getName());
                    cfg.addPackage(p.getName());
                }

                String mappingFile = Play.configuration.getProperty(propPrefix + "jpa.mapping-file", "");
                if (mappingFile != null && mappingFile.length() > 0) {
                    cfg.addResource(mappingFile);
                }

                if (Logger.isTraceEnabled()) {
                    Logger.trace("Initializing JPA" + getConfigInfoString(configName) + " ...");
                }
View Full Code Here

            final String dataSource = Yalp.configuration.getProperty("hibernate.connection.datasource");
            if (StringUtils.isEmpty(dataSource) && DB.datasource == null) {
                throw new JPAException("Cannot start a JPA manager without a properly configured database", new NullPointerException("No datasource configured"));
            }

            Ejb3Configuration cfg = new Ejb3Configuration();

            if (DB.datasource != null) {
                cfg.setDataSource(DB.datasource);
            }

            if (!Yalp.configuration.getProperty("jpa.ddl", Yalp.mode.isDev() ? "update" : "none").equals("none")) {
                cfg.setProperty("hibernate.hbm2ddl.auto", Yalp.configuration.getProperty("jpa.ddl", "update"));
            }

            cfg.setProperty("hibernate.dialect", getDefaultDialect(Yalp.configuration.getProperty("db.driver")));
            cfg.setProperty("javax.persistence.transaction", "RESOURCE_LOCAL");

            // Explicit SAVE for JPABase is implemented here
            // ~~~~~~
            // We've hacked the org.hibernate.event.def.AbstractFlushingEventListener line 271, to flush collection update,remove,recreation
            // only if the owner will be saved or if the targeted entity will be saved (avoid the org.hibernate.HibernateException: Found two representations of same collection)
            // As is:
            // if (session.getInterceptor().onCollectionUpdate(coll, ce.getLoadedKey())) {
            //      actionQueue.addAction(...);
            // }
            //
            // This is really hacky. We should move to something better than Hibernate like EBEAN
            cfg.setInterceptor(new EmptyInterceptor() {

                @Override
                public int[] findDirty(Object o, Serializable id, Object[] arg2, Object[] arg3, String[] arg4, Type[] arg5) {
                    if (o instanceof JPABase && !((JPABase) o).willBeSaved) {
                        return new int[0];
                    }
                    return null;
                }

                @Override
                public boolean onCollectionUpdate(Object collection, Serializable key) throws CallbackException {
                    if (collection instanceof PersistentCollection) {
                        Object o = ((PersistentCollection) collection).getOwner();
                        if (o instanceof JPABase) {
                            if (entities.get() != null) {
                                return ((JPABase) o).willBeSaved || ((JPABase) entities.get()).willBeSaved;
                            } else {
                                return ((JPABase) o).willBeSaved;
                            }
                        }
                    } else {
                        System.out.println("HOO: Case not handled !!!");
                    }
                    return super.onCollectionUpdate(collection, key);
                }

                @Override
                public boolean onCollectionRecreate(Object collection, Serializable key) throws CallbackException {
                    if (collection instanceof PersistentCollection) {
                        Object o = ((PersistentCollection) collection).getOwner();
                        if (o instanceof JPABase) {
                            if (entities.get() != null) {
                                return ((JPABase) o).willBeSaved || ((JPABase) entities.get()).willBeSaved;
                            } else {
                                return ((JPABase) o).willBeSaved;
                            }
                        }
                    } else {
                        System.out.println("HOO: Case not handled !!!");
                    }

                    return super.onCollectionRecreate(collection, key);
                }

                @Override
                public boolean onCollectionRemove(Object collection, Serializable key) throws CallbackException {
                    if (collection instanceof PersistentCollection) {
                        Object o = ((PersistentCollection) collection).getOwner();
                        if (o instanceof JPABase) {
                            if (entities.get() != null) {
                                return ((JPABase) o).willBeSaved || ((JPABase) entities.get()).willBeSaved;
                            } else {
                                return ((JPABase) o).willBeSaved;
                            }
                        }
                    } else {
                        System.out.println("HOO: Case not handled !!!");
                    }
                    return super.onCollectionRemove(collection, key);
                }

                protected ThreadLocal<Object> entities = new ThreadLocal<Object>();

                @Override
                public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
                    entities.set(entity);
                    return super.onSave(entity, id, state, propertyNames, types);
                }

                @Override
                public void afterTransactionCompletion(org.hibernate.Transaction tx) {
                    entities.remove();
                }

            });
            if (Yalp.configuration.getProperty("jpa.debugSQL", "false").equals("true")) {
                org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.ALL);
            } else {
                org.apache.log4j.Logger.getLogger("org.hibernate.SQL").setLevel(Level.OFF);
            }
            // inject additional  hibernate.* settings declared in Yalp configuration
            cfg.addProperties((Properties) Utils.Maps.filterMap(Yalp.configuration, "^hibernate\\..*"));

            try {
                Field field = cfg.getClass().getDeclaredField("overridenClassLoader");
                field.setAccessible(true);
                field.set(cfg, Yalp.classloader);
            } catch (Exception e) {
                Logger.error(e, "Error trying to override the hibernate classLoader (new hibernate version ???)");
            }
            for (Class<?> clazz : classes) {
                if (clazz.isAnnotationPresent(Entity.class)) {
                    cfg.addAnnotatedClass(clazz);
                    if (Logger.isTraceEnabled()) {
                        Logger.trace("JPA Model : %s", clazz);
                    }
                }
            }
            String[] moreEntities = Yalp.configuration.getProperty("jpa.entities", "").split(", ");
            for (String entity : moreEntities) {
                if (entity.trim().equals("")) {
                    continue;
                }
                try {
                    cfg.addAnnotatedClass(Yalp.classloader.loadClass(entity));
                } catch (Exception e) {
                    Logger.warn("JPA -> Entity not found: %s", entity);
                }
            }
            for (ApplicationClass applicationClass : Yalp.classes.all()) {
                if (applicationClass.isClass() || applicationClass.javaPackage == null) {
                    continue;
                }
                Package p = applicationClass.javaPackage;
                Logger.info("JPA -> Adding package: %s", p.getName());
                cfg.addPackage(p.getName());
            }
            String mappingFile = Yalp.configuration.getProperty("jpa.mapping-file", "");
            if (mappingFile != null && mappingFile.length() > 0) {
                cfg.addResource(mappingFile);
            }
            if (Logger.isTraceEnabled()) {
                Logger.trace("Initializing JPA ...");
            }
            try {
                JPA.entityManagerFactory = cfg.buildEntityManagerFactory();
            } catch (PersistenceException e) {
                throw new JPAException(e.getMessage(), e.getCause() != null ? e.getCause() : e);
            }
            JPQL.instance = new JPQL();
        }
View Full Code Here

TOP

Related Classes of org.hibernate.ejb.Ejb3Configuration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.