简体   繁体   中英

Not able to get session factory in hibernate

        try
        {
            System.out.println("openTxCoreSession() start...");
            TxCoreSessionFactory sessionFactory =
                TxCoreSessionFactory.getInstance("txcore.cfg.xml");
            System.out.println("Session factory created....");

            Session session = sessionFactory.openSession();
            System.out.println("session created");
            return session;
        }

        catch (Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }

Hibernate is not easy to start off with, does take a bit of time/effort.

For people who are confused, MyEclipse or JBoss Hibernate Tools, its no different.

The main reason to use Hibernate on a server platform is to get rid of the complex JDBC hell hole. The only reason you thought you needed a object relational mapping solution was to get some neatness in code and good old re-usability built into the design.

Also the below works for me.

           if (sessionFactory == null) {
                try {
                    String jdbcProperty = "jdbc:mysql://"+Globals.DBSERVER+"/MyDB" ;
                    Configuration configuration = new Configuration().configure() ;                 
                    sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder()
                                .buildServiceRegistry());

                } catch (Exception e) {
                    log.fatal("Unable to create SessionFactory for Hibernate");
                    log.fatal(e.getMessage());
                    log.fatal(e);
                    e.printStackTrace();
                }
            }

Hibernate.properties in the src folder of my eclipse project.

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost/MyDB
hibernate.connection.username=MYROOT
hibernate.connection.password=myPASSWORD
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

Also ensure that your configuration xml file (txcore.cfg.xml) is in the classpath of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM