Reputation: 23
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;
}
Upvotes: 0
Views: 4018
Reputation: 9574
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.
Upvotes: 1