Hi,
I have a user class which i'm mapping to a table in a MySQL database (i'm using version 4.0.12). It's a simple class which has fields like username, password .
I've written the following methods that returns me the configuration and Session objects, which i used in my db related methods:
private static Configuration getConfig() throws HibernateException {
Configuration config = new Configuration();
return config.configure();
}
private static Session getSession() throws HibernateException {
Configuration config = getConfig();
SessionFactory sessionFactory = null;
Session session = null;
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
return session;
}
Here's my method that adds the record:
public void addInDB() {
Session session = null;
try {
session = getSession();
} catch (HibernateException he){
he.printStackTrace();
}
try {
session.save(this);
session.flush();
session.connection().commit();
} catch (Exception e) {
try {
session.connection().rollback();
} catch (Exception e1) {
e.printStackTrace();
}
}
}
the following is the mapping file:
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory name="/jndi/dcmsSessionFactory">
<!-- properties -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dcms</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">4</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- mapping files -->
<mapping resource="UsersVO.hbm.xml"/>
<mapping resource="UserPrivilegesVO.hbm.xml"/>
<mapping resource="UserCategories.hbm.xml"/>
<mapping resource="PrivilegeDetails.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Mapping for a table UsersVO:
<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="org.dcms.util.UsersVO"
table="dcms_users"
dynamic-update="true"
dynamic-insert="true">
<id name = "user_userid" type ="int" column="user_userid">
<generator class = "increment" />
</id>
<property name = "user_username" type ="string" column ="user_username" length="12"/>
<property name = "user_password" type ="string" column ="user_password" length="12"/>
<property name = "user_status" type="character" column ="user_status" />
<property name = "user_categoryid" type="int" column ="user_categoryid" length="1" />
<property name = "user_logdatetime" type="date" column ="user_logdatetime" />
<property name = "user_sessionid" type="string" column ="user_sessionid" length="12" />
</class>
</hibernate-mapping>
When i run the code i get a NoInitialContextException. I'm unable to figureout what the problem is. I've pasted the output here. Can anyone please help ?
C:\j2sdk1.4\bin\javaw.exe -classpath C:\j2sdk1.4\jre\lib\charsets.jar;C:\j2sdk1.4\jre\lib\jaws.jar;C:\j2sdk1.4\jre\lib\jce.jar;C:\j2sdk1.4\jre\lib\jsse.jar;C:\j2sdk1.4\jre\lib\rt.jar;C:\j2sdk1.4\jre\lib\sunrsasign.jar;C:\j2sdk1.4\jre\lib\ext\lucene-1.2.jar;C:\j2sdk1.4\jre\lib\ext\mysql-connector-java-3.0.8-stable-bin.jar;C:\DCMS\build;C:\mysql-connector-java-3.0.8-stable\mysql-connector-java-3.0.8-stable-bin.jar;C:\DCMS\lib\hibernate2.jar;C:\DCMS\lib\cglib-asm.jar;C:\DCMS\lib\commons-beanutils.jar;C:\DCMS\lib\commons-collections.jar;C:\DCMS\lib\commons-dbcp.jar;C:\DCMS\lib\commons-logging.jar;C:\DCMS\lib\commons-pool.jar;C:\DCMS\lib\dom4j.jar;C:\DCMS\lib\odmg.jar;C:\DCMS\lib\xml-apis.jar;C:\DCMS\lib\commons-lang.jar;C:\DCMS\src\org\dcms\util;C:\DCMS\build\org org.dcms.util.UsersVO
May 12, 2004 10:17:57 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.0.3
May 12, 2004 10:17:57 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Hi net.sf.hibernate.cfg.Configuration@18e2b22
May 12, 2004 10:17:57 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
May 12, 2004 10:17:57 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: JVM proxy support: true
May 12, 2004 10:17:57 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UsersVO.hbm.xml
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UsersVO -> dcms_users
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserPrivilegesVO.hbm.xml
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserPrivilegesVO -> dcms_userprivileges
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserCategories.hbm.xml
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserCategoriesVO -> dcms_usercategories
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: PrivilegeDetails.hbm.xml
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.PrivilegeDetailsVO -> dcms_privilegedetails
May 12, 2004 10:17:58 AM net.sf.hibernate.cfg.Configuration configure
INFO: Configured SessionFactory: /jndi/dcmsSessionFactory
May 12, 2004 10:17:58 AM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.MySQLDialect
May 12, 2004 10:17:58 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 4
May 12, 2004 10:17:58 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dcms
May 12, 2004 10:17:58 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=}
May 12, 2004 10:17:59 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
May 12, 2004 10:17:59 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Hi net.sf.hibernate.cfg.Configuration@62937c
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UsersVO.hbm.xml
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UsersVO -> dcms_users
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserPrivilegesVO.hbm.xml
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserPrivilegesVO -> dcms_userprivileges
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserCategories.hbm.xml
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserCategoriesVO -> dcms_usercategories
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: PrivilegeDetails.hbm.xml
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.PrivilegeDetailsVO -> dcms_privilegedetails
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration configure
INFO: Configured SessionFactory: /jndi/dcmsSessionFactory
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
May 12, 2004 10:18:00 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
May 12, 2004 10:18:00 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
May 12, 2004 10:18:00 AM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.MySQLDialect
May 12, 2004 10:18:00 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 4
May 12, 2004 10:18:00 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dcms
May 12, 2004 10:18:00 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=}
May 12, 2004 10:18:00 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use outer join fetching: true
May 12, 2004 10:18:01 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use scrollable result sets: true
May 12, 2004 10:18:01 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: JDBC 2 max batch size: 15
May 12, 2004 10:18:01 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: echoing all SQL to stdout
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Factory name: /jndi/dcmsSessionFactory
May 12, 2004 10:18:02 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
WARNING: 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:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.getNameParser(InitialContext.java:429)
at net.sf.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
at net.sf.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:627)
at org.dcms.util.UsersVO.getSession(UsersVO.java:205)
at org.dcms.util.UsersVO.addInDB(UsersVO.java:108)
at org.dcms.util.UsersVO.main(UsersVO.java:229)
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Query language substitutions: {}
Hibernate: insert into dcms_users (user_username, user_password, user_status, user_categoryid, user_logdatetime, user_sessionid, user_userid) values (?, ?, ?, ?, ?, ?, ?)
Hi net.sf.hibernate.cfg.Configuration@1bcdbf6
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UsersVO.hbm.xml
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionImpl finalize
WARNING: unclosed connection
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UsersVO -> dcms_users
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserPrivilegesVO.hbm.xml
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserPrivilegesVO -> dcms_userprivileges
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: UserCategories.hbm.xml
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.UserCategoriesVO -> dcms_usercategories
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration addResource
INFO: Mapping resource: PrivilegeDetails.hbm.xml
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: org.dcms.util.PrivilegeDetailsVO -> dcms_privilegedetails
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration configure
INFO: Configured SessionFactory: /jndi/dcmsSessionFactory
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
May 12, 2004 10:18:02 AM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
May 12, 2004 10:18:02 AM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.MySQLDialect
May 12, 2004 10:18:02 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 4
May 12, 2004 10:18:02 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dcms
May 12, 2004 10:18:02 AM net.sf.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=}
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use outer join fetching: true
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Use scrollable result sets: true
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: JDBC 2 max batch size: 15
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: echoing all SQL to stdout
May 12, 2004 10:18:02 AM net.sf.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:mysql://localhost:3306/dcms
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Factory name: /jndi/dcmsSessionFactory
May 12, 2004 10:18:02 AM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
WARNING: 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:640)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
at javax.naming.InitialContext.getNameParser(InitialContext.java:429)
at net.sf.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
at net.sf.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:627)
at org.dcms.util.UsersVO.getSession(UsersVO.java:205)
at org.dcms.util.UsersVO.updateInDbUser(UsersVO.java:154)
at org.dcms.util.UsersVO.main(UsersVO.java:230)
May 12, 2004 10:18:02 AM net.sf.hibernate.impl.SessionFactoryImpl <init>
INFO: Query language substitutions: {}
Hibernate: update dcms_users set user_username=?, user_password=?, user_status=?, user_categoryid=?, user_logdatetime=?, user_sessionid=? where user_userid=?
Process terminated with exit code 0
Even though i get the insert query displayed, the DB doesn't seem to have been updated. No record gets added.
Thanks in advance.
Nilesh
|