Hibernate空指针异常
来源:互联网 发布:郑也夫 知乎 编辑:程序博客网 时间:2024/04/30 01:33
类文件
package cn.jmi.model;
public class Person {
private int id;
private String name;
private float height;
void person()
{}
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 float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
映射文件
<?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 package="cn.jmi.model">
<class name="Person" table="Person">
<id name="id">
<generator class="assigned" />
</id>
<property name="name"/>
<property name="height"/>
</class>
</hibernate-mapping>
配置文件
<!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.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost;database=user</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.password">111111</property>
<property name="hbm2ddl.auto">validate</property>
<mapping resource="cn/jmi/model/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类
package test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.jmi.model.Person;
public class Test extends HibernateDaoSupport{
public List<Person> test()
{
List<Person> l = (List<Person>)getHibernateTemplate().find("from Person");
//select * from dept
return l;
}
public void test1()
{
Person person = (Person)getHibernateTemplate().get(Person.class, 1);
System.out.println(person.getName());
}
public static void main(String[] args) {
/*List<Person> l= new Test().test();
for(Person p:l)
{
System.out.println(p.getId()+","+p.getName());
}*/
//new Test().test1();
//System.out.print(new Test().test().get(0).getName()) ;
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
new Test().test1();
Session s = sf.openSession();
// System.out.println(new Test().test().get(0).getHeight());
s.close();
System.out.println("end");
}
}
错误
Exception in thread "main" java.lang.NullPointerException
at test.Test.test1(Test.java:21)
at test.Test.main(Test.java:35)
- hibernate 空指针异常
- Hibernate空指针异常
- Hibernate空指针异常
- 关于hibernate空指针异常的几个问题
- org.hibernate.util.ReflectHelper.getConstructor 空指针异常
- Hibernate--Session session = sessionFactory.getCurrentSession()空指针异常
- 空指针异常
- struts2空指针异常
- Struts2 空指针异常
- NullPointerException空指针异常
- 空指针异常总结
- getServletContext()空指针异常
- 空指针异常
- struts2空指针异常
- 空指针异常
- NullPointerException空指针异常
- 空指针异常
- 空指针异常
- Geocoder google map地点搜索----地址及经纬度的解析
- python challenge 第五关解法 http://www.pythonchallenge.com
- c/c++结构体几种定义情况总汇
- 流媒体服务器简介
- oracle+伪列
- Hibernate空指针异常
- Java 类加载器( ClassLoader)浅析
- 修改mysql提示符
- 【理想流】项目管理本质论
- Java 类加载体系与ContextClassLoader
- 使用ConnectifyInstaller软件模拟wifi热点,实现网络共享
- Unicode 与 ANSI 的编码转换
- POJ 1502 MPI Maelstrom
- j2ee环境的搭建