so I'm trying to get my Java Application to connect to SQL Server 2012 through the
Microsoft JDBC Driver 4.0 for SQL Server, and everything seems to be going well but hibernate just keeps coming back with NullExceptions
and won't execute anything within the try/catch
(hence the NullException
), I have absolutely no idea why. Here is the pastebin from netbeans console (e.getMessage()
) running hibernate (for the purposes of this question, I am using an example table called prime_table
).
In the pastebin log, you'll notice ...
Feb 11, 2013 5:21:04 PM org.hibernate.cfg.Configuration doConfigure INFO: Configured SessionFactory: null
any ideas on why this is occuring? (I'm not sure, but it may be relevant to the overall stack trace).
Other logs (During JSP build)
- Netbeans (run)
- Apache tomcat
- Apache tomcat log
- Sql server ERRORLOG.log
- Hibernate throwable stacktrace (Update 2/14/2013)
All logs will be available up until Mid/Late March 2013
Resources, I've been reading
- Hibernate Example
- [Help] Null Pointer Exception in session.flush() Hibernate
- hibernate.cfg.xml not found
- hibernate map java Long to MySQL BIGINT error
Pre Configured Netbeans Project Setup commandline: tree /f
(source: iforce.co.nz)
Hibernate.cfg.xml (Added "hibernate.cache.provider_class"
as suggested by Hari)
<?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>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://Michael-PC:1433;databaseName=PrimeDB</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">online12</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Data Mappings -->
<mapping resource="prime.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Prime
public class Prime {
private long prime;
public void setPrime(long nPrime){
this.prime = nPrime;
}
public long getPrime(){
return this.prime;
}
}
prime.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="nz.co.xyleap.db.Prime" table="prime_table">
<id name="prime" type="long" column="prime" />
</class>
</hibernate-mapping>
Prime Table
CREATE TABLE prime_table (
[prime] bigint PRIMARY KEY NOT NULL,
)
Session function or FirstExample.java
public class FirstExample {
public static void main(String[] args) {
FirstExample ex = new FirstExample();
ex.session();
}
public void session() {
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("inserting record 13");
Prime p = new Prime();
p.setPrime(13);
session.save(p);
System.out.println("Done");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
As far as I know everything should be correct, and I should be able to insert records from Java Hibernate to my SQL database. But for some reason I can't :-/
UPDATE : Hibernate on its own, runs fine. But in conjunction with Spring, these errors occur. (More information in comments).