Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. public class HibernateUtilMongo {
  2.  
  3.     private static final Logger log = Logger.getLogger(HibernateUtilMongo.class.getName());
  4.     private static final SessionFactory sessionFactory;
  5.     private static final ServiceRegistry serviceRegistry;
  6.  
  7.     static {
  8.         try {
  9.             // create a new instance of OmgConfiguration
  10.             OgmConfiguration cfgogm = new OgmConfiguration();
  11.  
  12.             // enable JTA strategy            
  13.             cfgogm.setProperty(Environment.TRANSACTION_STRATEGY, "org.hibernate.transaction.JTATransactionFactory");
  14.             cfgogm.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta");
  15.            
  16.             // specify JTA platform
  17.             cfgogm.setProperty(Environment.JTA_PLATFORM, "org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform");
  18.  
  19.             // in order to select the local JBossJTA implementation it is necessary to specify these properties
  20.             cfgogm.setProperty("com.arjuna.ats.jta.jtaTMImplementation", "com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple");
  21.             cfgogm.setProperty("com.arjuna.ats.jta.jtaUTImplementation", "com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple");
  22.  
  23.             //configure MongoDB connection
  24.             cfgogm.setProperty("hibernate.ogm.datastore.provider", "mongodb");
  25.             cfgogm.setProperty("hibernate.ogm.datastore.grid_dialect", "org.hibernate.ogm.dialect.mongodb.MongoDBDialect"); //you can ignore this setting
  26.             cfgogm.setProperty("hibernate.ogm.mongodb.database", "Messenger");
  27.             cfgogm.setProperty("hibernate.ogm.mongodb.host", "127.0.0.1");
  28.             cfgogm.setProperty("hibernate.ogm.mongodb.port", "27017");
  29.  
  30.             //add our annotated class
  31.             cfgogm.addAnnotatedClass(Message.class)
  32.                     .addAnnotatedClass(History.class);
  33.  
  34.             // create the SessionFactory
  35.             serviceRegistry = new ServiceRegistryBuilder().applySettings(cfgogm.getProperties()).buildServiceRegistry();
  36.             sessionFactory = cfgogm.buildSessionFactory(serviceRegistry);
  37.         } catch (Throwable ex) {
  38.             log.log(Level.SEVERE, "Initial SessionFactory creation failed !", ex);
  39.             throw new ExceptionInInitializerError(ex);
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * Get the SessionFactory
  45.      *
  46.      * @return SessionFactory
  47.      */
  48.     public static SessionFactory getSessionFactory() {
  49.         return sessionFactory;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement