详细解决方案
hibernate总是报空指针是咋回事
热度:7478 发布时间:2013-02-25 21:08:11
public class Student implements java.io.Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Dao {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).buildServiceRegistry();
return cfg.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Session session = Dao.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Student s=new Student();
s.setId(2);
s.setName("admin");
session.save(s);
tx.commit();
session.close();
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.tutorial.domain">
<class name="entity.Student" table="student">
<id name="id"></id>
<property name="name"></property>
</class>
</hibernate-mapping>
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">zhxl</property>
<property name="connection.password">zhxl</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
不知道为什么总报空指针
2012-11-8 16:00:30 org.hibernate.annotations.common.Version <clinit>
相关解决方案
- hibernate 存入数据库当前时间
- Hibernate 投射文件中length属性无法改变integer字段的默认长度
- Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml解决办法
- hibernate DAO中加了局物,未提交
- hibernate 字段lazy 的兑现原理
- Hibernate 的query.list()的有关问题
- Hibernate 关联表字段赋值有关问题
- 零配置的有关问题(struts2+Hibernate)
- hibernate 连接 oracle session 有关问题
- hibernate 中没有做一对多 , 那种关联映射的关联的话如何做连接查询
- hibernate 联接mysql出错
- 大神们开始觅错啦!org.hibernate.exception.SQLGrammarException: could not execute query
- struts2,hibernate,spring3 常用框架的API CHM版本,该怎么处理
- struts2,hibernate,spring3 惯用框架的API CHM版本
- struts hibernate 测试小例子 不报错 但数据库没反应 求分析项目在小弟我的资源里面
- struts2 hibernate 分页有关问题
- hibernate与此同时使用多数据源?spring+hibernate
- hibernate 保留对象时,根据映射文件,无法获取主键值
- hibernate 多对多添加有关问题,弄一天多了,请求大佬解答。小的 在此谢过了
- struts + hibernate 后台页面修改数据后,前台页面显示的内容新旧数据交替出现。请高手帮忙分析上原因
- hibernate 多对1关联配置有关问题
- Hibernate,级联保存为何无效?
- hibernate 一对1 ,插入不进去数据
- hibernate 连接oracle异常
- struts+spring+hibernate 开发,在action层调用service层接口,结果替null 注入失败,需高手帮忙
- Hibernate 映射有关问题
- SSH框架导入struts,hibernate,spring分先后顺序吗>>解决方法
- Hibernate 中的session怎么管理
- SSH框架(就是struts+spring+hibernate)是怎么搭建的
- 请教小弟我不使用Myeclipse,可以在tomcat里面整合Spring,Hibernate,Struts2吗