所以我试图让我的 Java 应用程序通过
Microsoft JDBC Driver 4.0 for SQL Server连接到 SQL Server 2012 ,一切似乎都很顺利,但休眠只是不断回来NullExceptions
并且不会在其中执行任何内容try/catch
(因此) ,NullException
我完全不知道为什么。这是运行 hibernate的 netbeans 控制台 ( ) 中的pastebine.getMessage()
(出于这个问题的目的,我使用了一个名为 的示例表prime_table
)。
在 pastebin 日志中,您会注意到...
2013 年 2 月 11 日下午 5:21:04 org.hibernate.cfg.Configuration doConfigure 信息:已配置 SessionFactory:空
关于为什么会发生这种情况的任何想法?(我不确定,但它可能与整个堆栈跟踪有关)。
其他日志(在 JSP 构建期间)
- Netbeans(运行)
- 阿帕奇雄猫
- Apache tomcat 日志
- Sql 服务器 ERRORLOG.log
- Hibernate 可抛出堆栈跟踪(2013 年 2 月 14 日更新)
所有日志将在 2013 年 3 月中/下旬之前提供
资源,我一直在看
- 休眠示例
- [Help] session.flush() Hibernate 中出现空指针异常
- 未找到 hibernate.cfg.xml
- hibernate map java Long to MySQL BIGINT错误
预配置的 Netbeans 项目设置commandline: tree /f
(来源:iforce.co.nz)
Hibernate.cfg.xml(按照Hari"hibernate.cache.provider_class"
的建议添加)
<?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>
主要的
public class Prime {
private long prime;
public void setPrime(long nPrime){
this.prime = nPrime;
}
public long getPrime(){
return this.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>
素数表
CREATE TABLE prime_table (
[prime] bigint PRIMARY KEY NOT NULL,
)
会话功能或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();
}
}
}
据我所知,一切都应该是正确的,并且我应该能够将 Java Hibernate 中的记录插入到我的 SQL 数据库中。但由于某种原因我不能:-/
更新:休眠本身,运行良好。但是与 Spring 结合使用时,会发生这些错误。(更多信息在评论中)。