Error I am getting is : 1767 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
If I specify : name="java:hibernate/SessionFactory" is the hiberate.cfg.xml file with session-factory, I get the following error 2052 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDI javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
My intent is to just get moving to start with and then build my learning on hibernate along the way. I have less time and lot to get done :) (nothing new eh!!).
Please help. All files below. SIddharth
HibernateSessionFactory.java file package com.server.hibernate;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration;
/** * Singleton class, generates only one single SessionFactory is instantiated * Actually it only wraps the Hibernate SessionFactory. * * Implementor is free to use any kind of JTA or Thread transactionFactories. */ public class HibernateSessionFactory { private static SessionFactory sessionFactory = null ; private static HibernateSessionFactory hibernateSessionFactory = null ; private static Session mySession ; private HibernateSessionFactory() { }
public static Session getInstance() { if (null == hibernateSessionFactory) { hibernateSessionFactory = new HibernateSessionFactory() ; if (null == sessionFactory) { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); sessionFactory.openSession() ; mySession = sessionFactory.getCurrentSession() ; } } return mySession; }
public Session getCurrentSession() { return sessionFactory.getCurrentSession(); }
public static void close(){ if (sessionFactory != null) sessionFactory.close(); sessionFactory = null;
} }
junit java file package com.server.testcase;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.hibernate.Session; import org.hibernate.SessionFactory; import com.server.hibernate.HibernateSessionFactory; import com.server.hibernate.database.tables.DeviceTable; import com.server.hibernate.database.tables.TableRow; import junit.framework.TestCase;
import org.junit.After; import org.junit.Before; import org.junit.Test;
public class HibernateConnectionTest { private HibernateConnectionTest myTest = null ; private Session hSession = null ; private TableRow deviceTableRow = null ; @Before public void setup() { hSession = HibernateSessionFactory.getInstance() ; } @Test public void insertRow() { assertNotNull(hSession); DeviceTable deviceTable = new DeviceTable() ; deviceTable.setDeviceId(1) ; deviceTable.setHashId(2) ; deviceTable.setPhoneNumber(3) ; deviceTable.setSimId(4) ; deviceTableRow = new TableRow(hSession, new String("deviceDetails")) ; assertNotNull(deviceTableRow) ; ArrayList<String> inTestRow = new ArrayList<String>(TableRow.COLUMN_SIZE); ArrayList<String> outTestRow ; inTestRow.add(0, "hashValue1") ; inTestRow.add(1, "deviceId1") ; inTestRow.add(2, "simId1"); inTestRow.add(3, "phoneNumber1") ; deviceTableRow.setRow(inTestRow); outTestRow = (ArrayList<String>) deviceTableRow.getRow() ; assertEquals(outTestRow.get(TableRow.HASH_ID), "hashValue1") ; assertEquals(outTestRow.get(TableRow.DEVICE_ID), "deviceId1") ; assertEquals(outTestRow.get(TableRow.SIM_ID), "simId1") ; assertEquals(outTestRow.get(TableRow.PHONE_NUMBER), "phoneNumber1") ; } @After public void teardown() { hSession.close(); } }
DeviceTable.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 9 Jun, 2010 11:14:41 PM by Hibernate Tools 3.3.0.GA --> <hibernate-mapping> <class name="com.server.hibernate.database.tables.DeviceTable" table="deviceDetails"> <id name="hashId" type="int"> <column name="hashId" /> <generator class="assigned" /> </id> <property name="deviceId" type="int"> <column name="deviceId" /> </property> <property name="phoneNumber" type="int"> <column name="phoneNumber" /> </property> <property name="simId" type="int"> <column name="simId" /> </property> </class> </hibernate-mapping>
package com.server.hibernate.database.tables; import java.io.Serializable; public class DeviceTable implements Serializable { private int hashId ; private int deviceId ; private int phoneNumber ; private int simId ; public int getHashId () { return hashId ; } public int getDeviceId () { return deviceId ; } public int getPhoneNumber () { return phoneNumber ; } public int getSimId () { return simId ; } public void setHashId (int newHashId) { hashId = newHashId ; } public void setDeviceId (int newDeviceId) { deviceId = newDeviceId ; } public void setPhoneNumber (int newPhoneNumber) { phoneNumber = newPhoneNumber ; } public void setSimId (int newSimId) { simId = newSimId ; } }
Hibernate.cfg.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <mapping resource="com/mcruiseon/server/hibernate/database/tables/DeviceTable.hbm.xml"/> </session-factory> </hibernate-configuration>
Hibernate properties file (note the 5 in MySQL5InnoDBDialect, is this correct, I found this class in the jar file)
hibernate.connection.pool_size=2 hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.password=rajkumar hibernate.connection.url=jdbc:mysql://localhost/mCruiseOnServerDB hibernate.connection.username=root hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|