java - Hibernate Session Factory issue -
i using hibernate 4 building application. while running application getting following error.
failed create sessionfactory object.java.lang.nosuchfielderror: trace exception in thread "main" java.lang.nullpointerexception
my code snippet is,
try{ session = new dbconnection().getsession(); tx= session.begintransaction(); ........ ........ }catch(hibernateexception ex) { if (tx!=null) tx.rollback(); ex.printstacktrace(); session.close(); dbconnection.close(); } catch(exception ex) { ex.printstacktrace(); session.close(); } finally{ session.close(); // error shown in line while run time dbconnection.close(); }
dbconnection.java
public dbconnection() { try { configuration configuration = new configuration(); configuration.configure(); serviceregistry = new serviceregistrybuilder().applysettings(configuration.getproperties()).buildserviceregistry(); factory = configuration.buildsessionfactory(serviceregistry); } catch (throwable ex) { system.err.println("failed create sessionfactory object." + ex); throw new exceptionininitializererror(ex); } } public session getsession() { return factory.opensession(); } // call during shutdown public static void close() { factory.close(); }
my configuration file :
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration system "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.mysqldialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.driver </property> <!-- assume test database name --> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> test </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="use_sql_comments">false</property> <property name="hibernate.connection.pool_size">40</property> </session-factory> </hibernate-configuration>
please guide me wrong code or jar file?
**
answer: log4j cause of issue. removed log4j , added log4j-1.2.16.jar. gets fixed. thanks!
**
in code new dbconnection() refers. need get/open session in hibernate org.hibernate.sessionfactory.sessionfactory. please check whether using sessionfactory or not.
Comments
Post a Comment